I am working on a sketch involving a LED. I noticed that at startup, the LED (at digital port 8) is ON and not OFF.
So I added a digitalWrite(8, LOW); to the void_setup, but it does not seem to work. At power up the LED comes on and stays on.
Even if I put a digitalWrite-LOW in the main-loop, the LED stays on.
If I make it a blinking LED with writing HIGH and LOW, then it works as expected.
What am I missing here?
// Variables
int LED1 = 8; // Ped-pin for LED
// Basis
void setup() {
pinMode(LED1, OUTPUT);
digitalWrite(LED1, LOW);
}
// Main loop
void loop() {
//digitalWrite(LED1, HIGH);
//delay(1000);
//digitalWrite(LED1, LOW);
' //delay(1000);
}
THANK YOU!
That was the problem. The LED was connected the 5V rail on the breadboard instead of to GND.
Very much a beginner's mistake I admit (I confess I have only recently started playing with Arduino stuff).
The sketch I am working on measures distance with an ultrasonic sensor. Am also using a DHT11 to measure temp and humidity to correct the soundspeed for the calculations.
And then display all this info to a .96 OLED screen.
The idea is to have the LED blinker faster as a measured distance get shorter and slower if the distance is large.
That works now. But I still need to figure how to blink the LED slower or faster without slowing down the main-loop. (It's now being done with a adjusted delay-statement which of course simply slows down the loop.
See @anon57585045's suggestion. Designing functions that do things in destinctive time intervals without using blocking functions (like delay()) is one of the essential capabilities...
You should try to get a good understanding of how to work with the millis() function. That will avoid a lot of frustrating hours...
Thank you everyone for responding so quickly.
The millisec-option is one I am looking into right now but I should be abe to figure that out. Hadn't seen the example before but looks like this is the solution I was looking for.