Hey folks,
Right off the bat, I’m a complete newbie when it comes to arduino, but I’m pretty quick at picking things up and interested in learning.
The idea i have is for an electronic capture the flag pole. There would be two buttons on a marker pole, one for each team. If team A gets to the pole they must hold a button down for one minute to make the leds flash. Once they are lit, if team B wanted to capture the same marker, they would have to hold down their button for 30 seconds to reset the pole, and then continue to hold it for another minute to capture it themselves.
I have been looking around, and after getting help with the following, I should be able to code the press and hold to light an led like this. ( LED will probably be switched with a relay for fancier lighting later)
/*
* Press & Hold Switch LED program
*/
// Parameters
int HOLD_DELAY = 60000; // Sets the hold delay of switch for LED state change to 60seconds
int ledPin = 9; // LED is connected to pin x
int switchPin = 3; // Switch is connected to pin x
// In-Program Variables
unsigned long start_hold;
boolean allow = false;
int sw_state;
int sw_laststate = LOW;
int led_state = LOW;
// Setup
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
pinMode(switchPin, INPUT); // Set the switch pin as input
}
// Loop
void loop(){
sw_state = digitalRead(switchPin); // read input value
if (sw_state == HIGH && sw_laststate == LOW){ // for button pressing
start_hold = millis(); // mark the time
allow = true; // allow LED state changes
}
if (allow == true && sw_state == HIGH && sw_laststate == HIGH){ // if button remains pressed
if ((millis() - start_hold) >= HOLD_DELAY){ // for longer than x/1000 sec(s)
led_state = !led_state; // change state of LED
allow = false; // prevent multiple state changes
}
}
sw_laststate = sw_state;
digitalWrite(ledPin, led_state);
}
And I can code the second button for team b in exactly the same way just switching the button input and led ouput pins. Where I am stumped is getting the “Reset other teams light” feature integrated. I suppose if I left the code as is, they could just hold down the lit teams button and then their own, but that just leaves it open to cheating.
Any help here would be greatly appreciated.