Fake PWM, it works but is it safe and legal?

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

absolutely… Software pwm is as old as LEDS…

TRY THIS - no hardware PWM in sight.

It exist no command which function is to damage it own chip. Only cartoon comic evil "genius" make self-destruction button on everything items builded.

You need to get out more.

a7

aha, IBM is an evil. Samsung ftw!

I can see it now..... a hacker learns how to "trigger destruction" and all the IBM hardware is turned to a lump of silicon.

I have clearly been watching too much political commentary. I immediately related your question to "Fake News" :slight_smile:

not IBM hardware but US military. but this news are not proved.

Now if we could get the Chinese to steal it an use in their military products....... :slight_smile:

Killer poke command have killed old Commodore PET computers. There are ways to make computer or electronics self destruct without making it an intentional suicide design.

I turned up the self destruct chips whilst looking for documentation of a rumored “self-destruct” program that would kill an IBM PC if executed… haven’t found it yet, was I dreaming?

I can see the whole thing, the computer and monitor turned into a pile of flaming rubbish in just a few minutes.

a7

There was also the somewhat overhyped HCF (Halt and Catch Fire) opcode

I assume y’all can waste as much time as I do, googled for a reminder of certain op codes…

a7

Killer airplane captured some terrorists and crushed them into famous skyscraper.

You are wanting to represent three conditions (or 4 - power off = no light) with a 2 color LED.
OK
Green LEDs (generally) are MUCH less efficient then red. You can adjust the brightness of each by changing the current limiting resisitors to make the green current higher and the red lower.

You could also change the code so the indicator flashes on & off to indicate charging.

Also be aware 1 in 7 males have some degree of red - green color blindness!