Help: Bad loop syntax

Hello Everyone!

I'm stuck with a very simple code I presume, and I spent already a long time trying many things according to the arduino website.. something still wrong.

I hope I will fin some help and understand why I'm stuck!

Here the goal:
I have a normally closed switch (2) and a led (6)
The program wait till change of state switch opening
the led start Full
wait for a delay
and then fade out in XX seconds

everything works until the led is off then it turns on again, and fade.

So I'm stuck wuth my void loop and I try to make a second one but I don't know how.

Here the code:


const int buttonPin = 2;     // the number of the switch pin
const int ledPin =  6;      // the number of the LED pin

int brightness = 255;    
int fadeAmount = 5;    // how many points to fade the LED by
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}


void loop() {
  
  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // do nothing
    digitalWrite(ledPin, 0);
    Serial.println("Wait");
  } 
  else if (buttonState ==LOW)
  {
    Serial.println("Switch OFF");
    analogWrite(ledPin, brightness);
    delay(500); // delay led ON
    
    for (int i=255; i>=0; i--);
{
        brightness = brightness - fadeAmount;
      Serial.println(brightness);
delay(50);
  
   if (brightness <=0);
  {analogWrite (ledPin, 0);
}
  }
   }
}

I hope my explanation is understandable, will someone take time to help me?

Thanks in advance,

MagicFlash

Remove the semi colon at the end

Ps: make sure you indented the code in the IDE before copying and pasting here, that’s done by pressing ctrlT on a PC or cmdT on a Mac. The indentation would let you see that the block is not within the for loop

1 Like

Loop loops! Whatever you code will keep doing what it does over and over. Look up state machines. Consider making discrete functions. Look up edge detection

Post a schematic and some photos.

What does serial print. I suspect a lot

Remove this semi colon too..

1 Like

Thanks a lot everyone, bad syntax about those both semi colon!

I learned something, thanks again for your help!

FlashSmoke

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.