Hello,
I wanted to write a small test program for an ESP32. It should be a running light and when I press button 1 the flashing interval should be 100ms, for button 2 500ms and for button 3 1000ms, unfortunately the interval is always 1000ms no matter which button I press. Can someone tell me what I'm doing wrong?
Thank you very much
Hans
Have you checked that for your particular ESP32 module, GPIO pins 0, 1 and 2 are freely available as inputs, and not allocated for some other function?
when your code is doing LOW/HIGH illumination on these many pins, with delays, it can not read your button press. If you press and quickly release the button - then it is very unlikely that your sketch could pick it up. Try to press and hold the button.
So your buttons are HIGH by default, when they're NOT pressed (assuming you wired the button between the GPIO and GND, as it should be).
This will happen if the button is NOT pressed, since you're looking for the GPIO being HIGH.
Try if (Taster1 == LOW) Pause = 100;
(same for the other keys).
Having said that, this way of reading buttons is a recipe for disaster. I'd restructure the whole program. However, it should sort of work, in principle, if you fix the logic flaw.
I switched the inputs to 3.3 volts using the button and pressed the buttons long enough. I have now changed the inputs to pins 0, 4 and 5. The program starts with the value of button 1, when I press button 2 it changes to the value of button 2, when I press button 3 it changes to the value of button 3. If no button is pressed, the value of button 1 always works.
He can easily fix it by creating his own void my_delay( .. ) which can read the buttons and then doing actual delay(). Not ideal but better than what he have now
That doesn't sound right. Your buttons should be wired between the GPIO and GND. No 3.3V line involved anywhere. The GPIO's are pulled up by your code.
Careful which pins you use. IIRC pin 0 is a special purpose pin (BOOT) on ESP32.
Yeah, he could. Like you said, it's still crap, but at least it's a different flavor of crap.
On the first run of loop(), assuming no buttons are pressed, Pause=50 and there are 12x delay(), so that's 600ms in total, not 1000ms.
On further runs of loop(), assuming no buttons are pressed, Pause=0, so the 12x delay() will have (almost) no effect. The LEDs will probably all appear to the eye to glow at low brightness. In reality they are all flashing very briefly at full brightness and loop() will repeat hundreds maybe thousands of times each second.
If a button is pressed, let's say button on pin 1, Pause=500 and the interval would be 6000ms.
My guess is this: the button on pin 2 always reads high. Maybe all 3 buttons read HIGH. Then, Pause=1000 and the interval is 12000ms. But each individual LED is lit 1000ms after the previous one and maybe this is what you meant when you said that the period is 1000ms?
So maybe your buttons are incorrectly wired and read HIGH whether they are pressed or not.
I think you need to have another look at the code. With no buttons pressed, the pause is first set to 100, then to 500 and ultimately to 1000ms. The button presses are only registered in the brief moment (a few nanoseconds) at the start of loop. After that, it's all delays. So whatever button he presses, it's likely ignored anyway, unless it happens to be pressed just at the start of loop().
@Holzwurm56 what if you press and keep on holding one of your buttons? Does the period change then? Probably only if you press and hold the button on pin 2. If you press and hold the buttons on pins 1 and 2 at the same time, you may get a different period. If you press and hold all 3 buttons, then I suspect your LEDs will appear to glow dimly at the same time. Have you understood what is wrong yet?
I must be missing something, or perhaps post #1 has been edited after all the comments about the buttons being pulled up / default high.
That code suggests to me that the default state will be low. I assume the buttons are connected between the GPIO pin and Vcc, so they default to low and go high when a button is pressed.
Yes, it was edited. The original contained the lines I quoted in #7.
@Holzwurm56 as you can see, editing your post once people have already responded to it brings the risk of confusion. It's best to avoid this. If you want to post updated code, just reply to your own thread and insert the updated code.
Edit: disregard the bit in Italics above and please refer to @PaulRB's post below (#16)!
It's also code that won't do anything in particular on many Arduino boards since pulldown resistors are not present inside many microcontrollers (notably the ATMega series). On the ESP32, most GPIO's have pulldown resistors, but not all. @Holzwurm56 please take note of this. Also, the approach using pulldowns is something I'd not recommend. As said repeatedly before, INPUT_PULLUP is fine and just connect each button between the GPIO and GND. Switching over to INPUT_PULLDOWN is not going to fix the structural problems with your code, which remain the same in your updated post.
It doesn't seem to have been edited, although it is possible to edit a post within a couple of minutes of posting it without the forum indicating that an edit was done. The code you quoted in post #7 matches the original post.
I think we all missed that INPUT_PULLDOWN was used, until @Dave_Lowther spotted it.
I think I have found the error. INPUT_PULLDOWN does not work for every pin or not at all. I have now installed a pulldown resistor and it works. Thank you for any answer