Hey guys, I have written the code below with some help from this thread . I am life-cycle testing an electric actuator and I am using two reed switches (one at full stroke and one at bottom stroke). I need the actuator to go up until full stroke, wait 5 seconds, then go down to fully retracted, wait 5 seconds, and repeat. The code I have works in the sense that it will run the motor up until the upper switch is closed and then will go down until the down switch is closed. I need to add the 5 second delay in, but when I try, the code no longer works. Any help or advice is greatly appreciated.
//constants
const int DirectionPin = 5;
const int PwmPin = 6;
const int UpSwitch = 4;
const int DownSwitch = 3;
//variables
boolean UpSwitch_CurrentState = HIGH;
boolean DownSwitch_CurrentState = HIGH;
void setup() {
Serial.begin(9600);
pinMode (DirectionPin, OUTPUT); //Direction pin for H-Bridge
pinMode (PwmPin, OUTPUT); //PWM pin for H-Bridge
pinMode (UpSwitch, INPUT_PULLUP); //Up stroke limit switch
pinMode (DownSwitch, INPUT_PULLUP); //Down stroke limit switch
}
void loop()
{
UpSwitch_CurrentState = digitalRead(UpSwitch);
{
if (UpSwitch_CurrentState == LOW)
while ((DownSwitch_CurrentState = digitalRead(DownSwitch)) != LOW) {
DOWN();
}
{
DownSwitch_CurrentState = digitalRead(DownSwitch);
{
if (DownSwitch_CurrentState == LOW)
while ((UpSwitch_CurrentState = digitalRead(UpSwitch)) != LOW) {
UP();
}
}
}
}
}
void UP() { //Setting for motor up
digitalWrite(PwmPin, HIGH);
digitalWrite(DirectionPin, LOW);
}
void DOWN() { //Setting for motor down
digitalWrite(PwmPin, HIGH);
digitalWrite(DirectionPin, HIGH);
}
void STOP() { //Setting for motor stop/break
digitalWrite(PwmPin, LOW);
digitalWrite(DirectionPin, LOW);
}
P.S. My apologies for any coding errors/taboos.