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?
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....
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.