/*
Forum: https://forum.arduino.cc/t/text-auf-tastendruck/1399159
Wokwi: https://wokwi.com/projects/438098872682283009
2025/08/01
ec2021
*/
#include <avr/pgmspace.h>
constexpr byte buttonPin[] = {5, 4, 3, 2, 9 ,8, 7, 6};
constexpr uint8_t noOfButtons = sizeof(buttonPin) / sizeof(buttonPin[0]);
constexpr uint16_t maxZeichen {80};
constexpr char text1[] PROGMEM = "Dies ist der erste Text";
constexpr char text2[] PROGMEM = "Dies ist der zweite Text";
constexpr char text3[] PROGMEM = "Dies ist der dritte Text";
constexpr char text4[] PROGMEM = "Dies ist der vierte Text";
constexpr char text5[] PROGMEM = "Dies ist der fünfte Text";
constexpr char text6[] PROGMEM = "Dies ist der sechste Text";
constexpr char text7[] PROGMEM = "Dies ist der siebte Text";
constexpr char text8[] PROGMEM = "Dies ist der achte Text";
constexpr char* const tabelle[] PROGMEM = {text1, text2, text3, text4, text5, text6, text7, text8};
class btClass {
private:
byte pin;
byte state = HIGH;
byte lastState = HIGH;
unsigned long lastChange = 0;
public:
void init(byte aPin) {
pin = aPin;
pinMode(pin, INPUT_PULLUP);
}
boolean pressed() {
byte actState = digitalRead(pin);
if (actState != lastState) {
lastChange = millis();
lastState = actState;
}
if (state != lastState && millis() - lastChange > 30) {
state = lastState;
return state; // Returniert true, wenn der pin-State HIGH ist!!!
}
return false;
}
};
btClass button[noOfButtons];
void setup() {
Serial.begin(115200);
for (int i = 0; i < noOfButtons; i++) {
button[i].init(buttonPin[i]);
}
}
void loop() {
for (int i = 0; i < noOfButtons; i++) {
if (button[i].pressed()) {
printText(i);
}
}
}
void printText(int no) {
char buffer[maxZeichen];
strncpy_P(buffer, (char *)pgm_read_word(&(tabelle[no])),maxZeichen);
buffer[maxZeichen-1] = 0x00;
Serial.println(buffer);
}