Go Down

Topic: Push Button Counter (Read 8383 times) previous topic - next topic


not my code....

First, you need to start using Tools + Auto Format to format your code. Properly indented code is easy to read. That stuff you posted with the random indents is not.

Second, you need to adopt a consistent style. Either { goes on a new line (as I prefer) or it does not. Mixing styles makes for hard to read code.

Third, you really need to start helping yourself. For instance, use Serial.print() to print some information about countButton/countSwitch each time it is changed. Does it change when you expect, and only when you expect?

In my code, I only showed how to increment the value at the transitions, from released to pressed. You needed to add the code to limit the value to the range you desire. In my code, countSwitch counts 0, 1, 2, 3, 4, 5... You only have cases for 1 and 2.

What you should have cases for is odd or even or you should ensure that countSwitch never exceeds 2.


Another thought on how to "approcah" this requirement with the sketch and how it works.

With what you have said you want it to do:
It waits for the button to be pressed.
The "message number" is toggled between 0 and 1.
Depending on the value of "message number" a different message is sent on the TCP line.

But how to do all that.

Wait for the button to be pressed - ofcourse there is the problem of swtich bounce.
So, to remove bounce, you could then wait for the switch to be released.

Then act on what was set with the switch was pressed.
That means a bit more coding - but doesn't it always?
  For now, I won't go into that.  I have done test stuff with two wires for the switch and not had switch bounce.
(Funny that.)

But let us know how things are going with the latest sketch.

Note to others:
I am not trying to say in any way mine is better than yours.   There are MANY ways to do what he wants, and we are all putting our own perceived way of doing it.


Thank you lost_and_confused for your interest... i try your last sketch but unfortunately doesn't work.. when i push the button send infinity PH9 and the second push conntinue to send P9H


No problems.

Just a couple of thoughts:

PUSH - what does that mean to you?

A quick press, or a press and hold for about 3 seconds then release?

The sketch may "fail" if the button is down for a "while" because it will keep reseting the "loop_counter".
But once the button is up/released, it should only happen once more.

And, for the sake of making things a lot clearer, the two messages:
Make them VERY different to help people when "we" are quickly reading the reply.

For instance:  For the first message make it send "Button press 1" and when it is pressed the second time make it send "Button press 2" so it is clear there is not any other problems lurking.

The P9H and PH9 are confusing for me when I am reading and now thinking a bit more:
Seem to be the right way around.  So really it is extra information which isn't really needed.
The problem now as it seems is that somehow the messages are being sent continually.


Here is another effort to try and work out where the problem is:

Code: [Select]

#include <SPI.h>
#include <EthernetUdp.h>
#include <Ethernet.h>

#define BUTTON 7

int countButton = 0;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168,1, 177 };

unsigned int localPort = 7777;

byte remote_ip[] = { 192, 168,1, 100 };

unsigned int remote_port = 7777;

EthernetUDP Udp;

void setup() {





void loop()
int loop_counter;
    if(digitalRead(BUTTON)== 1) 
          //  Clear loop counter when the button is pressed.
          loop_counter = 0;
          countButton = countButton + 1;
          countButton = countButton % 2;
     if (digitalRead(Button) == 0)
     //  Now, this part of the code won't happen until the button is released.
         if (loop_counter == 0)
         //  This part will only happen if loop_counter is zero.
            loop_counter = 1;
            //  Set loop_counter to one so this code can only happen once
            //  In some ways this is not needed now because we are waiting for
            //  the button to be released before getting here.
            //  But it is better to be safe than sorry, and where you seem to be
            //  it is probably a good idea to do.
            switch (countButton) 
                case 1:
                case 2:


Hi lost_and_confused....excuse the delay, i try my your sketch...

i mean for "push" a quick press...the time required to press and release the button...
i try to be more precise and clear...
press 1 many messages P9H
press 2 many messages P9L
press 3 many messages P9H
press 4 many messages P9L

At this point...i think i have resign and i have to use 2 pin...


At this point...i think i have resign and i have to use 2 pin...

No, you don't. You simply need to use the method I showed, to detect a switch state change, not just the current switch state.
The art of getting good answers lies in asking good questions.



I'm just asking, but reading my sketch, why would he get multiple messages?

The code should stop that with the loop_counter variable.

What am *I* missing?



I'm just asking, but reading my sketch, why would he get multiple messages?

The code should stop that with the loop_counter variable.

What am *I* missing?

Your loop counter variable is local and not static, so the value isn't retained between the different iterations of the loop.


Code: [Select]
    if(digitalRead(BUTTON)== 1) 
The #defined names HIGH and LOW really should be used here, not constants.

Your code assumes that a switch press will not last longer than 30 microseconds. countButton will have an unpredictable value if the switch is held more than 30 microseconds.

30 microseconds is pretty good for debouncing, but is very short for a human to press and release a switch.

One really does need to detect the transition. There is just no other reliable method to get one action per press and release.
The art of getting good answers lies in asking good questions.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131