Hi , I currently want to use interrupt ADC and timer interrupt to debounce analog joystick . Cause of COVID -19 , I have to work with simulation so could not use the hardware.
Instead of let the joystick input to be 512 512 512 , i just want it to be single data like 512.
My code work fine for one input pin A0 . However, when it came to 2 input pin A0 and A1 , it work not very good (i think the error come from the way i switch the input A0 and A1 ).
Is there a way i can switch around A1 and A0 to read the input from joystick ?
Futhermore , is there a better way i can use to debounce my analog joystick ?
Here what i do to switch between A0 and A1 . Code :
//initalize variable
volatile boolean check_mpu;
volatile boolean adcDone;
volatile int adcReading_vert;
volatile int adcReading_horz;
byte adcPin [2] = { 0 , 1 };
boolean adcStarted;
void setup() :
{
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12 and CS10 bits for 256 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();//allow interrupts
ADCSRA = bit (ADEN); // turn ADC on
ADCSRA |= bit (ADPS0) | bit (ADPS1) | bit (ADPS2); // Prescaler of 128
ADMUX = bit (REFS0) | (adcPin[check_amux] & 0x07) ; // AVcc and select input port
}
ISR(TIMER1_COMPA_vect){
check_mpu = true ;
adcDone = true ;
}
ISR (ADC_vect){
adcDone = false;
if (check_amux == 0){
adcReading_vert = ADC;
check_amux = 1;
}
else if ( check_amux == 1){
adcReading_horz = ADC;
check_amux = 0;
}
} // end of ADC_vect
void loop()
{
if (adcDone == true)
{
adcStarted = false;
// do something with the reading, for example, print it
Serial.println("vertical");
Serial.println (adcReading_vert);
Serial.println("horizontal");
Serial.println (adcReading_horz);
}
// if we aren't taking a reading, start a new one
if (!adcStarted)
{
adcStarted = true;
// start the conversion
ADCSRA |= bit (ADSC) | bit (ADIE);
}
}