Great hack! if you use only one digital input you can use it for interruption, this could be done by adding an op amp in comparator configuration, comparing Vout: the output of your circuit with Vo: the least voltage that can be provide when pressing a button, if Vout >= Vo the op amp gives us 5v this is the signal that triggers the interrupt routine which is just reading Vout i.e knowing which button was pressed.
//The code: // These constants won't change. They're used to give names to the pins used: const int analogInPin = A5; const int outPin = 13; // Analog output pin that the LED is attached to int sensorValue = 0; // value read from the buttons int state = 0; // used for storing what button was pressed void setup() { Serial.begin(9600); // initialize serial communications at 9600 bps pinMode(outPin, OUTPUT); // set pin 13 as an output } void loop() { // print the results to the Serial Monitor: readSwitch(); Serial.print("Value = "); Serial.println(sensorValue); Serial.print("State = "); Serial.println(state); if (state == 0) { //turns the LED off if a certain button is pressed digitalWrite(outPin, LOW); readSwitch(); //contuines to poll the switch circuitry } if (state == 1) { //runs a program similar to the blink schetch if a certain button is pressed for (int i = 0; i < 2000; i++) { //set a for loop that counts up to 2000 if (i < 1000) { //if the for loop value is less than 1000 turn on the LED digitalWrite(outPin, HIGH); readSwitch(); //contuines to poll the switch circuitry also provides about a 1 ms delay } if (i > 1000) { digitalWrite(outPin, LOW); //if the for loop value is more than 1000 turn off the LED readSwitch(); //contuines to poll the switch circuitry also provides about a 1 ms delay } } } if (state == 2) { //creates the 2 flash sequence of the LED and polls the switches between delays digitalWrite(outPin, HIGH); delay(100); readSwitch(); digitalWrite(outPin, LOW); delay(100); readSwitch(); digitalWrite(outPin, HIGH); delay(100); readSwitch(); digitalWrite(outPin, LOW); delay(250); readSwitch(); } if (state == 3) { //Creates the fast blink effect and polls the switches after one cycle of the blink effect digitalWrite(outPin, HIGH); delay(75); digitalWrite(outPin, LOW); delay(75); readSwitch(); } } void readSwitch () { //creates the function to read the switch states sensorValue = analogRead(analogInPin); //read the analog pin the switches are connected to if (sensorValue < 800 && sensorValue > 750) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly state = 0; delay(100); //delay for debounce purposes } if (sensorValue < 749 && sensorValue > 650) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly state = 1; delay(100); //delay for debounce purposes } if (sensorValue < 649 && sensorValue > 425) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly state = 2; delay(100); //delay for debounce purposes } if (sensorValue < 425 && sensorValue >= 0) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly state = 3; delay(100); //delay for debounce purposes } delay(1); //delay for timeing purposes }
Keeps giving the error of "stray '\357' in program" It gives the error for the last line of code. Great video though! (Update) Fixed error but still doesn't work.
Doesn't work. Sorry for commenting so much but after fixing the error code nothing happens. I upload it and once everything is hooked up properly, it stays stuck on the blink command. The serial monitor just says, " state = 3 value = 0 "
Sorry it took so long for me to respond. If you are still trying to make this work I put the code on github. github.com/Eman2000/Arduino-4-Buttons-1-Pin
have you try to make a code that will translate this as a push buttons in game ? how to code it with some joystick/keybord library to make it work as a game kontroler ? Im flying planes in Digital Combat Simulator and a lot of buttons are needed ;)
I have put the code for this project on github.
github.com/Eman2000/Arduino-4-Buttons-1-Pin
Would this work for leds ? (connecting multiple leds to one arduino pin and control them individualy)
Great hack! if you use only one digital input you can use it for interruption, this could be done by adding an op amp in comparator configuration, comparing Vout: the output of your circuit with Vo: the least voltage that can be provide when pressing a button, if Vout >= Vo the op amp gives us 5v this is the signal that triggers the interrupt routine which is just reading Vout i.e knowing which button was pressed.
Hey man thanks i was needing like20 buttons on a project and had 12 inputs thankyou for this information
You can push multiple buttons by choosing a different resistor value for each button and putting them in parallel rather than series
//The code:
// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A5;
const int outPin = 13; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the buttons
int state = 0; // used for storing what button was pressed
void setup() {
Serial.begin(9600); // initialize serial communications at 9600 bps
pinMode(outPin, OUTPUT); // set pin 13 as an output
}
void loop() {
// print the results to the Serial Monitor:
readSwitch();
Serial.print("Value = ");
Serial.println(sensorValue);
Serial.print("State = ");
Serial.println(state);
if (state == 0) { //turns the LED off if a certain button is pressed
digitalWrite(outPin, LOW);
readSwitch(); //contuines to poll the switch circuitry
}
if (state == 1) { //runs a program similar to the blink schetch if a certain button is pressed
for (int i = 0; i < 2000; i++) { //set a for loop that counts up to 2000
if (i < 1000) { //if the for loop value is less than 1000 turn on the LED
digitalWrite(outPin, HIGH);
readSwitch(); //contuines to poll the switch circuitry also provides about a 1 ms delay
}
if (i > 1000) {
digitalWrite(outPin, LOW); //if the for loop value is more than 1000 turn off the LED
readSwitch(); //contuines to poll the switch circuitry also provides about a 1 ms delay
}
}
}
if (state == 2) { //creates the 2 flash sequence of the LED and polls the switches between delays
digitalWrite(outPin, HIGH);
delay(100);
readSwitch();
digitalWrite(outPin, LOW);
delay(100);
readSwitch();
digitalWrite(outPin, HIGH);
delay(100);
readSwitch();
digitalWrite(outPin, LOW);
delay(250);
readSwitch();
}
if (state == 3) { //Creates the fast blink effect and polls the switches after one cycle of the blink effect
digitalWrite(outPin, HIGH);
delay(75);
digitalWrite(outPin, LOW);
delay(75);
readSwitch();
}
}
void readSwitch () { //creates the function to read the switch states
sensorValue = analogRead(analogInPin); //read the analog pin the switches are connected to
if (sensorValue < 800 && sensorValue > 750) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly
state = 0;
delay(100); //delay for debounce purposes
}
if (sensorValue < 749 && sensorValue > 650) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly
state = 1;
delay(100); //delay for debounce purposes
}
if (sensorValue < 649 && sensorValue > 425) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly
state = 2;
delay(100); //delay for debounce purposes
}
if (sensorValue < 425 && sensorValue >= 0) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly
state = 3;
delay(100); //delay for debounce purposes
}
delay(1); //delay for timeing purposes
}
Keeps giving the error of "stray '\357' in program"
It gives the error for the last line of code. Great video though!
(Update) Fixed error but still doesn't work.
Very very very clever.
I wondered if this was possible. wasn't sure how to complete the task. I know nothing about programming!
Its Great toutrial Thank you very much sir.
How to use high digital input as a single pulse in Arduino
Thank you! This video is usefull
How to select the proper resistor for more buttons?
will this work with a relay? i am trying to figure out how i can change the delay with a push button. to make it slower or faster.
Doesn't work. Sorry for commenting so much but after fixing the error code nothing happens. I upload it and once everything is hooked up properly, it stays stuck on the blink command. The serial monitor just says, "
state = 3
value = 0
"
Sorry it took so long for me to respond. If you are still trying to make this work I put the code on github.
github.com/Eman2000/Arduino-4-Buttons-1-Pin
which you used resistance, 10k; 1/4W, 2W, 1W ?
Please send me answer.
Probably way too late, but 1/4 watt works just fine but since its such low current and voltage, basically any wattage works.
very helpful than u 😘😘😘
does this work with 9 buttons as well?
have you try to make a code that will translate this as a push buttons in game ? how to code it with some joystick/keybord library to make it work as a game kontroler ? Im flying planes in Digital Combat Simulator and a lot of buttons are needed ;)
need programming code please
Check the pinned comment please.
@Jens Tiefschneider But it's not