debonce return


scope:
i trying use debounce example built ide , modifying return button's state. once debounce button, wrote 2nd piece of code below track button presses incrementing numbers in array.

problem:
however, debounce function keeps returning low though inputs input_pullup high. when debounce function run continually on own, corrects itself, returns low first runs through, throwing false button press exit while loop.

i have gone through example , commented in own words try ease understanding of code. :d !

int debounce(int pin)
{
 
  int reading = digitalread(pin);
  //checs initial state of pin passed function

  // if switch changed reason (noise or actual press)
  if (reading != lastbuttonstate)
    {
    // reset debouncing timer
    lastdebouncetime = millis();
    }
    //when previous if statement false, skips reset
    // means on false, next if loop execute true
    // since lastdebouncetime not updated
 
  if ((millis() - lastdebouncetime) > debouncedelay)
    {
    // whatever reading at, it's been there longer
    // debounce delay, take actual current state:
   
    // if button state has changed back, ending button press
    if (reading != buttonstate)
      {
      buttonstate = reading;
      // sets buttonstate = reading if different, why?
     
      //i want return buttonstate here
      return buttonstate;
      }
    }
  lastbuttonstate = reading;
  //sets last button state equal read, before program executed again
}


-------------------------------------------------------------------------------------------

int setdispensetime()//if of buttons pressed, button increments array
{  //in correct time slot
  while(debounce(12) == low); //allows exit out of setdispensetime loop when while condition false
  {
      if(debounce(10) == low) //pin 10 = hour
      {
       timearray[0] = timearray[0] + 1; // increments day counter in array
       if(timearray[0] = 13)  //makes hour loop @ 24
       {timearray[0]=1;}
      }
      if(debounce(11) == low) //pin 11 = minute
      {
       timearray[1] = timearray[1] + 1;
       if(timearray[1] = 61)
       {timearray[1] = 0;}
      }
     
   
  }
}

code: [select]
.
.
.
return buttonstate;
      }
    }
  lastbuttonstate = reading;

if function has returned, how expect last line?


Arduino Forum > Using Arduino > Programming Questions > debonce return


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code