PDA

View Full Version : Button State controlling other functions of buttons



davidteensy
03-25-2013, 07:46 PM
Hello teensy gang!

I have a keyboard foot controller project that I have been working on, but I hit a problem... I want to have 12 buttons and two leds for each button, and then one other button (with one, or two, leds). The last button I want to act as a 'bank switch', so that it changes the functions of the other buttons.

put in a simpler way:

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

seems simple enough, but so far I have not been able to do it.

I have written stable code for a button to turn on an led and send a keystroke, and then when pressed again turn off the led and send the keystroke again. perfect. but when I try to do the rest as described above -



#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..

let me know what your great minds think... maybe I need a different attack?

thanks

PaulStoffregen
03-25-2013, 08:42 PM
I would recommend using only a single Bounce object per pin.

You probably should use a global scope variable to remember which mode. Something like this.....



int mode=0;

void loop() {
if (button2.fallingEdge()) {
if (mode == 0) {
mode = 1;
} else {
mode = 0;
}
}
if (button3.fallingEdge()) {
if (mode == 0) {
// do whatever button3 does in normal mode
} else {
// do whayever button3 does in the other mode
}
}
}


(also, it helps if you put the code tags in your message, to preserve the formatting)

davidteensy
03-26-2013, 05:23 AM
hmm, well this seems to work up until I try to get the leds to join in... I got the one switch to act as a bank just as you offered, (changing button 3's actions) and the corresponding 'bank' led lights up and turns off as expected, but the button3's corresponding leds won't act appropriately...

here's my code,


#include <Bounce.h>

Bounce bank = Bounce(PIN_B2, 10);
Bounce button3 = Bounce(PIN_B3, 10);

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

int state2 = LOW;
int state3 = LOW;
int state3b = LOW;

int mode = 0;
int reading2;
int reading3;
int reading3b;

int previous2 = HIGH;
int previous3 = 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() {

bank.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 (bank.fallingEdge()) {
if (mode == 0) {
mode = 1; } else {
mode = 0; }
}


button3.update();
reading3 = digitalRead(PIN_B3);
if (mode == 0) {
if (reading3 == HIGH && previous3 == LOW && millis() - time > debounce) {
if (state3 == HIGH)
state3 = LOW;
else
state3 = HIGH;
time = millis();
}
digitalWrite(PIN_B4, state3);
Keyboard.write ('a');
previous3 = reading3;}

else {
if (reading3b == HIGH && previous3b == LOW && millis() - time > debounce) {
if (state3b == HIGH)
state3b = LOW;
else
state3b = HIGH;
time = millis();
}

digitalWrite(PIN_B6, state3b);
Keyboard.write ('A');
previous3b = reading3b; }
}

I am thinking I just need to move some things around to get it... right now this code sends an unrelenting cascade of the letter 'a'....

Do I still need the bounce feature? or can I use fallingEdge as the sole button state detector?

thanks for the step towards finalisation!! getting so close!

davidteensy
03-26-2013, 05:26 AM
or couldn't I just use the state of 'bank' to act like the mode -

if (state2 == HIGH) {
//do whatever button3 does in normal mode
} else {
//do whatever button3 does in altered mode
}

?

davidteensy
04-04-2013, 04:44 AM
Well, I feel like I have tried this in numerous ways, and Paul's thought to use the 'mode' variable scope tack seems to be the best route, but I am still not getting the results I seek.

I have tried adapting the 'working' code from the top here to use along with the mode variable. But the problem for me seems to lie in the state of the switches, and their corrosponding leds, when the differing modes come into play. Not stated earlier that may prove crucial to the problem - I am using momentary switches - but I don't see why I need to buy different switches as these should be usable (considering the code at the top works fine for the basics of this project..).

my best shot at it thus far-



#include <Bounce.h>

Bounce bank = Bounce(PIN_B2, 10);
Bounce button3 = Bounce(PIN_B3, 10);

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

int state2 = LOW;
int state3 = LOW;
int state3b = LOW;

int mode = 0;
int reading2;
int reading3;
int reading3b;

int previous2 = HIGH;
int previous3 = 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() {

bank.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 (bank.fallingEdge()) {
if (mode == 0) {
mode = 1; } else {
mode = 0; }
}

if (mode == 0)
{
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_B4, state3);
Keyboard.write ('a');
previous3 = reading3;
}

else {
if (reading3b == HIGH && previous3b == LOW && millis() - time > debounce)
{
if (state3b == HIGH)
state3b = LOW;
else
state3b = HIGH;
time = millis();
}
digitalWrite(PIN_B6, state3b);
Keyboard.write ('A');
previous3b = reading3b;
}
}


right now all I get is a cascade of the letter 'a' when I reboot this code, and I don't see why.

any guesses out there?

PaulStoffregen
04-04-2013, 08:57 AM
I'd recommend using the Bounce library to read the buttons. Just use bank.fallingEdge() instead of all this complex stuff with digitalRead() and millis(). The Bounce library does all that for you.

Also, the indenting of this code is a mess. It makes reading quite difficult.

Please try to clean these up. If it still doesn't work, just post again and I'll give it a try here and see if I can figure out where its going wrong.

Nantonos
04-04-2013, 09:28 PM
Also, the indenting of this code is a mess. It makes reading quite difficult.


In trhe Arduino editor, menu item Tools > Auto Format

davidteensy
04-07-2013, 08:15 AM
int bank = PIN_B2;
int button3 = PIN_B3;

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

int mode = 0;

boolean reading2 = false;
boolean reading3 = false;
boolean reading3b = false;

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() {
if (digitalRead(bank)== LOW) {
delay(300);
reading2 = !reading2;
digitalWrite(ledPin4, reading2);
if (mode == 0) {
mode = 1;
}
else {
mode = 0;
}
}

if (digitalRead(button3)==LOW) {
if (mode == 0) {
delay(300);
Keyboard.print('a');
reading3 = !reading3;
digitalWrite(ledPin5, reading3);
}
else {
delay(300);
Keyboard.print('A');
reading3b = !reading3b;
digitalWrite(ledPin6, reading3b);
}
}
}


well that feels good