Problem with digitalRead not returning anticipated result


hi there, i'm working on project mega 2560 use bunch of ten switches , various leds being lit depending on switch pressed. (the project more complicated i've simplified things post. 1 of simplifications i've reduced number of switches , leds 2 each.) various reasons i'm using interrupt recognizing switch pressed , polling switches determine 1 pressed.

the problem i'm having while interrupt works fine when either of switches pressed (they wire-or'd interrupt pin), when (after debouncing) poll switches see 1 one pressed, both return high though switches grounded , isr set activate on low only.  (the inputs on mega set input_pullup). (please see circuit diagram below code.) i've verified isr activates when switch pressed. what want code do (and think should doing) flash appropriate led depending on switch pressed. what happens instead neither led lights up. (i have used other code , verified led circuit works).  debug statements return following when either button pressed:

button 1: 1
button 2: 1


since isr activates on low, should think 1 of buttons have low, neither are.

here simplified code:

code: [select]

volatile const int button1pin = 41;               // number of pushbutton pin
volatile const int button2pin = 40;             // number of pushbutton pin
const int led1pin = 6;                                       // number of led pin
const int led2pin = 5;                                       // number of led pin

// statemachine states
const int nobuttonpressed = 0;                // default state
const int buttonpressedstartdebouncing = 1;
const int buttonpresseddebouncingongoing = 2;
const int buttonpresseddebouncingdone = 3;
const int buttonpressedfading = 4;

volatile int state = nobuttonpressed;// statemachine state

volatile int reading;       // number of switch pressed

long debouncestarttime = 0;  // last time output pin toggled
long debouncedelay = 15;    // debounce time; increase if output flickers

unsigned long currenttime = 0;// store current system clock time


void setup() {
  pinmode(2, input_pullup);  // set int 0 input pullup
  pinmode(button1pin, input_pullup);
  pinmode(button2pin, input_pullup);
  pinmode(led1pin, output);
  pinmode(led2pin, output);

  serial.begin (115200);

  // enable isr
  attachinterrupt(0, buttonpress, low);
}

// isr triggered in case of button press
void buttonpress() {
  state = buttonpressedstartdebouncing;
}

void loop() {
  switch (state) {
    case (buttonpressedstartdebouncing):
    {
      debouncestarttime = millis();
      state = buttonpresseddebouncingongoing;
      break;
    }
    case (buttonpresseddebouncingongoing):
    {
      currenttime = millis();
      if ((currenttime - debouncestarttime) > debouncedelay) {
        reading = digitalread(button1pin);
        serial.print ("button 1: ");
        serial.println (reading);
        reading = digitalread(button2pin);
        serial.print ("button 2: ");
        serial.println (reading);
       
        if (digitalread(button1pin) == low)
        {
          digitalwrite(led1pin, high);
          delay(30);
          digitalwrite(led1pin, low);
          delay(30);
        }
        else if (digitalread(button2pin) == low)
       {
          digitalwrite(led2pin, high);
          delay(30);
          digitalwrite(led2pin, low);
          delay(30);
       }
       
        state = buttonpresseddebouncingdone;
      }
      break;
    }
    default:
    {
      break;
    }
  }
}


below simplified version of circuit showing 2 switches , 2 leds:


any appreciated! thank time.

btw, please ignore use of delay() - quick , dirty debug  - final project fades leds on/off, code works removed post.

quote
[font=courier new]  attachinterrupt(0, buttonpress, low);[/font]


as long either button pressed, interrupt service routine continue fire.  over , on , over.  starving loop of processing time.  try falling.


Arduino Forum > Using Arduino > Programming Questions > Problem with digitalRead not returning anticipated result


arduino

Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support