Hallo,
ich hab ein Arduino Uno Rev1 und ein LCD Shield - DFRobot (DFR0009)
Übe die tasten am Analog Eingang möchte ich einen Timer (Countdown) einstellen.
Jetzt hab ich erst mal zwei Scripte zusammengewürfelt.
Einmal die Tastenabfrage am ADC und einmal die Timerfunktion mit dem Timer2
der mir jede Sekunde die Uhr um einen herunter zählt.
Jedes Script funktioniert nur wenn ich beide zusammen lege,
bleibt die Uhr nach 3 Sec stehen nach einer ganzen weile
wird dann eine neue Uhrzeit angezeigt.
Ich vermute mal das sich der Interupt für den Timer mit
dem ADC harkt. Hier mal das gesamte Script.
#include <LiquidCrystal.h>
unsigned int tcnt2;
int int_counter = 0;
int minute = 59;
int sekunde = 60;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//Key message
char msgs[5][15] = {"Right Key OK ",
"Up Key OK ",
"Down Key OK ",
"Left Key OK ",
"Select Key OK" };
char hmenue[2][15] = {"Setup ",
"Start "};
int adc_key_val[5] ={30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
int selMenue=0; //menue Start position
int selMenueMax=1; //menue Start position
int selMenueMin=0; //menue Start position
int Minute=2;
int Secunden=30;
void setup()
{
/* First disable the timer overflow interrupt while we're configuring */
TIMSK2 &= ~(1<<TOIE2);
/* Configure timer2 in normal mode (pure counting, no PWM etc.) */
TCCR2A &= ~((1<<WGM21) | (1<<WGM20));
TCCR2B &= ~(1<<WGM22);
/* Select clock source: internal I/O clock */
ASSR &= ~(1<<AS2);
/* Disable Compare Match A interrupt enable (only want overflow) */
TIMSK2 &= ~(1<<OCIE2A);
/* Now configure the prescaler to CPU clock divided by 128 */
TCCR2B |= (1<<CS22) | (1<<CS20); // Set bits
TCCR2B &= ~(1<<CS21); // Clear bit
/* We need to calculate a proper value to load the timer counter.
* The following loads the value 131 into the Timer 2 counter register
* The math behind this is:
* (CPU frequency) / (prescaler value) = 125000 Hz = 8us.
* (desired period) / 8us = 125.
* MAX(uint8) + 1 - 125 = 131;
*/
/* Save value globally for later reload in ISR */
tcnt2 = 131;
lcd.begin(16, 2);
anzeige("Hallo!", 0);
delay(2000);
lcd.clear();
TCNT2 = tcnt2;
TIMSK2 |= (1<<TOIE2);
}
void loop()
{
adc_key_in = analogRead(0); // read the value from the sensor
if (int_counter == 1000) {
int_counter = 0;
sekunde--;
if (sekunde == 0) {
minute--;
sekunde = 59;
}
lcd.setCursor(0,1);
lcd.print("Zeit:");
lcd.setCursor(11,1);
if (minute < 10) {
lcd.print(0);
}
lcd.print(minute);
lcd.print(":");
if (sekunde < 10) {
lcd.print(0);
}
lcd.print(sekunde);
}
key = get_key(adc_key_in); // convert into key press
if (key== -1){
lcd.setCursor(0,0);
lcd.print(key);
}
else{
lcd.setCursor(0,0);
lcd.print(" ");
lcd.print(key);
}
}
void anzeige(char* s, byte zeile)
{
lcd.setCursor(0,zeile);
lcd.print(s);
}
ISR(TIMER2_OVF_vect) {
/* Reload the timer */
TCNT2 = tcnt2;
int_counter += 1;
}
// 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;
}
}
Ich hoffe Ihr habt einen Tipp für mich
Gruß Ingo