So now pin 2 will read HIGH unless you ground it with a switch, like normal people, or are experimenting and use a logically LOW output pin 3 and connect 2 to 3 with a jumper.
And as pointed out, as long as anything is making the input read LOW, the build in LED will blink. And blink. And blink…
If you use a pin to read a switch, use INPUT_PULLUP mode.
The pin gets a low 5V current through 20K to 50K resistance, not enough power to burn the pin out. Jumper that to GND for a switch, don't use an IO pin to ground another.
My test button is a jumper I touch to the grounded USB port box to "press".
I wanted to use piezo disks as tap and force sensors, I ended up filling a wire with charge proportional to the tap.
The wire holds that charge until digital reads drains it... digital pins 'eat' 1 microamp per readin a loop that reads 1 million times a second until the pin goes LOW and the count is an analog measure of the tap force.
I got 10K to 5V and it seems to work but in reverse.
I tried to GND and it does not work. Not sure how to reverse the reverse effect.
Anyway i ran into another problem not sure if its a hardware or a software issue.
I am trying to get the LED blink few times and then stay ON as long as i hold the button.
But sometimes when i let go (sometimes) code will run again and blink button few times instead of just turning it off.
It should loop infinitely once entered the function but it does not. It loops exactly as many times as the main loop has looped and exits back to main loop for some reason.
How is this even possible ?
It makes no sense from a logic perspective. It should loop forever until condition is......there is no condition, i just want it to loop.
Why ?
I need LED to stay ON as long as i hold the button.
I mean with the pattern i have, LED will blink few times and stay ON while i hole the button.
I dont know of any other way to make it happen other then enter a loop that keepts the LED on HIGH, but it does not work. It keeps blinking OVER AND OVER AND OVER
I will have to dig into your example to understand how it works. Thank you it does work.
For now, i opted out to do this instead, something i can understand at my monkey level.
while (1){
Serial.println("Holding");
if (digitalRead(buttonPin) == HIGH) {
break;
}
}
Not sure what are the side effects of this are other then your code is 41 lines and mine is 35 but it behaves just like yours.
Thanks !
I see.
Thank you for clarifying it to me in a way i can understand.
There is allot for me to learn here for sure.
I used to program allot using language called AutoIT for windows, some html, and php once in a blue moon.
I plan on making a break light blinker for my car. I wan to use a mosfet to blink 12v going into the light bulbs of the break lights using arduino.
Some have done it already but i dont like their approach. I want my arduino powered all the time so i will likely step down from 12v (after ignition) to 5v, that way arduino is ON as long as the car key is turned ON.
It seems like a lot but the basics are the basics and they are difficult until you know how. The basic concepts you should know to do your project:
The loop function loops, faster is better, use non blocking code.
Edge detection
State machines
Millis timing (blink without delay example in ide)
Basic button hardware as above.
The arduino is NOT a power supply, it is a logic supply
That's the pin to digital read. A LOW means the switch is closed and the low current 5V from the chip is safely flowing to GND (ground). A HIGH means the switch is open, current can't flow.