Go Down

Topic: Problem debouncing multiple buttons (Read 13112 times) previous topic - next topic

HazardsMind

Something in here is backwards. Adding some spaces may help you find the problem.
Quote
for(int i=0; i>99; i++){
    previousMillis[ i ]=0;
  }
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Yigiter007


Something in here is backwards. Adding some spaces may help you find the problem.
Quote
for(int i=0; i>99; i++){
    previousMillis[ i ]=0;
  }



I see i made that backward i changed it to this

Code: [Select]
  for(int i = 0 ; i < 99; i++)
        {
          previousMillis[i]=0;
        }


The purpose of that line was to fill up the array with 0 so that there is an initial value for the debounce function.

I changed it and tried uploading it to my board, but it still does the same thing.

What the GLCD does is that it displays the numbers i want then it displays the same number all around the first ones some even overlapping other numbers. The initial numbers will change so its keeping track and displaying the numbers from the array. After a while the GLCD stops updating certain numbers and doesn't display any new numbers, but the debounce light still works when i press the buttons.  Also the last two buttons cause the GLCD to display a number that increases continuously without a button push.

HazardsMind

Could you attach a picture of what your seeing on your GLCD?
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Yigiter007

Here is a series of pictures that are order in chronological order left to right and what action is being done.

Yigiter007


Yigiter007

Download them and open them on your desktop, the forum opens them inside the reply box in its full size and cuts it off.

Grumpy_Mike


Download them and open them on your desktop, the forum opens them inside the reply box in its full size and cuts it off.

Shan't.
If you read the "how to use this forum" sticky post it tells you to resize them before posting.

Yigiter007

Sorry about that, I missed that line in the forum guide. Here are the images

HazardsMind

#38
Mar 13, 2014, 11:06 pm Last Edit: Mar 13, 2014, 11:24 pm by HazardsMind Reason: 1
There are a few things you should fix. For starters, all of these:
Quote
actionstate=timecheck(125, 0);  
   if(actionstate==1)
You can condense these to just if( timecheck(125, 0) )  

Another thing is your debounce function should return something. You can change this to have a boolean variable and return the state  of that variable.
Quote
int debounce(int buttonNumber) //recieves a pin and debounces that pin
{
  static boolean ButtonOut = false;

 reading[buttonNumber] = digitalRead(buttonPin[buttonNumber]);
 if (reading[buttonNumber] != lastButtonState[buttonNumber])
 {
   lastDebounceTime[buttonNumber] = millis();
 }

 if ((millis() - lastDebounceTime[buttonNumber]) > debounceDelay[buttonNumber])
 {
   if (reading[buttonNumber] != buttonState[buttonNumber])
   {
     buttonState[buttonNumber] = reading[buttonNumber];

     if (buttonState[buttonNumber] == LOW)
     {
       ledState = !ledState;
       //buttonlook[buttonNumber]=1;        //changes variable to 1 to be later tested if true
       ButtonOut = true;   // if at any time one of the buttons is LOW, it will set the variable TRUE
       

     }
   }
 }

 // set the LED:
 digitalWrite(ledPin13, ledState);// lights the LED so you know button was pressed

 // save the reading.  Next time through the loop,
 // it'll be the lastButtonState:
 lastButtonState[buttonNumber] = reading[buttonNumber];

return ButtonOut; // will return false if no button is LOW and will return true if one of them is
}


Now once that is changed, you can do this as well.
Code: [Select]

   if(debounce(buttonNumber[i]) ) //debounces input pins, loops through all the pins
   {
     switch(i)
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Yigiter007

So I changed the code to the way you stated HazardsMind.

Changing the calls to  if( timecheck(125, 0) )  worked fine.

But when i changed debounce to return a value like you posted. The display on the GLCD starts showing numbers all over that are rapidly increase. Similar to the very last picture picture i sent

LED pin 13 lights up on the all the button pushs

HazardsMind

Tell me something, why did you do this, when the array buttonNumber has the elements 0 - 7 in it already?
Quote
for(int i=0; i<8; i++)
{

cleardebounce(); //clears debounces variables
debounce(buttonNumber[ i ]); //debounces input pins
if(buttonlook==1) //loops through all the pins


As for the LCD, its possible it has something to do with all the LCD.setX();  and LCD.setY(); Check them again and see if your letter spacing is correct.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Yigiter007

I'm not sure what you are asking. The array buttonNumber[ i ] has the numbers 0-7 in it and never changes. The loop cycles through 0-7.

Code: [Select]
for(int i=0; i<8; i++) //loops through 0 - 7
{
cleardebounce(); //clears debounces variables
debounce(buttonNumber[ i ]); //debounces input pin i and saves that value into buttonlook[buttonNumber] / buttonlook[i]
if(buttonlook==1) //if that button was pressed it does the corresponding action in the switch case 


The LCD.setX();  and LCD.setY() are the same commands I've been using. When I had the delays in my program they worked and displayed text, I then changed all the delays in the code and left the LCD.setx, LCDsety, LCD.printStr, and LCD.printNum the same. That's when the commands stopped working. If i put a delay(50) after the LCDprintStr it works.

HazardsMind

I dont have your LCD, so I can't do any tests to see why its not working.

Quote
I'm not sure what you are asking. The array buttonNumber[ i ] has the numbers 0-7 in it and never changes. The loop cycles through 0-7.

What I'm asking is why use an array with 0-7 in it, when you can just use the FOR loop that already cycles through 0-7. There is no difference of the 0-7 in the array to the 0-7 in the FOR loop, so why add in something that is not needed?
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Yigiter007

I see what you're asking. I setup the array because I have numerous buttons and I want to separate some of them by function. Like navigation buttons and action buttons. This way I can reference them by the array and not by pins. And in the future If add more buttons I can just make the array larger. Also the array might be later setup to not be in chronological order if I have to switch pins around or add a pin that's not in numerical order.

Yigiter007

So I talked with sparkfun tech support and they said I need to remove software serial from code. Problem is I don't know how to do that. The demo program library for the GLCD uses software serial and If I just comment out softwareserial it says it has errors in the regular SerialGraphicLCD library. So i think softwareserial and SerialGraphicLCD library are intertwined and need each other.

Here are a few of the lines of the errors

error: 'SoftwareSerial' does not name a type
In constructor 'LCD::LCD()':

error: 'serial' was not declared in this scope
In member function 'void LCD::printStr(char*)':

Go Up