PerryBebbington:
Bill,Here is my code, it is not very responsive and often pressing the buttons does not cause anything to appear in the serial monitor.
const uint8_t BUTTONPIN = 2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode (BUTTONPIN, INPUT_PULLUP);
pinMode (LED_BUILTIN, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(BUTTONPIN) == LOW) {
Serial.print("Button pressed");
}
digitalWrite(LED_BUILTIN, HIGH);
delay(5000);
digitalWrite(LED_BUILTIN, LOW);
delay(5000);
}
I am new to coding and have no idea why this is. Please help. Thanks, A Noob. So, serious point, how would you help someone with this without pushing them to blink without delay and similar techniques?
Ironically, this sketch demonstrates the exact issue I initially brought up of polling for things in loops.
Kind of funny....
First,
I would explain to them that they can't use delay() for something like this as it pauses everything so it will affect button reads.
Then I would refer them to a timer or metro library to handle the scheduling of the blinking LED so that the processing of the button does not get held up to do the blinking.
Next,
I would explain to them and warn them that there can be some real-world issues related to reading buttons due to electrical signal bouncing that can happen by just doing a simple button read as they have done.
And I would also tell them that once they remove the delay() issue, that they will likely start to see issues related to electrical bounce.
Then I would refer them to a button library that handles all that for them so that they can avoid all the pitfalls and real-world issues relating to switch/button debouncing and pick up some extra capabilities like being able to easily do things on a button being pressed, being released, or being pressed for a certain amount of time.
All easy to do with a button library.
While I see it all the time, I am against the constant pushing everyone twards the "blink without delay" technique to avoid some issues caused by using delay().
While it can work, I think it is often a bit cruel and unhelpful to many newbies and less technical people that just want to get things done.
There are usually easier ways (for them) to do what they want than having to mess around with dealing with inline counts and comparisons of millis() values.
To me what Arduino is about is making things easy and much of that is accomplished through libraries.
Arduino has a great big offering of libraries to do things that allows less technical users that have limited programming skills to string things together to do some amazing things.
IMO, the entire blink with out delay and its creation came out of a serious hole in the Arduino platform.
That hole is that Arduino contains no scheduling capabilities in its core library or bundled libraries.
This "blink with out delay" stuff should not be needed for many applications and often can end up with less technical users running into issues from creating incorrect implementations don't work or that fall apart on millis() rollover issues due to how the code was implemented.
I see lots and lots of posts on on the forum related to this and various issues around trying to implement it.
IMO, none of this "blink without delay" type stuff should be necessary.
It could be avoided through the use of libraries and, IMO, Arduino should include some simple scheduling capabilities out of the box so that some minimum level of scheduling is available on all Arduino platforms.
--- bill