Hello Guys ;
I'm trying to write a code to do the following:
When I press switch 1 , the Loop will ignore switch 2 and Vice Versa. I use for this an infinite loop "while(1)" and It's working great , the problem is ofc I can't call functions from outside the while(1) loop so if I want to display for example I must write Serial.println("switch1 on"); inside the infinite loop !
So how can I replace the while so I can call functions in order to add more actions when the switch is HIGH.
int switch1 = 37;
int switch2 = 38;
int led1 = 34;
int led2 = 35;
enum disVar
{
serial,
serial1,
serial2
};
byte disVar = serial;
void setup()
{
pinMode(switch1, INPUT);
pinMode(switch2, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
Serial.begin(9600);
}
void loop()
{
DisplayDataLCD();
checkswitch1();
checkswitch2();
delay(250);
}
void checkswitch1()
{
while (1)
{
if (digitalRead(switch1) == HIGH)
{
disVar = serial1;
digitalWrite(led1, HIGH);
} else
{
disVar = serial;
digitalWrite(led1, LOW);
break;
}
}
}
void checkswitch2()
{
while (1)
{
if (digitalRead(switch2) == HIGH)
{
disVar = serial2;
digitalWrite(led2, HIGH);
} else
{
disVar = serial;
digitalWrite(led2, LOW);
break;
}
}
}
void DisplayDataLCD()
{
switch (disVar)
{
case serial1 :
{
Serial.println("switch1 on");
break;
}
case serial2 :
{
Serial.println("switch2 on");
break;
}
case serial:
{
Serial.println("greetings");
break;
}
break;
}
}
Thx )))))