Digital read on analog pin doesn't work

Hi,

I've connected an active-high three-way switch to the PD4, PC2 and PC3 pins of an atmega328p, who are according to this graphic referenced in the arduino software as pin 4, A2 or 16 and A3 or 17.
But when running the following code, it seems like only pin 4 works:

// --- Pin definitions ---
// Volume switch
#define VOL_INTERRUPT_SWITCH 3     // goes high when three-way switch is used
#define VOL_RIGHT_SWITCH 4         // goes high when three-way switch is in right position
#define VOL_LEFT_SWITCH 17         // goes high when three-way switch is in left position
#define VOL_PRESSED_SWITCH 16      // goes high when three-way switch is pressed

//etc...

void volumeControl() {
  if(switchActive == false && digitalRead(VOL_INTERRUPT_SWITCH) == HIGH)
  {
    Serial.println("- Interrupt from volume button"); // debug
    Serial.println("Button states:" + (String)digitalRead(VOL_RIGHT_SWITCH) + ", " + (String)digitalRead(VOL_LEFT_SWITCH) + ", " + (String)digitalRead(VOL_PRESSED_SWITCH));// debug
    if (digitalRead(VOL_RIGHT_SWITCH) == HIGH)
    {
      Serial.println("-- Volume button right detected"); // debug
      if (volume < 255)
      {
        volume += 5;
        Serial.println("--- Volume button right action"); // debug
      }
    }
    else if(digitalRead(VOL_LEFT_SWITCH) == HIGH)
    {
      Serial.println("-- Volume button left detected"); // debug
      if (volume > 0)
      {
        volume -= 5;
        Serial.println("--- Volume button left action"); // debug
      }
    }
    else if(digitalRead(VOL_PRESSED_SWITCH) == HIGH)
    {
       mute = !mute; // switch state of mute variable
       Serial.println("Volume button pressed - Mute"); // debug
    }
  }
  switchActive = (digitalRead(VOL_INTERRUPT_SWITCH) == HIGH);
}

Just for understanding: Pin 3 is pulled high while the switch is used.

I measured all signals with a multimeter and they seem ok, but somehow the two who are connected to analog pins can't be read out, they only give a digital zero or random noise when read out analog. Is there something I missed with the pin-numbering? Or why do both digital pins work as expected, but the analog ones don't?

Thanks for your replies,
m9898

IF you're trying to read pin A2, why don't you reference it with "A2", instead of trying to figure out the actual pin number? That's probably where you're going off-course....

Regards,
Ray L.

The pin mapping between the A* analogue pin number aliases and digital pin numbers depends on the board being used. Why not just use A2 and A3 instead of pin numbers ?

As you have not posted your whole program who knows how you have setup the pins using the pinMode() function.

Well, actually I tried both ways to reference to the pins, so referencing with A2 and A3 does't work either.
Here is the relevant part of the schematics, the signals are then just directly connected to the atmega.

And this is the full code, using the mozzi sound synthesis library:

#include <MozziGuts.h>             // Mozzi main library 
#include <Oscil.h>                 // Mozzi template for an oscillator
#include <tables/sin2048_int8.h>   // Mozzi wavetable holding a sine wave
#include <twi_nonblock.h>          // Mozzi non-blocking I2C Library

// --- Mozzi definitions ---
Oscil <2048, AUDIO_RATE> aSin(SIN2048_DATA); // defines an sine tone oscillator with the sin2048 wavetable. Structure: Oscil <table_size, update_rate> name(table_data);
#define CONTROL_RATE 64            // number of control updates per second, only powers of 2
volatile int audioFrequency = 440; // stores present audio frequency
volatile byte volume = 100;        // stores present selected volume
volatile boolean mute = false;     // stores audio mute state
volatile boolean switchActive = false; // changes to true when three-way switch is used

// --- Audio Amplifier I2C definitions ---
#define AMP_ADDRESS 0x60       // TPA6130A2 device address
// Register 1: HP_EN_L, HP_EN_R, Mode[1], Mode[0], Reserved, Reserved, Reserved, Thermal, SWS
#define AMP_REG1 0x1           // TPA6130A2 register 1 address
#define AMP_REG1_SETUP 0xc0    // default configuration: 11000000 - both channels enabled
// Register 2: Mute_L, Mute_R, Volume[5-0]
#define AMP_REG2 0x2           // TPA6130A2 register 2 address
#define AMP_REG2_SETUP 0x34    // default configuration: 00110100 - both channels on –0.3 dB Gain

// --- Pin definitions ---
// Volume switch
#define VOL_INTERRUPT_SWITCH 3     // goes high when three-way switch is used
#define VOL_RIGHT_SWITCH 4         // goes high when three-way switch is in right position
#define VOL_LEFT_SWITCH 17         // goes high when three-way switch is in left position
#define VOL_PRESSED_SWITCH 16      // goes high when three-way switch is pressed




void setup() {
  delay(400);                           // wait for stable power supply
  Serial.begin(9600);                   // serial debug
  Serial.println("Gestartet");          // debug
  pinMode(VOL_INTERRUPT_SWITCH, INPUT); // sets digital pin 3 as digital input
  pinMode(VOL_RIGHT_SWITCH, INPUT);     // sets digital pin 4 as digital input
  pinMode(VOL_LEFT_SWITCH, INPUT);      // sets analog  pin 3 as digital input
  pinMode(VOL_PRESSED_SWITCH, INPUT);   // sets analog  pin 2 as digital input
  
  startMozzi(CONTROL_RATE);  // start sound library
  initialize_twi_nonblock(); // initialize I2C library and join I2C-Bus
  initializeAmp();           // initialize the headphone amplifier to standard setup values
  //attachInterrupt(VOL_INTERRUPT_SWITCH,volumeControl,RISING);  // attach Interrupt to pin D3 - triggered when three-way switch is used, seems not to work with mozzi
}

void initializeAmp() {
    amp_writeTo(AMP_REG1, AMP_REG1_SETUP);
    amp_writeTo(AMP_REG2, AMP_REG2_SETUP);
}

void updateControl() {
  aSin.setFreq(audioFrequency);
  volumeControl();
}

int updateAudio() {
  return (aSin.next() * volume)>>8;
}

void volumeControl() {
  if(switchActive == false && digitalRead(VOL_INTERRUPT_SWITCH) == HIGH)
  {
    Serial.println("- Interrupt from volume button"); // debug
    Serial.println("Button states:" + (String)digitalRead(VOL_RIGHT_SWITCH) + ", " + (String)digitalRead(VOL_LEFT_SWITCH) + ", " + (String)digitalRead(VOL_PRESSED_SWITCH));// debug
    if (digitalRead(VOL_RIGHT_SWITCH) == HIGH)
    {
      Serial.println("-- Volume button right detected"); // debug
      if (volume < 255)
      {
        volume += 5;
        Serial.println("--- Volume button right action"); // debug
      }
    }
    else if(digitalRead(VOL_LEFT_SWITCH) == HIGH)
    {
      Serial.println("-- Volume button left detected"); // debug
      if (volume > 0)
      {
        volume -= 5;
        Serial.println("--- Volume button left action"); // debug
      }
    }
    else if(digitalRead(VOL_PRESSED_SWITCH) == HIGH)
    {
       mute = !mute; // switch state of mute variable
       Serial.println("Volume button pressed - Mute"); // debug
    }
  }
  switchActive = (digitalRead(VOL_INTERRUPT_SWITCH) == HIGH);
}

void amp_writeTo(byte address, byte val) {
  twowire_beginTransmission(AMP_ADDRESS);  // start transmission to device   
  twowire_send( address );                 // send register address
  twowire_send( val );                     // send value to write
  twowire_endTransmission();               // end transmission
}

void loop() {
  audioHook();
}

Well the pins look like they are pulled down OK - check with a multimeter?

Checked it again, the pins are pulled down correctly and go high when the switch is actuated in the according position.

Is there any information missing?

There's the really simple test sketch that does nothing more than read the inputs and print the state, that you forgot to post.