Dawn Abbot Dawn Abbot - 4 months ago 18
Node.js Question

Controlling a Stepper Motor with an Ardiuno and Node.js

My stepper motor is controlled using a L293d driver connected to an Ardiuno. The tutorial that I got my information from is here, https://learn.adafruit.com/wifi-control ... ino-sketch, but have changed the code to match for the stepper motor instead of a dc motor. Currently, I am just testing with serial input--no wireless yet.

I have everything working. When I press the forward button, the motor moves forward. However, it only rotates through one revolution. I am not sure how to change the code so that when I click the forward button it rotates forward continuously until I press the stop button. Does anyone know how to do this?

#include <SPI.h>
#include <aREST.h>
#include <Stepper.h>
#include <Wire.h>

aREST rest = aREST();

int inA1 = 12; // input 1 of the stepper
int inA2 = 11; // input 2 of the stepper
int inB1 = 10; // input 3 of the stepper
int inB2 = 9; // input 4 of the stepper


#define STEPS 512

Stepper motor(STEPS, inA1, inA2, inB1, inB2);

void setup() {
pinMode(inA1, OUTPUT);
pinMode(inA2, OUTPUT);
pinMode(inB1, OUTPUT);
pinMode(inB2, OUTPUT);

Serial.begin(115200);
Serial.println(F("Hello, CC3000!\n"));

rest.set_id("1");
rest.set_name("robot");

//Expose functions
rest.function("forward",forward);
rest.function("backward",backward);
rest.function("stop",stop);
}


void loop()
{

rest.handle(Serial);

}

// Forward
int forward(String command) {
motor.setSpeed(10);
motor.step(100);
return 1;
}

//Backforward
int backward(String command) {
motor.setSpeed(-10);
motor.step(100);
return 1;
}

Answer

Failed: Simple set motor.step(2147483647). 2147483647 is max value of int.


New attempt: try implement next logic. I'm not c-guru so below dirty code

int lastCommand = 0; // 0 - nothing; 1 - forward; 2 - backward; 3 - stop;
void forward () {
    lastCommand = 1;
    ...
}

void backward () {
    lastCommand = 2;
    ...
} 

void stop() {
   lastCommand = 0;
   ...
}

void loop() {
    rest.handle(Serial);
    if (lastCommand == 1)
      forward();
    if (lastCommand == 2)
      backward();
}

If we push backward + forward + backward button and send to Serial 3 commands then motor execute all of them consistently. Imho it's a problem.