我用网络服务器设置了我的 Arduino Yun。该网页上有一系列按钮,用于控制蜂鸣器播放各种声音,以及一个用于控制步进电机的按钮。我正在使用使用 AccelStepper 库的链接代码。
该按钮适用于蜂鸣器,我单击它并播放声音。然而,对于电机,我点击按钮,点击按钮后驱动板上的 LED 会亮一秒钟,仅此而已。
我怀疑电机与蜂鸣器不同,它在移动到它的位置时需要命令始终为真,否则它会停止。所以网页上的按钮需要更像一个开关——点击开启并保持开启,再次点击关闭并保持关闭。但我不确定如何做到这一点。
或者,也许任何人都有使用此步进电机的经验,并且知道如何在没有 accelstepper 的情况下使用它。(我要做的就是让电机转到给定位置。)
我希望这不会太混乱。我已经很近了,但这个马达却让我感到非常痛苦。非常感谢!
草图代码:
//Bridge Setup
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
YunServer server;
String readString;
//Motor Setup
#include <AccelStepper.h>
#define HALFSTEP 8
// Motor pin definitions
#define motorPin1 3 // IN1 on the ULN2003 driver 1
#define motorPin2 4 // IN2 on the ULN2003 driver 1
#define motorPin3 5 // IN3 on the ULN2003 driver 1
#define motorPin4 6 // IN4 on the ULN2003 driver 1
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
//////////////
//const int thereader = 8;
int Buzzer1 = 9;
//int readerstate = 0;
void setup() {
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(100.0);
stepper1.setSpeed(200);
stepper1.moveTo(20000);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(9, LOW);
Bridge.begin();
//digitalWrite(13, HIGH);
// Listen for incoming connection only from localhost
// (no one from the external network could connect)
server.listenOnLocalhost();
server.begin();
////////////
//pinMode(Buzzer1, OUTPUT);
// initialize the pushbutton pin as an input:
//pinMode(thereader, INPUT);
}//--(end setup )---
void loop() {
YunClient client = server.accept();
// There is a new client?
if (client) {
// read the command
String command = client.readString();
command.trim(); //kill whitespace
Serial.println(command);
if (command == "rolloutcarpet") {
stepper1.run();
//delay(2000);
//stepper1.moveTo(-stepper1.currentPosition());
}
if (command == "playfanfare") {
tone(Buzzer1,400,200);
delay(500);
tone(Buzzer1,400,200);
delay(500);
tone(Buzzer1,450,225);
delay(300);
tone(Buzzer1,450,225);
delay(500);
tone(Buzzer1,400,200);
delay(500);
tone(Buzzer1,450,200);
delay(300);
tone(Buzzer1,600,300);
delay(3000);
}
else if (command == "stopfanfare") {
delay(10000);
}
client.stop();
}
delay(50);
}
HTML 代码:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="zepto.min.js"></script>
<script type="text/javascript">
function rolloutcarpet()
{
$('#content').load('/arduino/rolloutcarpet');
}
function playfanfare()
{
$('#content').load('/arduino/playfanfare');
}
function stopfanfare()
{
$('#content').load('/arduino/stopfanfare');
}
</script>
</head>
<body onload="setInterval(refresh, 500);">
<span id="content"></span>
<button onclick="rolloutcarpet()">Roll Out the Carpet</button>
<button onclick="ledoff()">LED BLUE OFF</button>
<br><br><br>
<button onclick="playfanfare()">PLAY FANFARE</button>
<button onclick="stopfanfare()">STOP FANFARE</button>
<img src="yun.png" align="right">
</body>
</html>