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

i would like to do this sequence...

when i push the button (first time) Pin do something (push button counter + 1)
when i push the button (second time) pin do something

reset push button counter

how can i do this..?
i try this sketch but don't work... errors are
UDP_write_pushbutton1.cpp: In function 'void loop()':
UDP_write_pushbutton1:57: error: jump to case label
UDP_write_pushbutton1:51: error: crosses initialization of 'char DataOut [4]'

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

#define BUTTON 7

int statoButton     = 0;      // stato del pulsante (inizialmente non premuto) 
int lastStatoButton = 0;      // ultimo stato del pulsante (per ora non premuto) 
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() {
 
  if(digitalRead(BUTTON)) 
  {   
    delay(15);   

    if(lastStatoButton==0) lastStatoButton=1; 
    else lastStatoButton=0; 
 
    if(countButton=0) countButton=countButton+1; 
    else countButton=0; 
  } 
   
  switch (countButton) 
  {
  case 1:
  Udp.beginPacket(remote_ip,remote_port);
  char DataOut[] = "P6H";
  Udp.write(DataOut);
  Udp.endPacket();
  delay(250);
  break;
 
  case 2:
  Udp.beginPacket(remote_ip,remote_port);
  char Data[] = "P6L";
  Udp.write(Data);
  Udp.endPacket();
  delay(250);
}
}


Logged

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

Code:
  if(countButton=0) countButton=countButton+1;
Don't you know the difference between = and ==? You should learn the difference.

Code:
  char Data[] = "P6L";
  Udp.write(Data);
as opposed to the simpler
Code:
  Udp.write("P6L");
?

You can avoid the error messages by enclosing the whole case statement in curly braces.
Logged

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

i'm sorry...you are right! I have to write == or <= or >=  ok... i modify... if(countButton=0)   in   if(countButton<=1)

and Udp.write("P6L"); and   i delete char variable

Now work but when i push the button send many udp messages i want to send only one message for push, is possible that?

Thanks  smiley
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1271
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You are sending many message because countbutton is 1. Every time through the loop you will be doing this.

Have you declared lastStatoButton for a reason? you do not appear to be using it and it may be the way to resolve your problem if you detect the change of state rather than the state itself.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

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

I changed the sketch ... and I only used the buttoncounter but as you said when you enter the loop sends many messages. i can't stop the loop...
maybe i have to use...if...else...
Logged

Oz
Offline Offline
God Member
*****
Karma: 4
Posts: 732
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nick,

So when I press the button the first time it does something, the next time it does.....  the same thing or something different?

What happens on the third press?  Does it do what was press number 1?

Post your new code please.

(Don't forget to put the [ code ] and [ /code ] commands  around it - without the spaces.)


Logged

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

I would like...
when i push the button (first time) arduino send the udp message P9H
when i push the same button (second time) arduino send the udp message P9L
restart
when i push the same button (third time) arduino send the udp message P9H
when i push the same button (fourth time) arduino send the udp message P9L

this is my code... now when i push the button (first time) arduino send INFINITY udp messages P9H
when i push the same button (second time) arduino send INFINITY udp messages P9L
restart
when i push the same button (third time) arduino send INFINITY udp messageS P9H
when i push the same button (fourth time) arduino send INFINITY udp messageS P9L

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() {
 
  if(digitalRead(BUTTON)) 
  {
    delay(30); 
    if(countButton<=1) countButton=countButton+1; 
    else countButton=0; 
  }   
  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

Oz
Offline Offline
God Member
*****
Karma: 4
Posts: 732
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this:

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 button_flag;
    if(digitalRead(BUTTON)) 
    {
          //
          button_flag = 1;
          delay(30);
          countButton = countButton + 1;
          countButton = countButton % 2;
      }
      if button_flag = 1
      {
        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;
        }
    }
    button_flag = 0;
}

I think you were falling over with the delays.  They maybe weren't long enough.
Also I simplified the counting of the button presses.
          countButton = countButton + 1;
          countButton = countButton % 2;
This will cycle countButton from 0 and 1 and back.
The % 2 is "Modulo 2" which means if gets the value and finds the remainder of dividing it by two.
So basically the answers are:  0, 1, 0, 1, 0, 1 and so on.

Button_flag is outside the loop so it will only be set to 0 once BUTTON is low.
That may help.
Logged

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

This is not good:
Code:
  if(digitalRead(BUTTON)) 
  {
    delay(30); 
    if(countButton<=1) countButton=countButton+1; 
    else countButton=0; 
  }   
Your requirements talk about pushing the switch n times, not incrementing as you hold the switch down.

You had a variable for the previous state of the switch. Put that back.
Code:
void loop()
{
   byte currState = digitalRead(SWITCH); // Buttons are for shirts; switches are for Arduinos
   if(currState != prevState)
   {
      if(currState == HIGH)
         countSwitch++;
   }
   prevState = currState;
}
Logged

Oz
Offline Offline
God Member
*****
Karma: 4
Posts: 732
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Drats!  Paul did a better job for not looping than I did.

Logged

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

i try the sketch posted by lost_and_confused but don't work..when i push the button send infinity P9H and the second push continue to send P9H

and i try this sketck with the part posted by PaulS..but when i push the button send infinity P9H, second push send infinity P9L and third push stop to send message and dont' restart.. is right the sketch?

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

#define SWITCH 7

int countSwitch = 0;
int prevState = 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(SWITCH, INPUT);

}

void loop()
{
   byte currState = digitalRead(SWITCH); // Buttons are for shirts; switches are for Arduinos
   if(currState != prevState)
   {
      if(currState == HIGH)
         countSwitch++;
 
   prevState = currState;
}
 
  switch (countSwitch) 
  {
  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
Edison Member
*
Karma: 29
Posts: 2441
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

#define BUTTON 7

int statoButton     = 0;      // stato del pulsante (inizialmente non premuto)
int lastStatoButton = 0;      // ultimo stato del pulsante (per ora non premuto)
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() {
 
  if(digitalRead(BUTTON))
  {   
    delay(15);   

    if(lastStatoButton==0) lastStatoButton=1;
    else lastStatoButton=0;
 
    if(countButton=0) countButton=countButton+1;
    else countButton=0;
  }
   
  switch (countButton)
  {
  case 1:
  {
  Udp.beginPacket(remote_ip,remote_port);
  char DataOut[] = "P6H";
  Udp.write(DataOut);
  Udp.endPacket();
  delay(250);
  break;
  }
  case 2:
  {
  Udp.beginPacket(remote_ip,remote_port);
  char Data[] = "P6L";
  Udp.write(Data);
  Udp.endPacket();
  delay(250);
  }
}
}

I should really go down and really all the comments  and code lol...

but all i did was add { } around case to make it compile.
Logged

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

ok cjdelphi...now Arduino don't send nothing  smiley

If i use this sketch...arduino does what i want but with 2 button! but i would like to do this only with a button...

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

#define BUTTON 7
#define BUTTON2 2
int  val = 0;
int  val2 = 0;

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x66, 0x7F };
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);
pinMode(BUTTON2, INPUT);

}

void loop() {
 
  val = digitalRead(BUTTON);
  val2 = digitalRead(BUTTON2);
 
  if (val == HIGH) {
  char DataOut[] = "P6H";
  Udp.beginPacket(remote_ip,remote_port);
  Udp.write(DataOut);
  Udp.endPacket();
  delay(250); 
}
  else if (val2 == HIGH) {
  Udp.beginPacket(remote_ip,remote_port);
  char Data[] = "P6L";
  Udp.write(Data);
  Udp.endPacket();
  delay(250);
}
}
Logged

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

@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: 4
Posts: 732
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi again.

I bashed that code and see I made a misakte.

What have I done?

READ THIS!

Originally I messed up with a counter  - called button_flag.
It is acutally a bad name - it is more a loop_counter.
So I have changed its name now.

When you press the button it is set to ZERO.

Then the sketch looks at loop_counter and only if it is ZERO, does it do the next part.
Then: loop_counter is set to ONE.
This stops the loop happening agian until the button is presssed and loop_counter is reset to ZERO.

I wasn't thinking too straight when I did the code, and like you I am new to the Arduino.

I hope this works for you now.



By the way, the indents:
If they are not the best, I am sorry, but for some reason notepad seems to have messed them up.

And I agree with what Paul said:
You need to find and stick to ONE way of writing your codes/sketches.  Mixing as you type is confusing for people reading it.



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)) 
    {
          //
          loop_counter = 0;
          delay(30);
          countButton = countButton + 1;
          countButton = countButton % 2;
     }
     if (loop_counter == 0)
     {
        loop_counter = 1;
        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;
        }
     }
}
[/code[

Logged

Pages: [1] 2   Go Up
Jump to: