Which serial devices are you using ?
Likely the code is not polling the button frequently enough.
I suggest declaring A0 as digital in INPUT_PULLUP and using digitaqlRead(button). Using analog is not good. All values from 1 to 1023 will be considered as HIGH and only 0 will be considered as LOW.
Thanks!
This is where it all originated:
Sorry it wont let me link to a page number, but it's #37 in the article.
The link opens up but how to get to page 37?
Know that artikels are often written by ambitious amateurs, and often contain designs being lucky for the creator but not for all viewers. It's quite usual members turn to Forum having trouble following "less good" or even crappy designs read somewhere.
Sorry it's so hard to get to.
You apparently have to use the right arrow to go to the table of contents, click on the article, go to the beginning of the article, and use the right arrow to go to #37.
In this case, it happened to be lucky for me, because it;'s worked quite well. Adding signals was easy.
I've spent time without success. Enough.
Looking at Your code I blame the delays for the button reading problem.
What if You press the button and hold it down until "something happens"?
If you just hold it down, it will simply cycle through opening and closing the turnout.
I has worked so well, I never considered it to be a "problem", but obviously it doesn't seem to be the best way to accomplish this, and I can learn much by fixing it.
I've saved a LOT of money buying cheap used manual Lionel Fastrack switches and putting a $2 servo on them.
Okey. Test if there's a difference between short and a little longer pressings?
As already posted, delays in the code are very often the reason for malfunctioning button registering.
There has always been a difference. If you just press the button quickly, it will not respond. If you hold it down for just a bit longer, it works.
Meanwhile, I was able to easily test:
int buttonRead = analogRead(button);
delay(200);
switch (switchState) {
and I can attest that changing delay(200) to delay(300) or more requires the button to held down longer be for it goes to what
switch (switchState)
may be at that time.
As I may have mentioned, I have had absolutely no problem or dissatisfaction with the actual functioning of the button.
Everything works great, but when I posted the code looking for ways to add the mp3 player (which at the moment only needs to play a steam whistle, a bell, and a diesel horn for a couple of seconds) there seems to be a lot of focus and attention paid to the presence of that A0 pin as analog.
I'd like to take good suggestions and improve it if I may, but it seems to work just fine, and has became a distraction from what I was really seeking advice on, which was adding the mp3 player to this particular sketch, which also has servos involved (more serial devices going on).
At the moment, I'm much more interested in avoiding conflicting serial baud rates, etc. than that button.
Thanks again, and I will be sure and remember to share this when I get everything working together.
An analogRead takes more time than a digitalRead.
How many different devices on your system use a serial interface and which pins are they using ?
We have 2 servos, one on data pins 2 and 3, and I wish to add the the Player, which uses data pins 18 and 19.
I'm assuming that under Setup
Serial.begin(9600)
will cover both devices
Thanks!
Servos don't usually use a serial interface. What type of servos are they, how are they connected to the Arduino and what commands do you use to control them ?
You're going on about "integrating" but then not integrating, because that's too hard, and ostensibly wanting "help", but all you've done is defend some poc from youtube where you're bogged down in unnecessary analogReads and the ever-popular delays.
Instead of wasting your time here you ought to chat up artie@artielangston because you just want to be validated in misunderstandings and stubbornness.
Probably missed something, but is there a reason you can't put the pushbutton on a interrupt pin??