Hello! I’m new to Particle and just having some fun learning. I decided try a simple timer for my first project. I have a pulsing led to begin the timer, then the led goes solid for a set time, then it blinks to let the user know it’s done. I was hoping some folks could take a look at my code and let me know how I could refactor it to do better.
Is a For Loop even the right approach?
const int led = D3;
const int pushButton = D2;
int x;
unsigned long prepTime;
unsigned long medTime;
unsigned long downTime;
void setup() {
pinMode(led, OUTPUT);
pinMode(pushButton, INPUT_PULLUP);
Serial.begin(9600);
}
void loop(){
prepTime = 5;
medTime = 5000;
downTime = 4;
int pushButtonState = digitalRead(pushButton);
if(pushButtonState == LOW){
for(int x = 0; x <= prepTime; x++){
Serial.print(" Prep starts here: ");
Serial.print(" led should fade in and out ");
Serial.print(" Values: ");
Serial.print(x);
for(int i = 0; i <= 255; i+=5){
analogWrite(led, i);
delay(50);
}
for(int i = 255; i >= 0; i-=5){
analogWrite(led, i);
delay(50);
}
if(x == prepTime){
x=0;
break;
}
}
for(unsigned long x = 0; x <= medTime; x++){
Serial.print(" Med starts here: ");
Serial.print(" led should be solid ");
Serial.print(" Values: ");
Serial.print(x);
digitalWrite(led, HIGH);
if(x == medTime){
x=0;
break;
}
}
for(int x = 0; x <= downTime; x++){
Serial.print(" Down starts here: ");
Serial.print(" led should be blinking ");
Serial.print(" Values: ");
Serial.print(x);
digitalWrite(led, LOW);
delay(1000);
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
if(x == downTime){
x=0;
Particle.publish("Complete","Good job!",60,PRIVATE);
break;
}
}
}
}
Thanks in advance for your help!