I have to create an electronic circuit that turns on an LED when pressing a button, if the LED is held down it only detects as if it were pressed once, in general if I press the button n times it will turn on n times but just after I finish pressing the button, like if I push it 15 times it will then light up 15 times but only after
Hello monloca
Welcome to the world's best Arduino forum ever.
Post your sketch, well formated, with well-tempered comments in so called code tags and schematic to see how we can help.
Have a nice day and enjoy coding in C++.
Is it for school ?
Does it have to replay it with the timing as well ? If you press the button with morse code, should it repeat the morse code to the led ? or do you only have to count the number of times ?
The first thing that you have to deal with is the bouncing of the button.
Then you have to look into millis() for timing.
You could try it in the Wokwi simulation. Start with an Arduino Uno, add a button, a led and resistors, and start coding: https://wokwi.com/
In the IDE Examples > Digital > StateChangeDetection
may be more what you're looking for
How?
Yes, it is for school.
And yes the led must repeat the the input of the button
Can you combine debouncing of the button, the StateChangeDetection and millis() ?
Oue teacher told us it didnt really mattered, he told us we could use a while loop
Excellent.
Write two small, stand-alone sketches.
- Debounce a button (look in IDE >> FILE >> EXAMPLES >> BUILT-IN EXAMPLES)
- A state change (see BUILT-IN EXAMPLES)
Write a third sketch:
3. Debounce a button using millis()
Write a fifth sketch:
4. Count the button presses from #3 and change states from #2.
Fifth:
5. Profit!
- Conquer the world with your army of robots!
Thanks I'll try it <3. Thank you!!
Programming Electronics Academy does a good job at describing and showing how to debounce a button press, using a transcript and video:
Wrote this 12 years ago for Nano, might work for you. If you need explanatory comments let me know.
uint32_t tStart, timeStart, tEnd = 1500;
const byte dbTime = 30, btn = 4, ledPin = LED_BUILTIN;
bool btnState = false, bState = true, oldBstate = true,
timing = false;
byte cntr;
void setup()
{
Serial.begin(9600);
Serial.println("Button press counter");
pinMode(btn, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop()
{
// debounce-----------------------------
if (digitalRead(btn) != btnState) // they are different!
{
btnState ^= 1; // make them equal
timeStart = millis(); // restart timer
}
if (millis() - timeStart > dbTime) // if not changed for dbTime,
bState = btnState; // btnState is valid, use it
// end debounce-------------------------
if(!timing && !bState && oldBstate)
{
tStart = millis();
timing = true;
}
if(timing && !bState && oldBstate)
{
cntr++;
tStart = millis();
oldBstate = bState;
}
if(millis() - tStart > tEnd)
{
timing = false;
while(cntr > 0)
{
Serial.println(cntr);
digitalWrite(ledPin,HIGH);
delay(50);
digitalWrite(ledPin,LOW);
delay(500);
cntr --;
if(cntr == 0)
Serial.println(cntr);
}
}
oldBstate = bState;
}
Thank you!!!
I'll check it out <3
A small program to check a button without causing the problem of "pressed many times." You will notice this button is checked twice, once for "pressed" and once for "released".
byte buttonPin = 3; // arduino pin >> button pin 1 then button pin 2 >> ground
byte state = 0; // current state
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP); // HIGH when not pressed
Serial.print("Button state: 0");
}
void loop() {
if (digitalRead(buttonPin) == 0) { // read button for PRESSED
delay(50); // "button debounce" to wait for natural button press ringing
if (digitalRead(buttonPin) != 0) { // read button for RELEASED
Serial.print(++state % 10); // count button presses from 0 to 9
}
}
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.