// hysteresis demo sketch
void setup() {
Serial.begin(9600);
Serial.println("HJello World!\n");
}
unsigned char sendLowMessage = 1; // yes, we need to send the low message
unsigned char sendHighMessage = 1; // yes, we need to send the high message
void loop() {
int distance_in_cm = getTheDistance(); // Write that function. Unclutter your loop!
if (distance_in_cm <= 333) {
if (sendLowMessage) {
// call the function that sends the Red Light Message
sendRedLightMessage(); // Write another function. Unclutter your loop!
sendLowMessage = 0; // disable the Red Light Message
sendHighMessage = 1; // enable the Green Light Message
}
}
if (distance_in_cm >= 666) {
if (sendHighMessage) {
// call the function that sends the Green Light Message
sendGreenLightMessage(); // Write yet another function. Unclutter your loop!
sendHighMessage = 0; // disable the Green Light Message
sendLowMessage = 1; // enable the Red Light Message
}
}
Serial.println("idle, no need to do nothing");
}
int getTheDistance()
{
return analogRead(0);
}
void sendRedLightMessage()
{
Serial.println("\nsending RED message boss!\n");
delay(777);
}
void sendGreenLightMessage()
{
Serial.println("\nsending GREEN message boss!\n");
delay(777);
}