Hello, I have a board Dfrobot Romeo V2. I wanted to use the buttons mounted on it. In the instructions on the manufacturer's website there is an example of their use - Under the section Example use of Button S1-S5.
I do not fully understand this code. I mounted the LED on pin 13 and this program works. The Led flashes.
I want to modify it to perform actions after pressing a specific button. I modified it a bit and it looks like this:
char msgs[5][15] = {
"Right Key OK ",
"Up Key OK ",
"Down Key OK ",
"Left Key OK ",
"Select Key OK" };
char start_msg[15] = {
"Start loop "};
int adc_key_val[5] ={
30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
void setup() {
pinMode(13, OUTPUT); //we'll use the debug LED to output a heartbeat
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
Serial.begin(9600);
/* Print that we made it here */
Serial.println(start_msg);
}
void loop()
{
adc_key_in = analogRead(0); // read the value from the sensor
digitalWrite(13, HIGH);
/* get the key */
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) { // if keypress is detected
delay(50); // wait for debounce time
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) {
oldkey = key;
if (key >=0){
Serial.println(adc_key_in);
Serial.println(msgs[key]);
if(adc_key_in==0){
digitalWrite(11,LOW);
digitalWrite(12,HIGH);
}
else if(adc_key_in==143){
digitalWrite(12,LOW);
digitalWrite(11,HIGH);
}
}
}
}
digitalWrite(13, LOW);
}
// Convert ADC value to key number
int get_key(unsigned int input)
{
int k;
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val[k])
{
return k;
}
}
if (k >= NUM_KEYS)
k = -1; // No valid key pressed
return k;
}
I added only two "IF" instructions that let me start the LEDs.
Strictly speaking, I want to slim down this program so that it would not be so extensive. There must be a simpler way to refer to a specific button.
Thanks for the help in advance.
the 5 buttons, when pressed change a resistance that you identify by reading the voltage on A0. each button generates a different resistance, so the voltage you see helps decide which button was pressed.
That's what the get_key() function does. it checks in which interval the reading of A0 is.
try with this (set your console at 115200 bauds)
const char* msgs[] = {
"Right Key",
"Up Key",
"Down Key",
"Left Key",
"Select Key"
};
// Convert ADC value to key number
const int adc_key_val[] = {30, 150, 360, 535, 760};
const byte NUM_KEYS = 5;
int8_t oldkey = -1;
int8_t get_key()
{
int input = analogRead(A0);
for (int8_t k = 0; k < NUM_KEYS; k++)
if (input < adc_key_val[k])
return k;
return -1; // no key
}
void setup() {
Serial.begin(115200);
Serial.println(F("Starting loop "));
}
void loop()
{
int8_t key = get_key(); // convert into key press
if ((key != -1) && (key != oldkey)) { // if keypress is detected
Serial.println(msgs[key]);
delay(50); // wait for debounce time (poor's man debounce :-) )
oldkey = key;
} else if (key == -1) oldkey = -1; // released
}
Thank you for commenting.
i've read your comment and it did really help me finding out what button really is. Turns out I need to further explore this subject.
I uploaded your code to the board, set bandwidth to 115200. When I press one button the console shows "Right Key Up Key" and sometimes it doesn't show anything;(
When I upload my code and run it and press one button at a time it shows this:
0
Right Key OK
143
Up Key OK
327
Down Key OK
503
Left Key OK
741
Select Key OK
first of all to help you understand. its very common for multiple buttons to be run through a combination of resistors to save on pins.
if you send the same voltage to a bunch of buttons with different resistances, when pushed then you can use one pin to receive them all and figure which button was pushed by measuring the voltage.
it looks like the bottom function that you took out calculates that for you.
the only unessasary part of the code i see is the silly two dimentional array at the top they use for labels in the serial print function. you should be able to take it out.
char msgs[5][15] = {
"Right Key OK ",
"Up Key OK ",
"Down Key OK ",
"Left Key OK ",
"Select Key OK" };
char start_msg[15] = {
"Start loop "};
int adc_key_val[5] ={
30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
void setup() {
pinMode(13, OUTPUT); //we'll use the debug LED to output a heartbeat
Serial.begin(9600);
/* Print that we made it here */
Serial.println(start_msg);
}
void loop()
{
adc_key_in = analogRead(0); // read the value from the sensor
digitalWrite(13, HIGH);
/* get the key */
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) { // if keypress is detected
delay(50); // wait for debounce time
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) {
oldkey = key;
if (key >=0){
// here is where you know that a button is pushed
// should be a unique number for each button
Serial.println((String)key);
// do stuff here
//if(key == somenumber){ do this}
//if(key == someothernumber){ do that}
}
}
}
digitalWrite(13, LOW);
}
// Convert ADC value to key number
int get_key(unsigned int input)
{
int k;
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val[k])
{
return k;
}
}
if (k >= NUM_KEYS)
k = -1; // No valid key pressed
return k;
}
Thank you @taterking, all of you for help. You helped me understand more about buttons.
Here's the final code that's working for me:
char msgs[5][15] = {
"Right Key OK ",
"Up Key OK ",
"Down Key OK ",
"Left Key OK ",
"Select Key OK" };
char start_msg[15] = {
"Start loop "};
int adc_key_val[5] ={
30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
void setup() {
pinMode(13, OUTPUT); //we'll use the debug LED to output a heartbeat
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
Serial.begin(9600);
/* Print that we made it here */
Serial.println(start_msg);
}
void loop()
{
adc_key_in = analogRead(0); // read the value from the sensor
digitalWrite(13, HIGH);
/* get the key */
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) { // if keypress is detected
delay(50); // wait for debounce time
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) {
oldkey = key;
if (key >=0){
// here is where you know that a button is pushed
// should be a unique number for each button
Serial.println((String)key);
// do stuff here
if(key == 0){ digitalWrite(11,HIGH);digitalWrite(12,LOW);}
if(key == 1){ digitalWrite(12,HIGH);digitalWrite(11,LOW);}
}
}
}
digitalWrite(13, LOW);
}
// Convert ADC value to key number
int get_key(unsigned int input)
{
int k;
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val[k])
{
return k;
}
}
if (k >= NUM_KEYS)
k = -1; // No valid key pressed
return k;
}