hi i am trying to control the stepper motor through GUI built on python.. i have written a code such that when i recieved a character from the GUI motor should rotate
in the Arduino serial monitor after reading a character it will end up with -1
here is my code please help
int motorPin1 = 8; // stepper motor pins connected to pin 8,9,10,11 of the arduino
int motorPin2 = 9;
int motorPin3 = 10;
int motorPin4 = 11;
int delayTime = 100; // delay time of half second
int incomingByte = 0; // for incoming serial data
int x,y;
void steppermotor(int dir, int ang);
void setup()
{
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop()
{
if (Serial.available() > 0)
{
Serial.println(incomingByte = Serial.read());
/the variable x is taken for to select clockwise or anticlockwise/
if (incomingByte == 97) // decimal value for a is 97
{
x = 0;
}
else if (incomingByte == 98) // decimal value for b is 98
{
x = 1;
}
Serial.println(incomingByte = Serial.read());
if (incomingByte == 99) // decimal value for c is 99
{
y = 90; //the variable y is taken for to select clockwise or anticlockwise
}
else if (incomingByte == 100) // decimal value for d is 100
{
y = 180;
}
else if (incomingByte == 101) // decimal value for e is 101
{
y = 270;
}
else if (incomingByte == 102) // decimal value for f is 102
{
y = 360;
}
steppermotor(x,y); // the variable dir is passed to x and variable y is passed to ang (dir = direction,ang = angle)
}
}
void steppermotor(int dir, int ang)
{
if(dir == 1)
{
for(int p=0; p<ang/7.2; p++) // condition p = ang/1.8*4
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
}
}
if (dir ==0)
{
for(int p=0; p<ang/7.2; p++)
{
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
}
}
}