variable toggle with button problem

Okay, well I guess I need some help with this code. I am just getting started with buttons, but I managed to get the input to toggle the on-board LED just fine. But I also want a boolean to toggle from true to false or false to true each time the button is pressed. I got it to kind of work, but it does not work correctly. Most of the time I have to press the normally open switch I have several times to get it to toggle the variable and turn on the appropriate LED. Sometimes for a few presses it works fine, but then it reverts back to having to press the button several times again. I was wondering if someone could look at the code below and tell me what I am doing wrong.

:~

/*

 * Debounce sketch

 * a switch connected to pin 2 lights the LED on pin 13

 * debounce logic prevents misreading of the switch state

 */


const int inputPin = 10;        // the number of the input pin

const int ledPin = 13;         // the number of the output pin

const int debounceDelay = 10;  // milliseconds to wait until stable

boolean dir = true;
boolean newdir = false;

String sMessage = "Hello, dir = ";

// debounce returns true if the switch in the given pin is closed and stable

boolean debounce(int pin)

{

  boolean state;

  boolean previousState;

  previousState = digitalRead(pin);          // store switch state

  for(int counter=0; counter < debounceDelay; counter++)

  {

      delay(1);                  // wait for 1 millisecond

      state = digitalRead(pin);  // read the pin

      if( state != previousState)

      {

         counter = 0; // reset the counter if the state changes

         previousState = state;  // and save the current state
         
         

      }
      
          
//       if(dir == true)
//         do{dir = false;}while(dir == true);
//       else
//         do{dir = true;}while(dir == false);

  }

  // here when the switch state has been stable longer than the debounce period

  return state;

}


void setup()

{
  Serial.begin(9600);
  pinMode(inputPin, INPUT);

  pinMode(ledPin, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
}


void loop()

{

  if (debounce(inputPin))
  {

    digitalWrite(ledPin, HIGH);
    delay(5);
    
    
    newdir = dir;
        
    dir = !dir;
    delay(1000);
    
    if(dir == newdir)
      !dir;
    
//    do
//    {
//       dir = !dir;
//   
//    }while(newdir == dir);


//      if(dir == true)
//        dir = false;
//      else
//        dir = true;
       
      
  }
  else 
  { 
    digitalWrite(ledPin, LOW);
    delay(5);
       
    
  }
    
    
    
  if(dir == false)
  {
    Serial.println("hello, dir = false");
    delay(1000);

    digitalWrite(11, HIGH);
    digitalWrite(12, LOW);
    
    
  }
  else
  {
    Serial.println("hello, dir = true");
    delay(1000);
    
    digitalWrite(12, HIGH);
    digitalWrite(11, LOW);
  }
  
  
  
}

I think you're having some problem with the debounce.

By the way, don't you get a warning or fault saying something like: previousState is used uninitialized. ??

The first time you compare it, it has no value so that may cause some issues.

Its amazing just how complex you can get just making a push button behave properly.

Here is a nice library for debouncing, that makes life a bit easier.

http://www.arduino.cc/playground/Code/Bounce

I also have a discussion of various techniques for this in my new book http://www.arduinobook.com

Si.

There are two big delay in your code (1 sec each) when arduino is NOT reading state of the button, so you have to keep it pressed for ~2 second. Rewrite your code w/o delay functions or decrease them to bare minimum. Better way is implement debounce inside interrupt routine, look for attachInterrupt application.

Magician: There are two big delay in your code (1 sec each) when arduino is NOT reading state of the button, so you have to keep it pressed for ~2 second. Rewrite your code w/o delay functions or decrease them to bare minimum. Better way is implement debounce inside interrupt routine, look for attachInterrupt application.

Thanks, but I had already taken those out and it did not effect anything. I had been wondering about using interrupts. I will look into that. I had really thought it was the debounce really as code as the LED on the board works fine, but who knows maybe it is.

bubulindo: I think you're having some problem with the debounce.

By the way, don't you get a warning or fault saying something like: previousState is used uninitialized. ??

The first time you compare it, it has no value so that may cause some issues.

I set those variables to be initialized to false and I get the same result. No, I did not get any warning messages.

Si: Its amazing just how complex you can get just making a push button behave properly.

Here is a nice library for debouncing, that makes life a bit easier.

http://www.arduino.cc/playground/Code/Bounce

I also have a discussion of various techniques for this in my new book http://www.arduinobook.com

Si.

Thanks I will look at those two resources. I don't quite understand why the LED on the board works just fine and the ones on my breadboard do not. It may be how I am trying to use them I guess. Again, thanks for those resources.

Si: Its amazing just how complex you can get just making a push button behave properly.

Here is a nice library for debouncing, that makes life a bit easier.

http://www.arduino.cc/playground/Code/Bounce

I also have a discussion of various techniques for this in my new book http://www.arduinobook.com

Si.

Well, I tried that library, but it did not help. It does the same thing as the previous code. So, this tells me it is not the debounce code, but somehow the variable state is not being changed though I did write it out using Serial.println() and I could see it changing though everything goes by so fast in the serial monitor that it is hard to tell. At this point I guess I will have to rethink how I am toggling the variable. Maybe I will have to use a second button and control each LED separately though I did not want to have to do that or perhaps use an actual spdt toggle switch rather than a NO push button.

Z.K.: I don't quite understand why the LED on the board works just fine and the ones on my breadboard do not.

This is the most useful piece of information you provided.

If you try your original code on the Arduino LED, does it work properly? If it does and the LEDs connected outside the board don't, can you show us how you're connecting everything?