Blinking L LED (No Blink example) [solved]

Since I uploaded my first sketch, which is shown bellow, I have had my L LED constantly blinking, but not as in the example, it blinks with a shorter interval when its off, so maybe like half a second between it being on, turning it off, and turning it on, that takes about half a second so that definetly is not the blink example, and in my sketch, I have even tried writing digitalWrite(LED_BUILTIN, LOW); but that doesn't seem to work, any clues?

#include <Servo.h>

Servo servo;

const int ledPin = 12;
const int buttonPin = 2;
const int switchPin = 3;

int buttonState = 0;
int switchState = 0;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(switchPin, INPUT);

digitalWrite(LED_BUILTIN, LOW);

digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
}

void loop() {
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, LOW);
}

}

Guess what, I just hooked up the wiring, and now what is happening is the L LED is constantly lit while my 3rd party LED hooked up to the breadboard is now blinking, what is this some FBI agents seeing whenever i hook up the light? Oh and if you are wondering, no the button doesn't do anything >:C

Here is a virtual look of my setup


Even on TinkerCAD, the same thing is happening the LED light is blinking without being told to on the code... Or am i missing something?

You have nothing holding the button pin LOW when the button is not pressed so it could be HIGH or LOW at any time. Consider using INPUT_PULLUP in the pinMode() for the button, wire the button to take the pin to GND when pressed and regard a state of LOW as indicating that the button is pressed

Also, there is no delay() at the end of the 'if' code so the led pin will go LOW then immediately HIGH

Alright, I have never worked with resistors because I understand only the smallest thing about them, which is that they control electricity power, but I never have used them, but ok I will do research about the INPUT_PULLUP and get back to you once I will add that into my code, thanks for responding, also I will change up the code's "if" statement. Again, thanks.

Your button switch is not wired correctly. The switch input is "floating" when the switch is not pressed so its state is indeterminate (random). The best way to wire a switch is to have one side to ground and the other to an input with its pinMode set to INPUT_PULLUP. The input will read HIGH when unpressed and LOW when pressed. Adjust your code as necessary.

You must have a current limiting resistor with the LED or risk damage to the LED and/or the Arduino. Use a 220 to 1000 Ohm resistor in series with the LED.

Fix those things and try the code.

Your code is not in code tags. Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Alright, INPUT_PULLUP it is, I will add that to my code and respond, appreciate it for the image.

@groundFungus Thank you both for helping me out, I have changed up the code so the if is when the button reads low, and I have used INPUT_PULLUP as the pin mode, and now my code works just how I wanted it. I appreciate your support and will be back if I need anything else, again, thanks to both of you.

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