Hi,
This is my first post on this forum as I am new to this whole Arduino universe.
I am trying to build a program that will allow me to use one momentary switch to toggle between two different controls. In this case I am trying to modify a reclining hospital bed remote so that the user (who has very limited hand mobility) has to only use one button to operate HEAD UP/DOWN functions.
The ideal operation would be: Hold button for chair UP, pause, hold button for chair DOWN, repeat.
The first code I have cobbled together (through the help of tutorials) makes one push button a momentary switch, where another push button is made in to a toggle switch. I am looking for advice to help me achieve my goal of only using the one push button for both operations.
The second code is built with only one push button in the build, but the code is a little over my head, it doesn't seem to work and I have no problem admitting that I don't quite understand it.
I am using LEDs to simulate the chair operations.
CODE 1:
//momentary toggle switch attempt 1 Dec. 20/2018
//define integers
int led1 = 4;
int led2 = 5;
int button1 = 8;
int button2 = 9;
boolean button1gate = true; //gate for momentary switch
boolean button2gate = true; //gate for toggle switch
int button2toggle = 0;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(button1, INPUT_PULLUP); //using the internal pullup resistor to initially register HIGH
pinMode(button2, INPUT_PULLUP); //using the internal pullup resistor to initially register HIGH
}
void loop() {
////BUTTON1//// - MOMENTARY SWITCH
//If button1 is on //when button is pushed 'button1:1'
if ( button1gate ) { //shorthand for button1gate == true as button1gate is defined above as true
if ( digitalRead(button1) == LOW ) {
digitalWrite(led1, HIGH);
button1gate = false;
}
}
//If button1 is off //when button is released 'button1:0'
if ( button1gate == false ) { //shorthand !button1gate
if ( digitalRead(button1) == HIGH ) {
digitalWrite(led1, LOW);
button1gate = true;
}
}
////BUTTON2//// - TOGGLE SWITCH
//If button2 is on //when button is pushed 'button2:2'
if ( button2gate ) { //shorthand for button2gate == true as button2gate is defined above as true
if ( digitalRead(button2) == LOW ) {
button2toggle = (button2toggle + 1) % 2; //this increases toggle count from 0 the %2(modulo 2) is t toggle between 2 states(0 and 1)
button2gate = false;
}
}
//If button2 is off //when button is released 'button2:0'
if ( button2gate == false ) { //shorthand !button2gate
if ( digitalRead(button2) == HIGH ) {
button2gate = true;
}
}
//toggle LED2 on and off with button2 button
if(button2toggle == 0) digitalWrite(led2, LOW); //shorthand - if all commands are written on the same line {} are not needed
if(button2toggle == 1) digitalWrite(led2, HIGH);
delay(15);
}
CODE 2:
/*momentary toggle rocker switch idea
* Using state change detection to be able to use one button to raise head or lower head of his bed controls; push, raise, push, lower, repeat
* Determine a debounce time that is suitable, maybe 2 seconds between raise and lower functions
*
*/
int switchPin = 2; // switch is connected to pin 2
int led1Pin = 8;
int led2pin = 9;
int val; // variable for reading the pin status
int val2; // variable for reading the delayed status
int buttonState; // variable to hold the button state
int Mode = 0; // What mode is the light in?
void setup() {
pinMode(switchPin, INPUT_PULLUP); // Set the switch pin as input
pinMode(led1Pin, OUTPUT);
pinMode(led2pin, OUTPUT);
buttonState = digitalRead(switchPin); // read the initial state
}
void loop(){
val = digitalRead(switchPin); // read input value and store it in val
delay(10); // 10 milliseconds is a good amount of time
val2 = digitalRead(switchPin); // read the input again to check for bounces
if (val == val2) { // make sure we got 2 consistant readings!
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (Mode == 0) {
Mode = 1;
} else {
if (Mode == 1) {
Mode = 2;
} else {
if (Mode == 2) {
Mode = 3;
} else {
if (Mode == 3) {
Mode = 0;
}
}
}
}
}
}
buttonState = val; // save the new state in our variable
}
// Now do whatever the lightMode indicates
if (Mode == 0) { // all-off
digitalWrite(led1Pin, LOW);
digitalWrite(led2pin, LOW);
}
if (Mode == 1) {
digitalWrite(led1Pin, HIGH);
digitalWrite(led2pin, LOW);
}
if (Mode == 2) {
digitalWrite(led1Pin, LOW);
digitalWrite(led2pin, HIGH);
}
}
I really appreciate all interest and advice I might receive for this project:)