First project.

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.

Something like this might be close to what you want. It doesn’t blink the lights and has separate lights for the two teams.

/*
 *  Press & Hold Switch LED program
 */

// Parameters
const unsigned long HOLD_DELAY = 60000;    // Sets the hold delay of switch for LED state change to 60 seconds
 
const int redTeamLEDPin= 9;
const int blueTeamLEDPin = 10;

const int redButtonPin = 2;
const int blueButtonPin = 3;

bool redButtonState = 0;
unsigned long redButtonStateChangeTime;
bool blueButtonState = 0;
unsigned long blueButtonStateChangeTime;

// 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(redTeamLEDPin, OUTPUT);      // Set the LED pin as output
  pinMode(blueTeamLEDPin, OUTPUT);      // Set the LED pin as output
  digitalWrite(redTeamLEDPin, LOW);
  digitalWrite(blueTeamLEDPin, LOW);
  pinMode(redButtonPin, INPUT);    // Set the switch pin as input
  pinMode(blueButtonPin, INPUT);    // Set the switch pin as input
}
 
// Loop
void loop(){
  // Handle red team button
  if (digitalRead(redButtonPin) != redButtonState)
      {
      redButtonState = ! redButtonState;  // Toggle the state
      redButtonStateChangeTime = millis();
      }
  if (redButtonState && millis() - redButtonStateChangeTime > HOLD_DELAY)
      {
      // Buttonb has been down the required time
      digitalWrite(redTeamLEDPin, HIGH);
      digitalWrite(blueTeamLEDPin, LOW);
      }

  // Handle blue team button
  if (digitalRead(blueButtonPin) != blueButtonState)
      {
      blueButtonState = ! blueButtonState;  // Toggle the state
      blueButtonStateChangeTime = millis();
      }
  if (blueButtonState && millis() - blueButtonStateChangeTime > HOLD_DELAY)
      {
      // Buttonb has been down the required time
      digitalWrite(redTeamLEDPin, LOW);
      digitalWrite(blueTeamLEDPin, HIGH);
      }   
  }

Awesome, I think thats exactly what i needed! Thank you so much!