My LED Wont run when I press the button

I have written some code, i dont really know what I am doing, no error messages appear when i upload but i am trying to make a servo motor and an LED run when I press a button. Everything works except for the LED.


#include <Servo.h>

// constants won't change
const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int SERVO_PIN  = 9; // Arduino pin connected to servo motor's pin
const int LED_PIN    = 3; 
Servo servo; // create servo object to control a servo

// variables will change:
int angle = 0;          // the current angle of servo motor
int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button

void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  servo.attach(SERVO_PIN);           // attaches the servo on pin 9 to the servo object
 // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  servo.write(angle);
  currentButtonState = digitalRead(BUTTON_PIN);
}

void loop() {
  lastButtonState    = currentButtonState;      // save the last state
  currentButtonState = digitalRead(BUTTON_PIN); // read new state

  if(lastButtonState == HIGH && currentButtonState == LOW) {
    Serial.println("The button is pressed");

    // change angle of servo motor
    if(angle == 0)
      angle = 180;
    else
    if(angle == 180)
      angle = 0;

    // control servo motor arccoding to the angle
    servo.write(angle);
   digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(0);                       // wait for a second
  }
}
```Preformatted text
````Preformatted text`

Blockquote

You have a LED_PIN on pin 3 but you don't use it; might that be the problem? You're controlling pin LED_BUILTIN (usually 13).

Which board?

You declare this pin but you never use it. Do you have an LED connected to GPIO pin 3.

The sketch is turning on the on-board LED (LED_BUILTIN).

How do I use it with pin 3?

Replace:

 pinMode(LED_BUILTIN, OUTPUT);

with

 pinMode(LED_PIN, OUTPUT);

Replace:

  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW

with:

  digitalWrite(LED_PIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_PIN, LOW);    // turn the LED off by making the voltage LOW

By the way, delay(0) doesn't do anything.

thanks.

Do you know how to fix the " Expected ',' before } token" error message? It happens when I upload and I dont know how to fix it.

The only way I can help you with that is for you to post your latest code and the complete error message.

Just for your information, it does not happen when you upload. It's a compiler error and you do not even get to the upload :wink:

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