What is wrong with my code ?

Hello,
i am new with arduino and i am trying to create a when i press the button the led will turn on, and when i press it again it should turns off.
but it don't works >:(
i really don't know what i am doing wrong.
I hope anybody can help me.
Thanks!

const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonOn = 0;
int buttonC = 0;
int LED = LOW;

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
buttonC = 1;
}

if (buttonState == LOW && buttonC == 1) {
if (buttonOn = 0) {
buttonOn = 1;
LED = HIGH;
buttonC = 0;

}
if (buttonOn = 1) {
buttonOn = 0;
LED = LOW;
buttonC = 0;
}

}
digitalWrite(ledPin, LED);
}

i really don't know what i am doing wrong.

Not reading "Read this before posting a programming question ..."

Not putting your code in code tags
Not describing what "It don't works" means
Not showing how your button is wired

Not debouncing your button
Maybe not using INPUT_PULLUP

Read this carefully:

    if (buttonOn = 0) {
      buttonOn = 1;
      LED = HIGH;
      buttonC = 0;

    }
    if (buttonOn = 1) {
      buttonOn = 0;
      LED = LOW;
      buttonC = 0;
    }

First, the syntax of an if test is

(if x == y) {  // <-- '==' and not '='
...
}

Second, if you enter the first test with buttonOn equal to 0 (which is the case at initialization), you will set buttonOn to 1, then enter the second test en set it again to 0, furthermore setting LED to LOW. So no light.

Maybe use

    else if (buttonOn == 1) {

I have copy this : https://www.arduino.cc/en/tutorial/button
and changed my way.
The circuit works if i use it from the website.

Can't follow your logic too easily without code tags and on my tiny phone screen.

But I would tackle this by looking at the state change detect example in the ide File / Examples.

Its working now i have changed to this.

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    buttonOn = 1;
   // digitalWrite(ledPin, HIGH);

  } else if (buttonOn == 1) {
    delay(10);
    buttonOn = 0;
    
    if (buttonC == 0) {
      buttonC = 1;
      
      digitalWrite(ledPin, HIGH);
      
    } else {
      buttonC = 0;
      digitalWrite(ledPin, LOW);
    }
  }
}

jordy82:
Its working now i have changed to this.

What Arduino are you using? Your sketch won't compile for me because 'ledPin' is not declared. Then 'buttonPin' was not declared. Then 'buttonState' was not declared. Then 'buttonOn', then 'buttonC'... which you only set and never read.

this is written a bit wierd. seems there are more variable and lines than what you need.

this might be a little easier for a beginner to follow:

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int last = 0;  
int LED = LOW;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  // check for change like this
  if(buttonState!=last){   last=buttonState;

 // check if the change was to high
    if(buttonState==HIGH){

    // here is where you do stuff
    // change the led to opposite of what it was before
    if(LED==LOW){LED=HIGH;}else{LED=LOW;}
      digitalWrite(ledPin, LED);
    }
 
    }


}

Here's taterking's code with each { and } on their own line and control-T'd which is even easier to follow :wink:

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int last = 0;
int LED = LOW;

void setup()
{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop()
{
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  // check for change like this
  if (buttonState != last)
  {
    last = buttonState;

    // check if the change was to high
    if (buttonState == HIGH)
    {

      // here is where you do stuff
      // change the led to opposite of what it was before
      if (LED == LOW)
      {
        LED = HIGH;
      }
      else
      {
        LED = LOW;
      }
      digitalWrite(ledPin, LED);
    }
  }


}

And here is the code using 'static' and local variables where global variables are not needed. I also added debounce so if the button contacts are noisy the LED will still switch reliably. I like to use an initial capital letter on global variables so it is easier to tell in the code where to look for the declaration. For values that will only contain 0 or 1 (like pin states) I like to use the 'bool' (boolean) type. The compiler will then make sure that the value doesn't go out of range. LOW==0==false, 1==HIGH==true.

const int ButtonPin = 2;     // the number of the pushbutton pin
const int LEDPin =  13;      // the number of the LED pin
const unsigned long DebounceInterval = 10;


// variables will change:
bool LEDState = LOW;


void setup()
{
  pinMode(LEDPin, OUTPUT);
  pinMode(ButtonPin, INPUT);  // External pull-down, active HIGH
}


void loop()
{
  static unsigned long lastButtonChangeTime = 0;
  static bool lastButtonState = LOW;


  unsigned long currentMillis = millis();
  bool buttonState = digitalRead(ButtonPin);


  // check for change like this
  if (buttonState != lastButtonState && currentMillis - lastButtonChangeTime >= DebounceInterval)
  {
    lastButtonState = buttonState;
    lastButtonChangeTime = currentMillis;


    // check if the change was to high
    if (buttonState)
    {
      LEDState = !LEDState;
      digitalWrite(LEDPin, LEDState);
    }
  }
}

johnwasser:
And here is the code using 'static' and local variables where global variables are not needed. I also added debounce so if the button contacts are noisy the LED will still switch reliably. I like to use an initial capital letter on global variables so it is easier to tell in the code where to look for the declaration. For values that will only contain 0 or 1 (like pin states) I like to use the 'bool' (boolean) type. The compiler will then make sure that the value doesn't go out of range. LOW==0==false, 1==HIGH==true.

const int ButtonPin = 2;     // the number of the pushbutton pin

const int LEDPin =  13;      // the number of the LED pin
const unsigned long DebounceInterval = 10;

// variables will change:
bool LEDState = LOW;

void setup()
{
  pinMode(LEDPin, OUTPUT);
  pinMode(ButtonPin, INPUT);  // External pull-down, active HIGH
}

void loop()
{
  static unsigned long lastButtonChangeTime = 0;
  static bool lastButtonState = LOW;

unsigned long currentMillis = millis();
  bool buttonState = digitalRead(ButtonPin);

// check for change like this
  if (buttonState != lastButtonState && currentMillis - lastButtonChangeTime >= DebounceInterval)
  {
    lastButtonState = buttonState;
    lastButtonChangeTime = currentMillis;

// check if the change was to high
    if (buttonState)
    {
      LEDState = !LEDState;
      digitalWrite(LEDPin, LEDState);
    }
  }
}

Thanks johnwasser !
also thank you for the explanation this really helps me alot.