27-05-2019, 21:06
Gdy odpalę kod i wcisnę każdy przycisk po kolei w konsoli wyskoczy mi coś takiego:
Te numery to (wydaje mi się) wartości adc_key_in.
Co więcej, umieściłem to pytanie na angielskim forum i pewna osoba podała mi taki kod:
Niestety nie działa jak powinien. Gdy wcisnę jeden przycisk pojawiają mi się takie (albo wcale) komunikaty:
Kod:
0
Right Key OK
143
Up Key OK
327
Down Key OK
503
Left Key OK
741
Select Key OK
Co więcej, umieściłem to pytanie na angielskim forum i pewna osoba podała mi taki kod:
Kod:
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
}
Kod:
Up Key OK
Right Key OK
Up Key OK
Left Key OK
Down Key OK
Left Key OK