Hey all! I’ve been toying with some code that is able to turn on many LED filaments in either orientation, using a square wave AC output I have designed in my code. Pin A0 and A5 are currently alternating being outputs and grounds. With this A0 will be on while A5 is ground and vise versa. With what I have now, I am able to toggle between an off, low, medium, and high PWM output, however the more I toggle between the three, the more susceptible the device has been to crashing and rebooting. Could somebody look at my code and see if there is a bug causing this? Perhaps something to do with returning values? There isn’t much to it as I’m not much of a coder (yet haha). Thanks!
Here is the code:
int led1 = A0;
int led2 = A5;
void setup()
{
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
Particle.function("led",ledToggle);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
int ledToggle(String command) {
while (command=="off") {
analogWrite(led1,0);
analogWrite(led2,0);
delay(5);
analogWrite(led1,0);
analogWrite(led2,0);
delay(5);
}
while (command=="l") {
analogWrite(led1,64);
analogWrite(led2,0);
delay(5);
analogWrite(led1,0);
analogWrite(led2,64);
delay(5);
}
while (command=="m") {
analogWrite(led1,120);
analogWrite(led2,0);
delay(5);
analogWrite(led1,0);
analogWrite(led2,120);
delay(5);
}
while (command=="h") {
analogWrite(led1,255);
analogWrite(led2,0);
delay(5);
analogWrite(led1,0);
analogWrite(led2,255);
delay(5);
}
}