Hi, I'm working on a project involving a platform moving along a threaded bar in both directions. The platform is restrained by two limit switches at the end of the assembly. The platform should first reach the interrupt limit switch(pin 2), the rotation direction should then switch to take the platform to the other limit switch(pin 5). During this process the number of steps equating to the platform travelling one length of the assembly is counted. Experiencing problems currently whereby the motor initially rotates in the reverse direction rather than the forward direction which I've coded for. Here is a copy of my code. I'm very new to coding so point out any obvious issues.
Thanks
const int stepsPerRevolution = 200;
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
unsigned int REVcount = 0;
int delayLength = 1;
volatile int ISRcount = 0;
volatile int state = LOW;
int SW2 = 5;
void setup()
{
pinMode(3, OUTPUT);
pinMode(11, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
digitalWrite(3, LOW); //initially motor has zero motion, loop introduces first movement
digitalWrite(11, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
pinMode(5, INPUT);
attachInterrupt(0, REV, CHANGE);
Serial.begin(9600);
}
void loop()
{
if (ISRcount < 1)
{
enableMotor();
motorFWD(); //if ISRcount is below 1, motor forward
dissableMotor();
}
else
{
dissableMotor(); //if IRScount is above or equal to 1, dissable motor
}
int i = 0;
int x = 0;
if ((ISRcount == 1) && (Serial.available()>0))
{
int x = Serial.parseInt();
for (i = 0; i < x; i++) //for statement to loop motorFWD until it reaches position x
{
enableMotor();
motorFWD();
dissableMotor();
}
Serial.print("Platform calibration complete, at position ");
Serial.print(x);
Serial.print("/");
Serial.println(REVcount);
ISRcount = 2; //increase in ISRcount to prevent looping and start dissableMotor
}
else
{
dissableMotor();
}
}
void REV()
{
ISRcount = 1;
while (digitalRead(5) == HIGH) //Interrupt triggered by Switch 1 and ends when Switch 2 is no longer ON
{
enableMotor(); //Interrupt sequence, used to alter motion to REV to take platform to Switch 2.
motorREV(); //The number of steps are counted for use in later possible operations
REVcount++;
dissableMotor();
}
if (digitalRead(5) == LOW)
{
detachInterrupt(0); //when switch 5 is triggered, Interrupt ends
}
}
void enableMotor()
{
digitalWrite(3, HIGH);
digitalWrite(11, HIGH);
}
void dissableMotor()
{
digitalWrite(3, LOW);
digitalWrite(11, LOW);
}
void motorFWD()
{
digitalWrite(9, LOW); //enable CHA
digitalWrite(8, HIGH); //disable CHB
digitalWrite(12, HIGH); //sets direction of CHA
analogWrite(3, 255); //moves CHA
delay(delayLength);
digitalWrite(9, HIGH); //disable CHA
digitalWrite(8, LOW); //enable CHB
digitalWrite(13, LOW); //sets direction of CHB
analogWrite(11, 255); //moves CHB
delay(delayLength);
digitalWrite(9, LOW); //enable CHA
digitalWrite(8, HIGH); //disable CHB
digitalWrite(12, LOW); //sets direction of CHA
analogWrite(3, 255); //moves CHA
delay(delayLength);
digitalWrite(9, HIGH); //disable CHA
digitalWrite(8, LOW); //enable CHB
digitalWrite(13, HIGH); //sets direction of CHB
analogWrite(11, 255); //moves CHB
delay(delayLength);
}
void motorREV()
{
digitalWrite(9, LOW); //enable CHA
digitalWrite(8, HIGH); //disable CHB
digitalWrite(12, HIGH); //sets direction of CHA
analogWrite(3, 255); //moves CHA
delay(delayLength);
digitalWrite(9, HIGH); //disable CHA
digitalWrite(8, LOW); //enable CHB
digitalWrite(13, HIGH); //sets direction of CHB
analogWrite(11, 255); //moves CHB
delay(delayLength);
digitalWrite(9, LOW); //enable CHA
digitalWrite(8, HIGH); //disable CHB
digitalWrite(12, LOW); //sets direction of CHA
analogWrite(3, 255); //moves CHA
delay(delayLength);
digitalWrite(9, HIGH); //disable CHA
digitalWrite(8, LOW); //enable CHB
digitalWrite(13, LOW); //sets direction of CHB
analogWrite(11, 255); //moves CHB
delay(delayLength);
}