I corrected your logic by changing 1 word. What did I change?
The capacitor would go across the switch.
I corrected your logic by changing 1 word. What did I change?
The capacitor would go across the switch.
tl;dr: edit the pushbutton characteristics in the wokwi to include switch bounce.
I don't know what you are doing, and it seems neither do you. I cannot understand why you can't paste code into the sketch window.
The wokwi will not allow you to test adding a capacitor to solve a problem. Its analog chops are somewhat limited.
You can choose to reject the advice I linked in #72 above regarding the legitimacy of using one capacitor only to do.
Just use software. It's easy enough and at this point you should be able to glean from this thread enough to do it correctly. Or use ezButton as I recommended in #72 abkve.
Bit the very most important point everyone seems to be ignoring or overlooking is…
You are using magic switches in the simulation because somehow their ability to simulate the very real problem that you will face in the very real world has been turned off.
In the diagram window, select the pushbutton and in the dialog box you will see a checkbox labeled "Bounce". Click on it to, um, check it, that is to say to turn on the bounce simulation the part is capable of providing.
I don't know when it started, but it has spread unchecked and unchecked - y'all are riffing off a crippled diagram, or doing something in your sleep or wokwi has changed to default to perfect and perfectly unrealistic pushbutton behaviour. Listed in order of probability.
Fix it. See how it breaks the code. Get ready for more fun when you try to make something work IRL. In this case, what is usually a boon has seemed to be counterproductive. The simulator is only good for some things. I can usually do all the software and most of the diagram in the simulator before touching or even buying parts, but it may only be because I e touched and bought lotsa parts over some long time, never mind precisely how long.
a7
Do not use wokwi simulation, it does not work well in combination with .h header files.
Yust do it with a real arduino, even with an old tactile switch.
Facing the real world is a better challenge than some simulation effort.
If you have any problems using Tabs or header files in the arduino Ide, let me know and I will make 1 arduino.ino file that you can upload.
Every hobbyist has a spare Uno, some leds and a push button lying around.
Nonsense.
The wokwi may have its challenges and limitations, but it certainly is fully ably to deal with .h header files.
I put your code into the wokwi with its header file; I have wokwi projects with multiple tabs *.h, *.cpp and *.ino and…
… since it uses the same system for building code, the results are as expected.
a7
I didn't sign up for an account on Wokwi; I don't need another site that lets me sign up for an account, then makes it very difficult to close the account. Since I didn't sign up, that function doesn't work. As to your assertion regarding the circuit, I didn't design it from scratch, used input from several posters to create what I posted, and have no clue what the abilities are regarding the Wokwi site. All I know is the circuit designed on the site worked after several changes were made to result in the behavior I wanted; start simulation, green leds are lit. Press the button, green goes off and red comes on. Press it again and red goes off and green goes on. Process repeats for each button press. There was no capacitor in that circuit, there was a small issue with the code that made it necessary to hook up the red an green LEDs to the opposite pins that were designated in the code. I have since found the issue in the code; line 21 if (buttonState == HIGH && lastButtonState == LOW) {
was reversed to
line 21 if (buttonState == LOW && lastButtonState == HIGH) {
then the green LEDs would be on when simulation started with the green and red LEDs connected to the correct pins. Another poster pointed this out, but corrected it incorrectly;
line 21 if (buttonState == LOW && lastButtonState == LOW) {
resulting in the pattern only repeating once, then getting stuck on the same LEDs with each additional button press.
Line 21. But it was corrected incorrectly.
line 21 if (buttonState == HIGH && lastButtonState == LOW) {
is what it should be. But you had
line 21 if (buttonState == LOW && lastButtonState == LOW) {
which resulted in the switch only happening once, then getting stuck on the same LEDs with each additional button press.
It was pointed out to me where the issue was. On line 21, HIGH and LOW needed to be switched, but the poster incorrectly made them both LOW, resulting in the simulation getting stuck on the same LED after the second button press.
Also, I am having problems with the suggestion that I should add a 0.1 uF capacitor to the circuit, but Wokwi doesn't have a capacitor as an option in the parts list, and I don't know where in the circuit it needs to be added.
Where exactly across the switch would the capacitor go? Since the Wokwi site doesn't have capacitor as a parts option, you could take the image and draw in Paint where it should go.
Got it backward.
line 21 if (buttonState == LOW && lastButtonState == HIGH) {
is what it should be. Too many cooks in the kitchen throwing ingredients every which way, confusing the issue. Well meaning, but still confusing.
If you can type in the sketch window, you should be able to paste there. This has been my experience when I am not logged in. Right now I am at a workstation that doesn't know me at all. I am as about as far away from signed in at wokwi as you can get. So I begin to wonder why you cannot.
Please post links to your simulations, pictures are worth less to the point of being worthless.
I recommend you stop fussing with a hardware hack to fix the faulty software you've been saddled with. Three days to toggle an LED with a pushbutton has to be some kind of record and my help doesn't seem to be. Helping.
So I wish you luck. You are very close to something that will work. And there will be no arguing with that.
a7
All I know is when I tried to paste the code, I get a popup box that states I have to signup for an account to do it.
The following link is for the simulation that does work as intended, with the correction in the code so the green LEDs are on when simulation starts, and switches from green to red, then from red to green with each button press.
Don't know why it works; I have been told it shouldn't work. If so, then the simulation site is pretty much worthless.
Also, I need to know which components I need to order to build the circuit.
All I can think is that somehow you have missed my repeated statements concerning the simulator's ability either to use perfect switches or to model in a useful way the real world behaviour that should be expected and accounted for, however, when using real world switches.
You have the bouncing characteristic turned off. I know neither who unchecked that box nor when, but it has been traveling around and misleading ppl for some time.
You also must have skipped or failed to appreciate
Check the box, run the project, see the failure.
I can't now, but just to make this stop I will add the if statement that will make this work purely with software when I am in the lab. Soon-ish enough.
a7
Sounds great. Thanks.
Here is the first of two solutions. I use two LEDs, a second can be added in parallel or series IRL to make each a pair. Series is better, maybe, but the wokwi (someone said, so...) doesn't like that. In series two LEDs would share one series current-imiting resistor.
// https://wokwi.com/projects/420103535976304641
// https://forum.arduino.cc/t/toggling-between-two-pairs-of-leds/1341607
auto PRESSED = LOW;
const int greenLED = 12;
const int redLED = 10;
const int button = 2;
bool greenNow;
bool lastButtonState;
void setup() {
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(button, INPUT_PULLUP);
digitalWrite(greenLED, HIGH);
digitalWrite(redLED, LOW);
greenNow = true;
lastButtonState = digitalRead(button) == PRESSED;
}
void loop() {
bool buttonState = digitalRead(button) == PRESSED;
if (buttonState != lastButtonState) {
if (buttonState) {
greenNow = !greenNow;
digitalWrite(greenLED, greenNow ? HIGH : LOW);
digitalWrite(redLED, greenNow ? LOW : HIGH);
}
lastButtonState = buttonState;
delay(25);
}
}
a7
Here is the second solution using ezButton. See how EZ it is.
// https://wokwi.com/projects/420120497546844161
// https://forum.arduino.cc/t/toggling-between-two-pairs-of-leds/1341607
# include <ezButton.h>
const byte greenLed = 12;
const byte redLed = 10;
ezButton button(2);
bool greenNow;
void setup() {
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
greenNow = true;
button.setDebounceTime(21);
}
void loop() {
button.loop();
if (button.isPressed()) {
greenNow = !greenNow;
digitalWrite(greenLed, greenNow ? HIGH : LOW);
digitalWrite(redLed, greenNow ? LOW : HIGH);
}
}
HTH
a7
Post #37.
Thanks.. Tom...
You shoukd have quit then, instead of going on to posting a faulty hand-made solution!
Those nice new tacts you got might not always open cleanly.
a7
And give the op a free bit of code, and learn nothing.
By the way that code did work in the real world, no bounce, new tactile buttons.
Thanks.. Tom...