Stepper Motor Control! :(

Hello,

I’ve been trying to control a stepper motor through the Leap Motion (motion sensor), Processing,
Adafruit Motor Shield v2 and Arduino.

I think I am done with sending data from Processing to Arduino
but I guess there is something wrong with my Arduino code :frowning:


what I am trying to do:

a. The motor runs clockwise, rpm 45.

b. (whilst the motor’s running) when my hand’s on the left, the Leap Motion detects it,
and Processing sends “1” to Arduino.
when Arduino receives “1”, the stepper motor turns counter clockwise.

c. (Whilst the motor’s running) when my hand’s on the right, the Leap Motion detects it, and Processing sends “3” to Arduino.
when Arduino receives “3”, the stepper motor turns clockwise - Back to “a”.


Arduino Code:

#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include “utility/Adafruit_PWMServoDriver.h”

//To Use MotorShield for Stepper Motor (200 Steps)
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

void setup() {
AFMS.begin();
Serial.begin(9600);
}

void loop(){
// set 45rpm
myMotor->setSpeed(45);

// the motor runs clockwise ----> “a” part I mentioned above.
myMotor->step(200, FORWARD, DOUBLE);
byte val;

// if Processing is sending data
if (Serial.available()) {

// the data will be assigned as “val”
val = Serial.read();

// if val (the data from Processing) is 1 ---------> “b” part I mentioned above
if (val = 1) {

// the motor turns counter clockwise
myMotor->step(200, BACKWARD, DOUBLE);}

// if val (the data from processing) is 3 -----------> “c” part I mentioned above
else if(val = 3){

// the motor turns clockwise
myMotor->step(200, FORWARD, DOUBLE);
}
}


the result of this Arduino code:

the motor runs 45 rpm, clockwise as I set at the top of “void loop” but doesnt turn clockwise
when Processing sends “1” to Arduino (“b” part doesnt work)
I think Processing sends the data well but don’t know why my code doesnt work… :frowning:


what do you think?

I think it is just that your IF statements are wrong. They should be

if (val == 1) {

and not

if (val = 1) {

You may be interested in stepper motor basics

…R