digitalRead

Hello all.
I have a question regarding digitalRead.

when I use it when I have just one button it works just fine.

but when I use digitalRead twice in the same program (two different buttons) then it will simply not work.
are there some buffers that need to be flushed ? is there a bug in this command ?
am i simply missing something very very fundamental ?

thanks
Angel

is there a bug in this command ?

Possible, but just how likely do you think it might be?
How many posts are there here, having found such a bug?

I'd love to help, but I can't see your code.

thanks for replying.

this code is meant to be a game for disabled kids (we are building a special press button for them)
any way, this is a simple press button that lite up leds one by one, and also allow to reset the game.

I know my code is written badly, I did try and make is as easy to follow as possible.

Thanks
Angel

//****************************************************************************//
//****************************************************************************//
/*
decide which PIN is for a LED
and which PIN in ment for a button
*/

const int buttonPin =9;     // the number of the pushbutton pin             
const int led1 =  1;      // the number of the LED pin
const int led2 =  2;      // the number of the LED pin
const int led3 =  3;      // the number of the LED pin
const int led4 =  4;      // the number of the LED pin
const int  buz =  5;       // the number of the buzzer pin
const int res =  0; // the number of the reset                                          


/*
control the time for the BLINK
*/
const int delayCountDown = 500;  //delay countdown
const int delayblink = 200;    //delay for blink

//****************************************************************************//
//****************************************************************************//
 



//set values
const int deleyRICHOD = 10; //delay for switcing
int buttonState;                     // variable for reading the pushbutton status
int counter = 0;                        //main counter
int firstTime = 1;                   //flag
int resetState;                       // variable for reading reset status
int flagReset = 0;
 
 
// actual program beggins
void loop(){
  
  if (firstTime == 1){
    countDown();
    firstTime=0;
  }//if
    
  //check for reset
  resetState = digitalRead(res);

  if (resetState == LOW){
// should be resetBoard()    but I just want to see that it will actually work
    countDown();
  }


//was a button pressed ?
  buttonState = digitalRead(buttonPin);

  if(buttonState == LOW){

    counter++;    
    
    if(counter == 0)
      zero();
  
    if(counter ==  1)
      one();
  
    if(counter ==  2)
      two();
  
    if(counter ==  3)
      three();
   
    if(counter ==  4){
      four();
      counter = 0;
      victory();
    }//(counter ==  4)
   
    checkButtonReleae();
  }//(buttonState == LOW)
} //loop


void setup() {

  // initialize the LED pin as an output:
  pinMode(buttonPin, INPUT);     

  pinMode(led1, OUTPUT);     
  pinMode(led2, OUTPUT);     
  pinMode(led3, OUTPUT);     
  pinMode(led4, OUTPUT);     

  pinMode(buz, OUTPUT);     

  pinMode(res, INPUT);     

}


void zero(){
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);   
} //zero


void one(){
   digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);   
} //one


void two(){
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);   
} //two


void three(){
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    digitalWrite(led4, LOW);   
} //three


void four(){
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    digitalWrite(led4, HIGH);   
} //four

void five(){
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    digitalWrite(led4, HIGH);   
} //five

void six(){
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    digitalWrite(led4, HIGH);   
} //six

void full(){
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    digitalWrite(led4, HIGH);   
} //full

void victory(){
  digitalWrite(buz, HIGH);

  for (int i=0; i<4; i++){
      delay(delayblink);   
      zero();
      delay(delayblink);   
      full();
  }//for
  delay(delayblink);   
  zero();
  digitalWrite(buz, LOW);
  
}//victory

void checkButtonReleae(){
  delay(deleyRICHOD);
  while( digitalRead(buttonPin) == LOW){
  }//while

    delay(deleyRICHOD);
}

void countDown(){
  digitalWrite(buz, LOW);

  four();
  delay(delayCountDown);
  three();
  delay(delayCountDown);
  two();
  delay(delayCountDown);
  one();
  delay(delayCountDown);
   zero();
  delay(delayCountDown);
}//countDown

void resetBoard(){
  digitalWrite(buz, LOW);  
  flagReset = 0;
  counter = 0;
  countDown();  
} // resetBoard

You need to describe your circuit.
You're not using the built-in pullups, but do you have external ones?

How does the circuit and sketch behave, and how does that differ from what you expect?

my expectations are:
there are 4 light, there will be a count down (working)

when I press the first button, the leds will lite up one by one, until all 4 are up. then there will be a victory flash (working)

the games starts a again (working)

during any part of the game, I would like to press the second button and reset the game. currently I have changed it to just countdown so that I could sea some reaction. - this does not work.

toy_2.pdf (23.7 KB)

Try:

void setup() {

  // initialize the LED pin as an output:
  pinMode(buttonPin, INPUT);     

  pinMode(led1, OUTPUT);     
  pinMode(led2, OUTPUT);     
  pinMode(led3, OUTPUT);     
  pinMode(led4, OUTPUT);     

  pinMode(buz, OUTPUT);     

  pinMode(res, INPUT);     
  digitalWrite (res, HIGH);
  digitalWrite (buttonPin, HIGH);

}

GRATE!

thanks
seems to be working fine now

All the mod did was to enable the built-in pullup resistors on the switch inputs.
This stops them from floating, and ensures they reliably read HIGH when not pressed.

Good to know.

once move, many thanks to your help.
and to your very fast response and know-how.