Help testing code, I don't have access to Arduino for another 5 days!

I have been working on a project and think I have the code done, but I left my Arduino at a friends house and won’t be able to get it back for 5 days! I am asking if anyone here could take a look (or test) the code so I can know if it works. Or if not, where to start looking, without losing a week of testing and troubleshooting. I am VERY new to Arduino programming (I’m a HTML and CSS programmer normally :blush:) so I may have something extremely wrong, but it verifies the code so hoping that everything is OK.

The code runs on an Arduino UNO R3 with an Ethernet Shield with a button attached to digital pin 2. What it is supposed to do is listen for the button to be pressed (an emergency stop) and when it is pressed send a command over UDP that runs a Macro. And when the button is unpressed to send another command over UDP to run a reset Macro.

One part I am not sure about is; I want it to wait till the button state changes before it does anything, I only want the UDP commands to be sent once when the button changes.

If any questions or suggestions please respond, and thanks for the help! :smiley:

Code below:

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008

EthernetUDP Udp;// An EthernetUDP instance to let us send and receive packets over UDP

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

// Variables
const int  buttonPin = 2;   // the pin that the pushbutton is attached to
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  pinMode(buttonPin, INPUT);  // initialize the button pin as a input:
  Serial.begin(9600);  // initialize serial communication:
  Ethernet.begin(mac,ip); // start the Ethernet:
  Udp.begin(localPort); // start the UDP:
}


void loop() {
  buttonState = digitalRead(buttonPin);  // read the pushbutton input pin:
    if (buttonState == HIGH) {  // compare the buttonState to its previous state
  // if the current state is HIGH then send 911 command
      Serial.println("911!!");    
       // send the 911 command
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write("$Macro 911#");
    Udp.endPacket();
   delay(10);
}
    else {
      // if the current state is LOW then send 990 command
      Serial.println("All Safe"); 
       // send the safe command
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write("$Macro 900#");
    Udp.endPacket();
   delay(10);      
    } 
}

No need to even test the code. The requirements that you outline are not addressed.

I want it to wait till the button state changes before it does anything

You don't determine if a state change occurs. You only determine if the state is HIGH.

    if (buttonState == HIGH) {  // compare the buttonState to its previous state

This code does NOT do what the comment says.

Why are you not using the internal pullup resistor? Do you have an external resistor? Is it wired as a pullup resistor?

You need to record the previous state of the switch at the end of loop, so that, at the beginning, you can compare the current state to the previous state, to detect that a state change occurred. Then, you can determine whether the change was from released to pressed or from pressed to released, and react accordingly.

Thanks for that, I had the code in and removed it for some reason, and this just saved me a week of waiting!
I was planning on using an external resister, then looked into the internal resister so added that code also!

Thanks!

Updated code, though not sure if it works the same now or not :frowning: (Sorry forgot to update comments in code)

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008

EthernetUDP Udp;// An EthernetUDP instance to let us send and receive packets over UDP

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

// Variables
const int  buttonPin = 3;   // the pin that the pushbutton is attached to
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  pinMode(buttonPin, INPUT);  // initialize the button pin as a input:
digitalWrite(buttonPin, HIGH); // switch internal pull up
  Serial.begin(9600);  // initialize serial communication:
  Ethernet.begin(mac,ip); // start the Ethernet:
  Udp.begin(localPort); // start the UDP:
}


void loop() {
  buttonState = digitalRead(buttonPin);  // read the pushbutton input pin:
  if (buttonState != lastButtonState) {    // compare the buttonState to its previous state
    if (buttonState == HIGH) {    // if the current state is HIGH then send 911 command
      Serial.println("911!!");    
       // send the 911 command over UDP
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write("$Macro 911#");
    Udp.endPacket();
   delay(10);
   buttonPushCounter++;
    }
    else {
      Serial.println("All Safe");       // if the current state is LOW then send 990 command
       // send the safe command over UDP
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write("$Macro 900#");
    Udp.endPacket();
   delay(10);      
    } 
  lastButtonState = buttonState;    // save the current state as the last state, for next time through the loop
  }
}
    if (buttonState == HIGH) {    // if the current state is HIGH then send 911 command

When using the internal pullup resistor, HIGH is not pressed, and LOW is pressed.

If you put each { on a new line, and use Tools + Auto format, your code would be a lot easier to read.

  lastButtonState = buttonState;    // save the current state as the last state, for next time through the loop

This should be done at the end of loop, regardless of whether buttonState equals lastButtonState, or not.

Otherwise, that looks like it should work, assuming that your ethernet connection works, and you can send and receive UDP packets on the other end.

OK so change the button state to LOW to activate 911. I thought the lastButtonState = buttonState; section was already at the end of the loop, I assume you mean to place it at the VERY end of the loop, so move it down one }, correct?

How does this look,

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008

EthernetUDP Udp;// An EthernetUDP instance to let us send and receive packets over UDP

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

// Variables
const int  buttonPin = 2;   // the pin that the pushbutton is attached to
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() 
{
  pinMode(buttonPin, INPUT);  // initialize the button pin as a input:
  digitalWrite(buttonPin, HIGH); // switch internal pull up
  Serial.begin(9600);  // initialize serial communication:
  Ethernet.begin(mac,ip); // start the Ethernet:
  Udp.begin(localPort); // start the UDP:
}


void loop() 
{
  buttonState = digitalRead(buttonPin);  // read the pushbutton input pin:
  if (buttonState != lastButtonState) {    // compare the buttonState to its previous state
    if (buttonState == LOW) // if the current state is LOW then send 911 command
    {
      Serial.println("911!!");    
      // send the 911 command over UDP
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.write("$Macro 911#");
      Udp.endPacket();
      delay(10);
      buttonPushCounter++;
    }
    else 
    {
      Serial.println("All Safe");       // if the current state is LOW then send 990 command
      // send the safe command over UDP
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.write("$Macro 900#");
      Udp.endPacket();
      delay(10);      
    } 
  }
  lastButtonState = buttonState;    // save the current state as the last state, for next time through the loop
}

And thanks for your help, I can test the network and other settings when I get the Arduino back in a few days!

I assume you mean to place it at the VERY end of the loop, so move it down one }, correct?

Yes. That moves it out of the if (buttonState != lastButtonState) block.