Go Down

Topic: Button State controlling other funcitions of buttons (Read 648 times) previous topic - next topic


OK, sorry for the delay... it would be nice if one could save notes here, and post at a later time...

I have tried other avenues to store the state of the button, and have found bounce to be the most reliable and stable.

anywho, my above code runs like I explained earlier, the shift is held but does not affect the outcome of the other button presses - fine.  so to change tack I tried out a way to use the 'state' of one button (my bank button) to affect the outcome of the other buttons.  in other words: if state of bankbutton is HIGH; light led saying so, and if 'a'button is pressed, switch led-a to high and type letter 'a', and store state of 'a'button for further changes - else if state of bankbutton is LOW; turn off led saying so, and if 'a'button is pressed, switch led-A to high (or low depending on past state) and type letter 'A', store state...

that makes sense right?

here is the pudding -

#include <Bounce.h>
Bounce button2 = Bounce(PIN_B2, 10);
Bounce button3a = Bounce(PIN_B3, 10);
Bounce button3b = Bounce(PIN_B3, 10);

int ledPin4 = PIN_B4;
int ledPin5 = PIN_B5;
int ledPin6 = PIN_B6;

int state2 = LOW;     
int state3a = LOW;
int state3b = LOW;
int reading2;
int reading3a;
int reading3b;         
int previous2 = HIGH;   
int previous3a = HIGH;
int previous3b = HIGH;
long time = 0;       
long debounce = 200; 
void setup() {
pinMode(PIN_B4, OUTPUT);
pinMode(PIN_B5, OUTPUT);
pinMode(PIN_B6, OUTPUT);   }
void loop() {
reading2 = digitalRead(PIN_B2);
  if (reading2 == HIGH && previous2 == LOW && millis() - time > debounce)
{ if (state2 == HIGH) state2 = LOW;
    state2 = HIGH;
    time = millis();    }
  digitalWrite(PIN_B5, state2);
  previous2 = reading2;
if (state2 == LOW)
reading3a = digitalRead(PIN_B3);
  if (reading3a == HIGH && previous3a == LOW && millis() - time > debounce)
{  if (state3a == HIGH) state3a = LOW;
   else state3a = HIGH;
   time = millis();   }
digitalWrite(PIN_B4, state3a);
previous3a = reading3a;

if (button3a.fallingEdge())
{ Keyboard.print("b");
else (state2==HIGH)
reading3b = digitalRead(PIN_B3);
  if (reading3b == HIGH && previous3b == LOW && millis() - time > debounce)
{  if (state3b == HIGH) state3b = LOW;
   else state3b = HIGH;
   time = millis();   }
digitalWrite(PIN_B6, state3b);
previous3b = reading3b;

if (button3b.fallingEdge())
{ Keyboard.print("B");

but this doesn't work! currently the 'bank' switch doesn't work stabley, and the key switch seems to spit both of the letters out, or one, and turns on both, one, or none of the leds...seems like I am catching the code in parts of the loop..

any help with this attack, or any other clear solutions, would be much appreciated. I certainly feel there can be a much more elegant code to this simple bit of hardware.


so, I had read someone's post here who asked about buttons and they had placed it in the 'sensor' section, but let me know if this is the wrong spot.. I say that in part b/c it seems like nobody has been able to offer up a better avenue for what seems to be a very simple sketch.  (I mean compared to all the complex stuff folks are up to here, my simple button scheme seems like peanuts )

any ideas or help for what I am up to would be great. and I am patient, and will wait and tinker for the best solution, but just a little frustrated I haven't been able to complete this seemingly simple task.

again -

have two buttons and three leds.

if buttonbank has been pressed (turn on led2)
press button1 and send keystroke 'A' and turn on led1
press button1 again to send keystroke 'A' and turn off led1

else buttonbank has not been pressed (led2 off)
press button1 again, send keystroke 'a' and turn on led3
press again to send keystroke 'a' and turn off led3

thanks for your time

Go Up