Pages: [1]   Go Down
Author Topic: push buttonsss  (Read 583 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:



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..smiley-grin please help for better code..

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26350
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:

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...
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3383
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:

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:
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.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

digitalRead is relatively slow.

Not compared to me it isn't.
Logged


Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 616
Posts: 49444
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Not compared to me it isn't.
It's all about perspective.
Logged

Pages: [1]   Go Up
Jump to: