Pages: [1]   Go Down
Author Topic: Button State controlling other funcitions of buttons  (Read 577 times)
0 Members and 1 Guest are viewing this topic.
Olympia, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello y'all.

I have done some searching for the answer to my problem, and have yet to find a suitable answer.

I want to create a foot pedal that basically acts as a keyboard with my computer.
I have found numerous examples that demonstrate that, but I have found most of the designs lacking something.  long story short I want leds to display the status of buttons that have been pressed, and be able to have one button act as a 'shift key' and make it so the buttons are able to be used twice.
does that make sense?

and so far I have been able to write the code that reads my button press, sends a keystroke, and turns on a led. when pressed again led turns off, and the keystroke is sent again. perfect.

but my problem lies in the 'shift key', or 'bank', switch. I have tried making the keystroke on one of the buttons 'hold shift', and it works, but the program is clever enough to keep the state of the other keystrokes their original size.

more to come - I can't save to post here later?
Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you upload the sketch (between code tags).

Do you use the press and release functions ? or just the write function ?

You could send a SHIFT to the PC, or you can make some kind of 'shift'-alike code in the Arduino and change the code sent for the other buttons.
Logged

Olympia, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yeah here is the code

#include <Bounce.h>

Bounce button2 = Bounce(PIN_B2, 10);
Bounce button3 = Bounce(PIN_B3, 10);
int led2Pin = PIN_B4;
int led3Pin = PIN_B5;

int state2 = HIGH;     
int reading2;         
int previous2 = LOW;   
int state3 = HIGH;     
int reading3;         
int previous3 = LOW;   
long time = 0;       
long debounce = 200; 

void setup() {
 
  pinMode(PIN_B2, INPUT_PULLUP);
pinMode(PIN_B3, INPUT_PULLUP);
 pinMode(PIN_B4, OUTPUT);
pinMode(PIN_B5, OUTPUT);
}
void loop() {
 
  button2.update();
 reading2 = digitalRead(PIN_B2);
  if (reading2 == HIGH && previous2 == LOW && millis() - time > debounce) {
    if (state2 == HIGH)
      state2 = LOW;
    else
    state2 = HIGH;
time = millis();   
  }
digitalWrite(PIN_B4, state2);
 previous2 = reading2;

if (button2.fallingEdge()) {
    Keyboard.set_modifier(MODIFIERKEY_SHIFT);
Keyboard.send_now();
    ledPin2 = HIGH;
}

  button3.update();
 reading3 = digitalRead(PIN_B3);
  if (reading3 == HIGH && previous3 == LOW && millis() - time > debounce) {
    if (state3 == HIGH)
      state3 = LOW;
    else
    state3 = HIGH;
time = millis();   
  }
digitalWrite(PIN_B5, state3);
 previous3 = reading3;

if (button3.fallingEdge()) {
    Keyboard.println("x");
    ledPin3 = HIGH;
}

}
gotta go now, but this doesn't reveal the extent that I have tried.
but any help y'all can offer in the meantime (at work now, can get more into it afterwards)

thanks!
Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's not Arduino Leonardo code.
Are you using a Teensy ? Are the keyboard functions with a library of the Teensy ?
Logged

Olympia, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes, I have a teensy, and I meant to mention that. good deduction
Logged

Olympia, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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_B2, INPUT_PULLUP);
pinMode(PIN_B3, INPUT_PULLUP);
pinMode(PIN_B4, OUTPUT);
pinMode(PIN_B5, OUTPUT);
pinMode(PIN_B6, OUTPUT);   }
void loop() {
 
button2.update();
reading2 = digitalRead(PIN_B2);
  if (reading2 == HIGH && previous2 == LOW && millis() - time > debounce)
{ if (state2 == HIGH) state2 = LOW;
    else
    state2 = HIGH;
    time = millis();    }
  digitalWrite(PIN_B5, state2);
  previous2 = reading2;
if (state2 == LOW)
{
button3a.update();
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)
{
button3b.update();
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.
Logged

Olympia, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: