Hi people.
Im building an airsoft bomb for some games.
I have one button to arm the bomb and one button to disarm the bomb.
When i disarm the bomb it shouldnt just be one click defuse.
My first idea was to use delay as this would get the program back to the millis that ran before i start defusing the bomb. But now that ive made it the delay does not stop exactly when the button is released again. Could i do something to stop a delay or is that exactly why people usually advice against delay?
My code with delay looks like this and works except for the slip once the button is released it keeps delaying til the 60 seconds has passed.
I guess the compilation error is because i use IF inside IF. All of this is inside a while and it works fine if the button is just pressed or i make the code with delay. I cant exactly figure out how to do it with millis.
Can i make it as a While inside a While to get the defuse timer working ?
Hint: Delay() works well but it is a dumb command. When activated it starts watching its clock and will do nothing but watch its clock until it has timed out. No other code will work while delay is watching its clock. As indicated in another post somebody can walk up to you and not be seen as you are busy with your phone.
Tailor this to suit your button pressing needs by substituting in that in the switch/case control structure.
Check it out to see if you get any ideas:
const int ledPin = LED_BUILTIN; // the number of the LED pin
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
int timer = 10; // Set the timer to 10 seconds
boolean bombArmed;
boolean gotTheMessage;
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
digitalWrite(ledPin, LOW);
bombArmed = true;
gotTheMessage = false;
}
void loop() {
if (Serial.available() > 0) {
char setBomb = Serial.read();
switch (setBomb) {
case 'a':
if (gotTheMessage == false) {
Serial.println("\nBomb is armed - do something!\n");
delay(1000);
gotTheMessage = true;
}
bombArmed = true;
break;
case 'd':
bombArmed = false;
Serial.println("\ndisarmed");
break;
case 'r':
Serial.println("\nreset clock");
timer = 10;
break;
}
}
if (bombArmed == true) {
theFinalCountdown();
}
digitalWrite(ledPin, LOW);
}
void theFinalCountdown() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
digitalWrite(ledPin, HIGH);
previousMillis = currentMillis;
Serial.println(timer);
timer--; //decrease timer count
if (timer < 0) {
Serial.println("\nBOOM!");
digitalWrite(ledPin, HIGH);
//while (true); // uncomment this if you don't want auto reset after 5 seconds
while (millis() < previousMillis + (interval * 5)) {
// do a whole lotta nothin'
}
timer = 10;
bombArmed = false;
gotTheMessage = false;
}
}
}