I am trying to write a code that will monitor the status of 2 limit switches at the same time, serial printing the status of each one.
Currently it wont print that neither limit is active, nor will it update if both limits are pressed when one is held and the second is activated and de activated.
any help would be great.
code is below:
int Lswitch = 2;
int Rswitch = 4;
int Lflag = 0;
int Rflag = 0;
int Ract = 0;
int Lact = 0;
int Rinact = 0;
int Linact = 0;
void setup()
{
Serial.begin(9600);
pinMode(Lswitch, INPUT);
pinMode(Rswitch, INPUT);
}
void loop()
{
if( ((digitalRead(Lswitch) == LOW) && (Linact == 0)) && ((digitalRead(Rswitch) == LOW) && (Rinact == 0)))
{
Serial.println("no limit active");
Linact = 1;
Rinact = 1;
delay(20);
}
if( ((digitalRead(Lswitch) == HIGH) && (Lact == 1)) && ((digitalRead(Rswitch) == HIGH) && (Ract == 1)))
{
Serial.println("both limits active");
Lact = 0;
Ract = 0;
delay(20);
}
if( (digitalRead(Lswitch) == HIGH) && (Lflag == 1))
{
Serial.println("L switch limit active");
Lflag = 0;
Lact = 1;
delay(20);
}
if( (digitalRead(Lswitch) == LOW) && (Lflag == 0))
{
Serial.println("L switch limit inactive");
Lflag = 1;
Linact = 1;
delay(20);
}
if( (digitalRead(Rswitch) == HIGH) && (Rflag == 1))
{
Serial.println("R switch limit active");
Rflag = 0;
Ract = 1;
delay(20);
}
if( (digitalRead(Rswitch) == LOW) && (Rflag == 0))
{
Serial.println("R switch limit inactive");
Rflag = 1;
Rinact = 1;
delay(20);
}
if( (digitalRead(Lswitch) == LOW) && (Lflag == 0))
{
Lflag = 1;
Linact = 0;
delay(20);
if( (digitalRead(Rswitch) == LOW) && (Rflag == 0))
{
Rflag = Rflag;
Rinact = 0;
}
delay(20);
}
if( (digitalRead(Rswitch) == LOW) && (Rflag == 0))
{
Rflag = 1;
Rinact = 0;
delay(20);
if( (digitalRead(Lswitch) == LOW) && (Lflag == 0))
{
Lflag = Lflag;
Linact = 0;
}
delay(20);
}
digitalWrite(Lswitch, HIGH);
digitalWrite(Rswitch, HIGH);
}