Pages: 1 [2]   Go Down
Author Topic: Push Button Counter  (Read 5453 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Edison Member
*
Karma: 29
Posts: 2445
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

not my code....

@cjdelphi
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.
Logged

Oz
Offline Offline
God Member
*****
Karma: 5
Posts: 737
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Oz
Offline Offline
God Member
*****
Karma: 5
Posts: 737
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Oz
Offline Offline
God Member
*****
Karma: 5
Posts: 737
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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() {

Ethernet.begin(mac,ip);

Udp.begin(localPort);

pinMode(BUTTON, INPUT);

}

void loop()
{
int loop_counter;
    if(digitalRead(BUTTON)== 1) 
    {
          //  Clear loop counter when the button is pressed.
          loop_counter = 0;
          delay(30);
          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:
                Udp.beginPacket(remote_ip,remote_port);
                Udp.write("P9H");
                Udp.endPacket();
                delay(250);
                break;
   
                case 2:
                Udp.beginPacket(remote_ip,remote_port);
                Udp.write("P9L");
                Udp.endPacket();
                delay(250);
                break;
            }
         }
     }
}
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Now
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...
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49101
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Oz
Offline Offline
God Member
*****
Karma: 5
Posts: 737
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Paul,

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?
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3379
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Paul,

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49101
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    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.
Logged

Pages: 1 [2]   Go Up
Jump to: