Hello!
I am trying to control a stepper motor using MATLAB. The matlab code takes an input and assigns a value of "1" if its positive and a value of "2" if its negative. The ardunio will read this info and a rotation value, recMove to turn the motor a certain number of times in a certain direction. I understand that Serial.read can only read the first byte coming in, so how can I make the Serial.read work for my recDirection variable?
My code is attached below:
Than you!
// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
const int ledpin = 13;
int recValue;
int recMove;
int recDirection;
int movem;
void setup() {
// Sets the two pins as Outputs
Serial.begin(9600);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
}
void loop()
{
if (Serial.available()>0)
{
//recValue = Serial.read();
recMove = Serial.read();
movem=recMove;
//Serial.end()
; //Serial.begin(9600)
delay(250);
recDirection = Serial.read();
Serial.println(recDirection);
if (recDirection = 2)
{
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < (movem*200); x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(4000);
digitalWrite(stepPin,LOW);
delayMicroseconds(4000);
}
delay(4000); // One second delay
}
else if (recDirection = 1)
{
digitalWrite(dirPin,LOW); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < (movem*200); x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(4000);
digitalWrite(stepPin,LOW);
delayMicroseconds(4000);
}
delay(4000); // One second delay
}
Serial.println(recDirection)
;for (;;){} // kils the program
}
}