Pad5buttons
---. date: 15/10/14
Pad 5 boutons¶

Objectif¶
Lire sur le port séries les valeurs de chaque bouton actionné.
Liste de matériel¶
- Arduino
- Pad 5 buttons
Schéma¶
| Pad | Arduino |
|---|---|
| OUT | A0 |
| VCC | 3.3V |
| GND | GND |
Code¶
/*
5 button Keypad Tester
*/
// Global Variables
const byte NONE = 0;
const byte LEFT = 1;
const byte UP = 2;
const byte RIGHT = 3;
const byte DOWN = 4;
const byte SELECT = 5;
const byte keypadPin = A0;
byte key = 0;
void setup()
{
Serial.begin(9600);
while (!Serial) { ; }
Serial.println(F("Start\n"));
pinMode(keypadPin, INPUT_PULLUP); // sets analog pin for input
}
void loop()
{
key = getKey();
if (key==LEFT) { Serial.println("LEFT"); }
if (key==RIGHT) { Serial.println("RIGHT"); }
if (key==UP) { Serial.println("UP"); }
if (key==DOWN) { Serial.println("DOWN"); }
if (key==SELECT) { Serial.println("SELECT"); }
if (key==NONE) { Serial.println("NO KEY PRESSED"); }
delay(100);
}
/*
Key Values
Left / First button = 15
Up / Second button = 72
Down / Third = 124
Right / Forth = 170
Select / Fifth = 212
No button pressed = 1021
Because the values change depending on the actual components and even the temperature we need to test for a range of values.
I tend to use +/- 20. This gives:
Left - 0 to 35
Up - 52 to 92
Down - 104 to 144
Right - 150 to 190
Select - 192 to 232
*/
byte getKey()
{
int val = 0;
byte button = 0;
val = analogRead(keypadPin);
// Serial.println(val); // Use to calibration, print value
button = NONE; // use NONE as the default value
if ( val <= 35) { button = LEFT; } // left
else if ( (val >= 150) && (val <=160) ) { button = UP; } // up
else if ( (val >= 340) && (val <= 350 ) ) { button = DOWN; } // down
else if ( (val >= 520) && (val <= 525) ) { button = RIGHT; } // right
else if ( (val >= 750) && ( val <=755) ) { button = SELECT; } // select
return button;
}