Problem debouncing multiple buttons

Sorry I thought I had fixed the previous posted code. Here is the same code posted in code format. Sorry about the delayed response. (also attached the file if anyone wants it)

GLCD_first_demo_v8.ino (8.03 KB)

Did anyone look at my code? Or did everyone leave this topic. Sorry for the delayed code format fix.

I'm looking at it but it's hard to see what's happening with all the other code. You should really have a test program that does nothing but debounce the buttons and get that module working, then add it to the LCD stuff.


Rob

The debounce function works, but I think either my pseudo time delay I created or something with the way LCD.printStr is displaying text doesn't work. I can see the LED pin 13 light up when i test the debounce function and it works with no delay.

The LCD.printNum function works fine and displays the numbers correctly and I used printNum and printStr the same just put them in different functions since the text only needs to be put once and stays there until it is cleared or over written.

Could it be since that I'm running through the whole code without delays that the LCD.printStr isn't getting enough time to display its text before it gets called again further down the code? Maybe something to do with the buffer.

I can chop my code up into modular pieces and test different functions if you want me to.

Something in here is backwards. Adding some spaces may help you find the problem.

for(int i=0; i>99; i++){
previousMillis[ i ]=0;
}

HazardsMind:
Something in here is backwards. Adding some spaces may help you find the problem.

for(int i=0; i>99; i++){
previousMillis[ i ]=0;
}

I see i made that backward i changed it to this

  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.

Could you attach a picture of what your seeing on your GLCD?

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

pic 2

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

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.

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

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

There are a few things you should fix. For starters, all of these:

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.

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.

    if(debounce(buttonNumber[i]) ) //debounces input pins, loops through all the pins
    {
      switch(i)

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

Tell me something, why did you do this, when the array buttonNumber has the elements 0 - 7 in it already?

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.

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.

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.

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

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?

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.

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*)':