Denouncing joystick ? Should I using Multiple ADC interrupt for joystick debounce

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);
    }    
}

Why interrupts? Did you ever try without?
Without interrupts you won't have related problems and questions.

I have try without interrupt , but joystick don’t give me a single result like 512 . It give me like 512 , 512 ,512,...

So I think about use time interrupt and adc to debounce

If so , do u have any suggestion for me to debounce the joystick ? Use millis and if else ?

Hello
Take a DMM and check the outputs of the joystick to see what types of output signals will be provided.

Sorry I forgot to notice that cause of COVID . I have to work with simulation. Don’t have any hardware with me .

The joystick or any other analog input gives you as many values as you call analogRead(). If all values are the same there is nothing to debounce, you simply ignore unchanged values.

If you really mean debouncing an input then look at the Debounce, StateChangeDetection and Smoothing examples in the IDE. They are applicable to analog inputs as well, with a change of the data type from bool to int.

Oh ok, but I also want to using both ADC and timer interrupt to lowing my usage power . It there I way I can using ADC for this case?

You can use whatever you like to reduce power consumption. I don't know what makes most sense in your project.