goodmorning guys..
i need a little help with this project..i want to play it like a game..the first person to press will win..but the problem is that the first player will always win at the loop..it does not call for the timer function..why?
//Define the Buttons
#include "ClickButton.h"
//button input pins (digital)
int b1 = 7;
int b2 = 8;
int b3 = 9;
const int bu4=6;
//button status
int bu1, bu2, bu3 = 0;
/////////////////////
int lastClickCode=0;
int r,x=0;
//Status of whether or not a button has been pressed;
int hold = 0;
ClickButton button1(bu4, LOW, CLICK_PULLUP);
void setup() {
//allows you to see on a computer who buzzed in first
Serial.begin(9600);
//Lockout Status LED pin with is statndard on the Arduino Board(s)
pinMode(13, OUTPUT);
//Button inputs defined
pinMode(b1, INPUT);
pinMode(b2, INPUT);
pinMode(b3, INPUT);
//The delay allows time for the voltage to stabilize (exp. because the electronics are not removed from the buttons!)
delay(1000);
}
void loop(){
sign();
}
void sign() {
//Read to see if a button is pushed
if(hold == 0){
bu1 = digitalRead(b1);
bu2 = digitalRead(b2);
bu3 = digitalRead(b3);
//If the status of any of the Buttons has changed to being pulled low
//lockout the other users and light up the appropriate button
//player 1
if(bu1==0){
hold = 1; //lockout the other users
Serial.println("Player 1");
timer();
}
//player 2
if(bu2 == 0){
hold = 1; //lockout the other users
Serial.println("Player 2");
delay(3000);
timer();
}
//player 3
if(bu3 == 0){
hold = 1; //lockout the other users
Serial.println("Player 3");
delay(3000);
timer();
}
}
}
void timer(){
r=1;
if(r==1){
// Note: Will return the last click decoded, even if no new clicks are received!
button1.Update();
// Toggle LED on single clicks. But only if the click code is new
if(button1.click == CLICK_SINGLECLICK && button1.click != lastClickCode)
{
Serial.println("happily");
}
// blink faster if double clicked
if(button1.click == CLICK_DOUBLECLICKED&&button1.click!=lastClickCode)
{
Serial.println("inlove");
}
// blink even faster if triple clicked
if(button1.click == CLICK_TRIPLECLICKED&&button1.click!=lastClickCode) {
Serial.println(" ");
}
// update the LED
// Save previous button click code
lastClickCode = button1.click;
x=1;
if(x==1){
hold=0;
sign();
}
}
}
///////////////////////////////SELECTS TIME TO USE////////////////////////////////