Joystick ADC interrupt

Preformatted textI want to use ADC interrupt and timer to save power when using joystick ?
But my code not work really good. I think because the way I switch between A0 and A1 . Could I have advise how read input from A0 and A1 using ADC interrupt


int check_amux = 0 ; 

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

@ohmybae, please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

thank u , i just fix it

Colon?

Your code does not compile. The colon after setup does not belong there; check_amux is unknown.

Lowlevel stuff is not my strong point.

sorry , i will fix it

sorry i will fix it. cause it a long code so i pick out the part i want so forgot to include the initialize for check_amux

Please give a better description.

One thing might be that Serial.print does not show anything because you're missing a Serial.begin() in setup().

Please do not edit your opening post when posting new / modified code; just put it in a new reply.

thank u
the input not reading correctly like what i expected. it only read the vertical value of joystick and not read the horizontal . The problem might due to the switching between pinA0 and A1 . However , i dont know a better way to switch pin when using ADC interrupt on 2 pin . Furthermore , i think I had reach the limit to replies for today . So could i message u later on


int check_amux = 0 ; 

volatile boolean check_mpu;

volatile boolean adcDone;

volatile int adcReading_vert;

volatile int adcReading_horz;

byte adcPin [2] = { 0 , 1 };

boolean adcStarted;

void setup() 

{
 Serial.begin( 115200);

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

As far as I can see, you never switch analog channels using the ADMUX register.

And I think you're making your life very complicated by using interrupts.