joystick and interrupt hassles

I searched and found a post from 2016 by Bouba ( topic=422211.0 ) where he was trying to use the analog inputs from a joystick to wake up a battery powered remote.

I have a similar problem, except I can use the digital pushbutton of the joystick to bring it out of sleep.

I am using the joystick X to step through blocks on an LCD screen, and the Y to step through the characters to display. It all works fine, and transmits the 2 names displayed, unless I add the battery saving sleep function to turn off the remote after 10 seconds of idle. ( I have shown a simple loopcounter on this example )

The whittled down code below shows the basic reading of the joystick position, and the awakening of the unit with the interrupt pin grounded with the push button on the joystick. ( I switch on the backlight pin to supply 5v to the pots )

It works fine if the sleep function isn’t used, I can read the values from the pots as I move the stick.

I can also do the same when I use the sleep function BEFORE the first sleep, but then when it wakes all 4 values then show only the last yVal, which doesn’t change with the joystick.

I am obviously overlooking something stupid here, and have thrown in some delays and double reading of the analog pins as suggested, but perhaps it is to do with the timers ?

I am so looking forward to having time to actually read up on C++ when I retire, but meanwhile can any one spot my error please ?

   // #include <EEPROM.h>
   // #include <VirtualWire.h> 
   // #include <LiquidCrystal.h> // we need this library for the LCD commands
    #include <avr/sleep.h>      // powerdown library
    #include <avr/interrupt.h>  // interrupts library
    int xPin = A5;  //  joystick pins
    int yPin = A3;
    int txPin = 3;
    int tx = 0;
    int xVal =0;
    int yVal=0;   
    int backlightPin = 18;
    int pin2 = 2; // Int0 interrupt pin
    long loopcount = 0;
    void pin2Interrupt()   {
    void setup()
      Serial.println(" setup");      
      pinMode (backlightPin , OUTPUT );  
      pinMode (txPin , INPUT );  
      digitalWrite(txPin, HIGH);  // set pullup for transmit button
      pinMode (xPin , INPUT );  //  joystick pot wipers
      pinMode (yPin , INPUT ); 
      pinMode(pin2, INPUT);                 // our sleep interrupt pin
      digitalWrite(pin2, HIGH);  // set pull up res for interrupt
    void loop()   
      if ( loopcount >= 30 ) {   // for testing
        enterSleep () ; 
      digitalWrite(backlightPin, HIGH); // lights LCD and supplies 5v to joystic pots  
      delay ( 10 );  // let 5v get up for the joystick
      xVal = 0;  yVal = 0 ;
      readjoystick ();
      loopcount ++ ;     
      Serial.print(" loopcount = ");  
    void readjoystick () {
      Serial.print("  in readjoystick ,   ");
      delay ( 100 ) ;
      xVal = analogRead(xPin);  
      xVal = analogRead(xPin);  // hi Z pots read twice
      Serial.print("xVal =  ");  
           delay ( 100 ) ;
      yVal = analogRead(yPin);  
      yVal = analogRead(yPin);  // hi Z pots read twice
      Serial.print(",    yVal =  ");  
    void enterSleep()
      Serial.println(" in entersleep  "); 
      attachInterrupt(0, pin2Interrupt, FALLING);
      delay(50); // need this?
      digitalWrite(backlightPin, LOW); 
      set_sleep_mode(SLEEP_MODE_PWR_DOWN);  // setting up for sleep ...
      sleep_enable();                       // setting up for sleep ...
      ADCSRA &= ~(1 << ADEN);
      PRR = 0xFF;
      sleep_mode();                         // now goes to Sleep and waits for the interrupt
      /* The program will continue from here after the interrupt. */
      //  Serial.println(" waking up ");
      detachInterrupt(0);                 //disable interrupts while we get ready to read the keypad 
      PRR = 0x00;
      /* First thing to do is disable sleep. */
      digitalWrite(backlightPin, HIGH);   // lights LCD and supplies 5v to joystic pots                       
      loopcount = 0;   
      xVal = 0;  yVal = 0 ;      
    }  //  end of entersleep
      PRR = 0x00;

Doesn’t this switch off all the peripherals including the analog subsystem?

Ah, I have no idea, I just copied all that section from someone elses sketch.

Perhaps I will leave that out and check the standby current,


Thanks MorganS,
It was the ADCSRA &= ~(1 << ADEN); that was causing it ( obviously in retrospect :slight_smile: )
I left that out and the sleep current for this section is about 10 microamps so for 2000mAH AA cells would give me years so thats fine.