Hello there,
I've been trying to drive a stepper motor and everything has been working fine so far... I managed to make it go four steps at a time. But now, I wanted to make the motor go only one step. I tried having a look at other people's code but all I found were libraries...
This is what I wrote (proud of it, kinda):
//Motor A
const int motorPin1 = 7; // Pin 14 of L293
const int motorPin2 = 2; // Pin 10 of L293
const int PwmPinA = 5;
//Motor B
const int motorPin3 = 3; // Pin 7 of L293
const int motorPin4 = 4; // Pin 2 of L293
const int PwmPinB = 6;
//Variablen
int steps;
int x ;
void setup(){
Serial.begin(9600);
//Set pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(PwmPinA, OUTPUT);
pinMode(PwmPinB, OUTPUT);
analogWrite(PwmPinA, 255);
analogWrite(PwmPinB, 255);
}
int linksDrehen()
{
int Delay = 100;
x = x + 1;
// if(x > 4)
// {
// x = 1;
// }
if(x = 1)
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(Delay);
Serial.print(x);
Serial.println(" M1");
}
if(x = 2)
{
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(Delay);
Serial.print(x);
Serial.println(" M2");
}
if(x = 3)
{
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(Delay);
Serial.print(x);
Serial.println(" M3");
}
if(x = 4)
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(Delay);
Serial.print(x);
Serial.println(" M4");
}
}
void loop()
{
linksDrehen();
Serial.println("---------");
delay(1000);
}
the algorithm is supposed to go like this:
-add one to the value of x
- is it higher than four? If yes, set to one
- activate the step corresponding to the value of x
It takes the stepper four steps to be able to repeat the pattern. I'm really confused because it already worked when I tried it out without all the if's.
In desperate need of help,
Smagel
P.S.: I need it for school and it will be graded! I need help...