Analog keypad on Arduino
FunduinoPublished on
Fünf Taster über einen einzigen analogen Eingang auslesen
You need this
Analog keyboard module - 5 buttons, 5 V
3,00 € *Item no. F23106483About the product — Analog keyboard module - 5 buttons, 5 V
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
Breadboard cables male-female - 40 pieces, 20 cm
1,74 € *Item no. FUN-1010554About the product — Breadboard cables male-female - 40 pieces, 20 cm
All 3 parts together 15,86 € *Ready to ship immediately · Delivery time: 1-3 business days
* All prices include statutory VAT, plus shipping costs.
The analog keyboard module
Connections and analog values
The sketch
Detect a button press and display it in the Serial Monitor
int Taster;
int Analogwert = 0;
#define Taster1 1
#define Taster2 2
#define Taster3 3
#define Taster4 4
#define Taster5 5
#define KeinTaster 0
//From here, a new program block called "Tasterstatus" is created. It only checks which button is pressed.
int Tasterabfrage()
{
Analogwert = analogRead(A0); // Read the button module on analog pin A0.
Serial.println(Analogwert); //This line displays the read analog value in the serial monitor. This can be useful if the button module is powered with a voltage other than 5V, as this would result in different analog readings. Otherwise, this line can be omitted.
if (Analogwert > 1000) return KeinTaster; // As long as no button is pressed, the analog value is 1023
if (Analogwert < 15) return Taster1; //The analog value for button 1 is approx. 0
if (Analogwert < 50) return Taster2; //The analog value for button 2 is approx. 30
if (Analogwert < 120) return Taster3; //The analog value for button 3 is approx. 85
if (Analogwert < 300) return Taster4; //The analog value for button 4 is approx. 163
if (Analogwert < 400) return Taster5; //The analog value for button 5 is approx. 348
return KeinTaster; // Output when no button is pressed.
}
// This ends the program block "Tasterstatus".
void setup()
{
Serial.begin(9600); //Serial port communication is started. This is needed to display the read value in the serial monitor.
}
void loop()
{
Taster=Tasterabfrage(); //Here, the loop jumps to the program section "Tasterstatus" above and reads the pressed button.
if (Taster==0) Serial.println("No button was pressed"); // This line can be omitted if output should appear in the serial monitor only when a button is pressed.
if (Taster==1) Serial.println("Button 1 was pressed");
if (Taster==2) Serial.println("Button 2 was pressed");
if (Taster==3) Serial.println("Button 3 was pressed");
if (Taster==4) Serial.println("Button 4 was pressed");
if (Taster==5) Serial.println("Button 5 was pressed");
} //End loopRate the instructions