Bug in compiler or what ever?
Simplified circuitry there is 2 LEDs and 1 analog input.
LED connected to pin PB1 lights very dim when output is high.
It seems that output has been configured to high impedance mode.
That happens if inside setup()
analog input A1 (pin 7, PB2) is configured after digital output PB1.
If order of pin configuration is changed everything is OK.
Is that compiler bug?
/* ____
D5 PB5 1|o |8 Vcc
D3 PB3 2| |7 PB2 A1 Voltage measurement, potentiometer
D4 PB4 3| |6 PB1 D1 --|>--|
GND 4|____|5 PB0 D0 --|>--|
*/
byte led0 = PB0;
byte led1 = PB1;
byte potentiometer = A1;
int voltage;
/* It is important in which order pin configuration has been set.
Wrong order causes PB1 to high impedance, low current, output.
This must be compiler bug, I think.
If port BP1 setup has been made using straight to register there is no problem in order.
Any other analog input port and digital port combination works correctly.
*/
void setup()
{
pinMode(led0, OUTPUT);
pinMode(led1, OUTPUT); // Doesn't work if this is before pinMode(potentiometer, INPUT);
//DDRB &= ~(1 << DDB2); // Alternative working setup for analog input A1
pinMode(potentiometer, INPUT);
//pinMode(led1, OUTPUT); // Works if this led1 setup is after pinMode(potentiometer, INPUT);
}
void analog_input()
{
voltage = analogRead(potentiometer);
if (voltage < 500) {
digitalWrite(led0, HIGH);
digitalWrite(led1, LOW);
}
else
{
digitalWrite(led0, LOW);
digitalWrite(led1, HIGH);
}
}
void loop()
{
analog_input();
}