Hi
I am currently trying to get LCD Smartie Running on a Leonardo which works great, but im am having an issue regarding the buttons which use the ADC.
The sketch works fine on Duemilanove/UNO the issue is with the Leonardo as it has no TIMER2.
I tried to change the Timer so it would be compatible with the Atmega32u4 with not much success, as i am not very familiar with all the registers (mine field).
I wonder if anyone has any suggestions for change of timer code.
The code sends the relevant button ASCII (1,2,3,4,5) code over serial, back to LCD Smartie to change the current preconfigured screen of choice.
#define DEBOUNCE_MAX 17 // max value for debounce count
#define DEBOUNCE_ON 12 // debounce pressed threshold 12x4ms = 48ms
#define DEBOUNCE_OFF 2 // debounce released threshold 8ms
//int adc_key_val[5] ={30, 150, 360, 535, 760 };
//int adc_key_val[5] ={10, 145, 329, 505, 740 };
// ADC BUTTONS { R , UP, DWN ,L , SEL } MO Buttons
int adc_key_val[5] ={100, 250, 350, 660, 820};// MY ADC KEYPAD
byte NUM_KEYS = 5;
char key=-1;
char oldkey=-1;
// debounce counters
byte button_count[5];
// button status - pressed/released
byte button_status[5];
// button on flags
byte button_flag[5];
//----------------------------------------------------Timer Interrupt Code (Leonardo has no TIMER2) --------------------------------------------------
// Timer2 interrupt routine - 10 Bit?????????
// 1/(160000000/256/(256-6)) = 4ms interval
ISR(TIMER2_OVF_vect) { ////overflow interupt ????????????????????????
TCNT2 = 6;
update_adc_key();
}
// Convert ADC value to key number
char get_key(unsigned int input)
{
char 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;
}
void update_adc_key(){
int adc_key_in;
char key_in;
byte i;
adc_key_in = analogRead(0);
key_in = get_key(adc_key_in);
for(i=0; i<NUM_KEYS; i++)
{
if(key_in==i) //one key is pressed
{
if(button_count[i]<DEBOUNCE_MAX)
{
button_count[i]++;
if(button_count[i]>DEBOUNCE_ON)
{
if(button_status[i] == 0)
{
button_flag[i] = 1; // pressed state flag ON
button_status[i] = 1; //button debounced to 'pressed' status
}
}
}
}
else // no button pressed
{
if (button_count[i] >0)
{
button_count[i]--;
if(button_count[i]<DEBOUNCE_OFF){
button_status[i]=0; //button debounced to 'released' status
}
}
}
// output button pressed state
if(button_flag[i]==1)
{
button_flag[i]=0; // clear flag to avoid repeats
//Change to Serial.write for IDE v1
//Serial.print('A'+i,BYTE); // output MO button code
Serial.write('A'+i); // Send the ASCII button codes A,B,C,D,E Right= "A" ,UP= "B", Down="C" ,Left= "D" , Select= "E"
// Serial.print('A'+i); // output ASCII button code
}
}
}
Many Thanks
koog