Help programming buttonstatechange


i struggling find way use button state change control multiple modes on led.

i want able have 3 different states on led.

solid (led on)
flashing
dimming

so far have been able program 1 led alter between on , off using code, hoping advice or changes make add stages.

const int  buttonpin = 2;    // pin pushbutton attached to
const int ledpin = 3;       // pin led attached to

// variables change:
int buttonpushcounter = 0;   // counter number of button presses
int buttonstate = 0;         // current state of button
int lastbuttonstate = 0;     // previous state of button

void setup() {
  // initialize button pin input:
  pinmode(buttonpin, input);
  // initialize led output:
  pinmode(ledpin, output);
  // initialize serial communication:
  serial.begin(9600);
}


void loop() {
  // read pushbutton input pin:
  buttonstate = digitalread(buttonpin);

  // compare buttonstate previous state
  if (buttonstate != lastbuttonstate) {
    // if state has changed, increment counter
    if (buttonstate == high) {
      // if current state high button
      // wend off on:
      buttonpushcounter++;
      serial.println("on");
      serial.print("number of button pushes:  ");
      serial.println(buttonpushcounter);
    }
    else {
      // if current state low button
      // wend on off:
      serial.println("off");
    }
  }
  // save current state last state,
  //for next time through loop
  lastbuttonstate = buttonstate;

 
  // turns on led everytime button pushes
  // checking modulo of button push counter.
  // modulo function gives remainder of
  // division of 2 numbers:
  if (buttonpushcounter %  1== 0) {
    digitalwrite(ledpin, high);
  } else {
   digitalwrite(ledpin, low);
  }
}
   
thanks in andvance help.

start with
code: [select]

#define led_solid 0
#define led_flash 1
#define led_dimming 2

int led_state = led_solid ;


and use button change update state.  use state
to determine how led driven.

i think have 4 states, since dimming implies led eventually
reaches led_off...


Arduino Forum > Using Arduino > Programming Questions > Help programming buttonstatechange


arduino

Comments

Popular posts from this blog

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums

Multithumb configuration params not working? - Joomla! Forum - community, help and support