Hi, I am programming a tilt sensor. The idea sounds quite simple but I got stuck in the programming.
Basically there are two options. Standing and standing on top (180 degree).
I programmed a debounce for the tilt. The debounce time is 1 second.
After the switching of the tilt sensor an LED shall light up for half a second. Then there is a waiting time (e.g. 10 minutes). After the 10 min the LED shall light up for another half a second. That´s it for the first szenario.
If you turn around the tilt sensor for another 180 degree it starts from the beginning. LED half a second, 10 min waiting, Led half a second. Finished so far.
But if you turn around the tilt sensor during the 10 min waiting time, the waiting time starts again. For instace: you turn the tilt around, the LED flashes, you wait 7 minutes, you turn around the tilt sensor again you wait for 10 min, the LED flashes.
Could need some help
int tiltSens = 2;
int signal = 13;
int tiltStat;
int oldtiltStat = LOW;
unsigned long counterOne = 0;
int debounceTime = 1000;
int flashTime = 100;
int waitTime = 10000;
void setup()
{
pinMode(tiltSens, INPUT);
digitalWrite(tiltSens, HIGH);
pinMode(signal, OUTPUT);
digitalWrite(signal, LOW);
Serial.begin(9600);
}
void loop()
{
tiltStat = digitalRead(tiltSens);
if (tiltStat != oldtiltStat) {
counterOne = millis();
}
if ((millis() - counterOne) > debounceTime && (millis() - counterOne) < (debounceTime + flashTime) || (millis() + debounceTime + flashTime - counterOne) > waitTime && (millis() + debounceTime + flashTime - counterOne) < (waitTime + flashTime)) {
digitalWrite(signal, HIGH);
}
else{
digitalWrite(signal, LOW);
}
oldtiltStat = tiltStat;
}