Problems powering stepper motor from web server (button) via Arduino Yun

Hi guys. I have my Arduino Yun set up with a web server. The webpage has a series of buttons on it, which control the buzzer playing various sounds, and a button to control a stepper motor. I'm using the code linked which uses the AccelStepper library.

The button works fine for the buzzer, I click it and it plays the sound. For the motor however, I click the button, and the LED lights up on the driver board for a second after the button is clicked but that's it.

I suspect that the motor, unlike the buzzer, needs the command to be true all the time while it moves to it's position, otherwise it stops. So the button on the webpage needs to act more like a switch - click to come on and stay on, click again to go off and stay off. But I'm unsure how to do this.

Or else, perhaps anyone has experience with this stepper motor and knows how to use it without accelstepper. (All i'm trying to do is get the motor to turn to a given position.)

I hope this isn't too confused. I'm so close, yet this motor is causing me a massive pain. Many thanks!

The sketch code:

//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 code:

Roll Out the Carpet      LED BLUE OFF

PLAY FANFARE
       STOP FANFARE

stepper1.run() simply tells the library to see if the stepper is at the commanded position. If so, nothing happens. If not, the stepper motor steps ONCE.

I suspect that the motor, unlike the buzzer, needs the command to be true all the time while it moves to it's position, otherwise it stops.

No. YOU need to create a blocking function that tells the stepper where to go, and then waits for it to get there. Completely defeating the purpose of using AccelStepper. You might as well use Stepper, and just call step(n), where n defines the number of steps to move. That function takes care of the blocking for you.

Hi Paul, thanks for your reply.
I understand your point which makes sense why it's not working at the moment.
The reason I used AccelStepper was that the Arduino stepper doesn't work very smoothly with this motor. Is there no way rework the code I was using (The first block of code on this page 28BYJ-48 Stepper Motor with ULN2003 driver and Arduino Uno – 42 Bots) to tell it to move to a certain position?

I came accross this (http://arduino-info.wikispaces.com/SmallSteppers) since your post, but the movement of the motor itself is really jerky, not at all as smooth as the code in the previous link.
Best,
T

EDIT: The first set of code in my second link - which uses stepper as you recommended - works perfectly with the button. But the movement is pretty horrible. Jerky and awkward. Is there any way of modifying the code in the first link to make it blocking?

Is there any way of modifying the code in the first link to make it blocking?

Yes. The AccelStepper library has a distanceToGo() function that reports how many steps are still needed to get to the target position.

    stepper.moveToPosition(50000); // take 50000 steps
    while(stepper.distanceToGo() != 0) // While not there
    {
        stepper.run(); // Take another step
    }

Paul I could kiss you! It worked a dream.
The using AccelStepper the motor seems to have way more torque. now I can just fiddle with the speed settings.
Many, many thank you's.