Modified existing project from this link: Problem : Trigger limit switch to stop DC motor
I modified this code to control two relays which are wired to motor poles for Forward or Reverse movements until they physically reach a limit switch on either side.
- Serial input of 'A' turns on one relay. If
Limitswitch1
is pressed, this relay switches off. - It detects the current state of the system and awaits the Serial input of 'B' which then turns on the second relay. If
Limitswitch2
is pressed, this relay switches off.
The issues are:
Please help with code - I require option where the two relays switch off on the serial input of letter 'S' and to add the corresponding state machine. Essentially to add a Stop function.
The limit switches work intermittently, sometimes they respond flawlessly, sometimes not at all. I have bought three different brands of switches to test but all have the same behaviour. I have pulled switches up and down with resistors in vain. I have performed button count test using code from this forum and confirm that the switch also behaves erratically. Can changes to code improve switch feedback?
Suggestions will be greatly appreciated.
const byte RELAY1 = 2;
const byte RELAY2 = 3;
const byte LimitSwitch1 = A1;
const byte LimitSwitch2 = A2;
const byte State_Idle = 0;
const byte State_Forward = 1;
const byte State_Reverse = 2;
byte currentState;
void setup(){
pinMode(RELAY1,OUTPUT);
pinMode(RELAY2,OUTPUT);
pinMode(LimitSwitch1, INPUT);
pinMode(LimitSwitch2, INPUT);
Serial.begin(9600);
currentState = 0xFF; // Force a change to enter the idle state
}
void loop(){
// Check if we need to change state
byte nextState = currentState;
switch (currentState)
{
case State_Idle:
{ // If we received a valid command, start the operation
if(Serial.available() > 0)
{
switch (Serial.read())
{
case 'A':
nextState = State_Forward;
break;
case 'B':
nextState = State_Reverse;
break;
default:
break;
}
}
}
break;
case State_Forward:
if(digitalRead(LimitSwitch2))
nextState = State_Idle;
break;
case State_Reverse:
if(digitalRead(LimitSwitch1))
nextState = State_Idle;
break;
default:
nextState = State_Idle;
break;
}
// If there was a change, switch state and trigger enter condition
if (nextState != currentState)
{
currentState = nextState; // Move in the new state
switch (currentState)
{ // Entering the new state
case State_Idle:
// Switch both Relays OFF
digitalWrite(RELAY1, HIGH);
digitalWrite(RELAY2, HIGH);
break;
case State_Forward:
// Move Forward
digitalWrite(RELAY1, LOW);
digitalWrite(RELAY2, HIGH);
break;
case State_Reverse:
// Move Reverse
digitalWrite(RELAY1, HIGH);
digitalWrite(RELAY2, LOW);
break;
}
}
}