void setup() {
pinMode(0, INPUT); // charging check, pin 5 of ATTiny85
pinMode(1, INPUT); // done check, pin 6 of ATTiny85
pinMode(2, INPUT); // low battery check pin 7 of ATTiny85
pinMode(3, OUTPUT); // red LED, pin 2 of ATTiny85
pinMode(4, OUTPUT); // green LED, pin 3 of ATTiny85 both on should produce orange/yellow light
} // pin 1 is reset, pin 4 and 8 is GND and VCC
// LEDs are common anode, red and green through current limiting resistor to ATTiny85
void loop() {
if (digitalRead(2) == LOW) { // low battery signal, turn on red only
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
} else if (digitalRead(1) == LOW) { // charging is done status, turn on green only
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
} else if (digitalRead(0) == LOW) { // charging in progress, turn on both LEDs
digitalWrite(3, !digitalRead(3)); // fake PWM, was too orange/red as is
digitalWrite(4, LOW);
} else { // LED off, not plugged in and not powered on.
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
}
I am using a 2mm common anode R/G LED and it will be tied into Adafruit Powerboost 1000c since the onboard LEDs will not be visible when I install it into my portable game console. I wanted to move those LED, make it work on a single bulb and small so 2mm R/G LED is the end. Since the Powerboost 1000c has 3 LEDs, converting them into 2 LEDs by using original red (low battery) and green (charging done) as they were and making orange from combining red/green on original yellow charging LED.
When I did the original code, the red/green combined appeared reddish-orange and somewhat hard to see from pure red so I faked PWM on red lead to reduce its brightness by half, it appears more orange/yellow and easier to see. I don't have a datasheet on this specific LED, it is an old stock as no one regularly makes 2mm bicolor LEDs so I don't know if red and green has close spec or if green has slightly higher requirement than red. I supposed I could change the resistor to something a bit lower for green or higher for red.
The issue: is that safe and legal coding or would I burn up the chip?
digitalWrite(3, !digitalRead(3)); // fake PWM, was too orange/red as is
alternates red pin at each loop as long as pin D0 is high
I coded the priority using else so if I am charging and the battery was low, it wouldn't try to flip-flop between low and charging. If it's low, it lights up red and skips the rest of if statements. And when the battery's not low and not charging, the LED stays off.
Total code (with unnecessary options like bootloader, milis(), and brownout all disabled, LTO enabled) was 478 bytes. This should work on ATTiny13