Go Down

Topic: push buttonsss (Read 731 times) previous topic - next topic

Code: [Select]




int b1 = 9;
int b2 = 8;
int


//button status
boolean bu1 =0;
boolean bu2 =0;

//Status of whether or not a button has been pressed
int hold = 0;


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);

 
 //The delay allows time for the voltage to stabilize (exp. because the electronics are not removed from the buttons!)
 delay(1000);
}

void loop() {
 
 //Read to see if a button is pushed
 if(hold == 0){
   bu1 = digitalRead(b1);
   bu2 = digitalRead(b2);
   
   //player 1
   if(digitalRead(b1)==HIGH||digitalRead(b2)==LOW)
  {
     hold=1; //lockout the other users
     Serial.println("Player 1");
   }
   //player 2
   if(digitalRead(b2)==HIGH||digitalRead(b1)==LOW){
   
     hold=1; //lockout the other users
     Serial.println("Player 2");

   }
 }
 else{
   digitalWrite(13, HIGH);   // set the LED on to indicate that the other users are locked out
 }
}


void time(){

delay(15000);
}
[font=Verdana][/font]



Sir i need a little help..i am a beginner here,,i dont think this gonna work..:D please help for better code..

i want to output the first button who pressed first. thanks

AWOL

Quote
,i dont think this gonna work.

Not a great attitude to approach software development.

Make sure both switches are on the same port and read the whole port.
digitalRead is relatively slow.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

tuxduino

Quote
,i dont think this gonna work.


You give up without even trying ? Make some experiments with your code, post your results and _then_ ask questions. :)

Code: [Select]


const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;     // the number of the LED pin



// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int hold=0;

void setup() {
 
// initialize the LED pin as an output:

  pinMode(ledPin, OUTPUT);      

  // initialize the pushbutton pin as an input:

  pinMode(buttonPin, INPUT);    
}

void loop(){

  // read the state of the pushbutton value:

  buttonState = digitalRead(buttonPin);


  // check if the pushbutton is pressed.

  // if it is, the buttonState is HIGH:
if(hold==0)
{

if(buttonState1||buttonState2){
delay(5000);

{
  if (buttonState == HIGH) {  
    // turn LED on:    
hold=1;
digitalWrite(ledPin1, HIGH);  
  }
if(buttonState2==HIGH){
hold=1;
digitalWrite(ledPin2, HIGH);
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}
{




well thanks sir..i have sought a new code..i just need it yout reply...

Arrch


Code: [Select]


const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;     // the number of the LED pin



How can you check which person pressed their button first with only a single button

Quote
// check if the pushbutton is pressed.

How?

Code: [Select]
if(buttonState1||buttonState2){
delay(5000);

{
  if (buttonState == HIGH) {  

You're checking three different buttonState variables, but you've only declared and read 1; That's not going to work.

tuxduino

The last code you posted doesn't even compile. Please take at least the effort to post compilable code. Thank you.

Nick Gammon


digitalRead is relatively slow.


Not compared to me it isn't.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

Quote
Not compared to me it isn't.

It's all about perspective.

Go Up