Toggle UDP only once

Hi
I am building a flightsim control panel with a large number of toggle switches. while building and wiring are not a problem coding is . I am new to arduino which has been a steep learning curve but I have had some success.

Unlike most other button boxes or sim controllers I cannot use joystick type systems, the sim interface is UDP based. So far I have set up the sim computer side and tested by sending UDP from packetsender.

I have now figured out how to send the datagram over the network from the ardunio (Mega with ethernet shield) . The part I thought would be simple is proving to be more difficult, getting the code to send just once on moving the toggle switch. I have installed button.h library and edited the code down to 1 toggle switch which works in the serial monitor , but when I insert the UDP code it stops. messing with the braces I did get it to send but it keeps sending the data for on then off repeatedly rather than just once. Hear is the code I have so far. Any help would be greatly appreciated.

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

EthernetUDP Udp;
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x5C, 0x18}; // mac of ethernet shield
IPAddress ip(192, 168, 0, 54); // ip of Ardunio Mega
unsigned int localport = 8888;

IPAddress remoteIP(192, 168, 0, 52); // ip of target computer to send datagram too
unsigned int remotePort = 49260; // listening port of tatget computer

byte BattMasterOn[10] = {0x21, 0x52, 0xff, 0xff, 0x03, 0x10, 0x01, 0x00, 0x01}; // Datagram to send when switch is moved to on
byte BattMasterOff[10] = {0x21, 0x52, 0xff, 0xff, 0x03, 0x10, 0x01, 0x00, 0x00}; //Datagram to send when switch is moved to off

Button button1(7); // Connect your button between pin 7 and GND

void setup() {

button1.begin();
Serial.begin(9600);
Ethernet.begin(mac, ip);
Serial.print("IP : ");
Serial.println(Ethernet.localIP());
Udp.begin(localport);
}

void loop() {

if (button1.toggled())
{ if (button1.read() == Button::PRESSED)
Serial.println ("Master Battery On"); // Just to test it
Udp.beginPacket(remoteIP, remotePort);
Udp.write(BattMasterOn, 10); // Datagram for on
Udp.endPacket();
else
Serial.println("Master Battery Off"); // Just to test it
Udp.beginPacket(remoteIP, remotePort);
Udp.write (BattMasterOff, 10); // Datagram for off
Udp.endPacket();
}

}

SIM_Test.ino (1.5 KB)

use code tags see forum guide
try adding a few {..} e.g. I commented out the udp calls just for the test

void loop() {

  if (button1.toggled())
  {
  if (button1.read() == Button::PRESSED)   {   // <<<<<<<< added  
    Serial.println ("Master Battery On");       // Just to test it
 //   Udp.beginPacket(remoteIP, remotePort);
//    Udp.write(BattMasterOn, 10);               // Datagram for on
 //   Udp.endPacket();
  }          // << added 
  else   {   // <<<< added
    Serial.println("Master Battery Off");      // Just to test it
   // Udp.beginPacket(remoteIP, remotePort);
  //  Udp.write (BattMasterOff, 10);            // Datagram for off
  //  Udp.endPacket();
  }  //   << added
  }

}

a run gives

IP : 192.168.0.54
Master Battery On
Master Battery Off
Master Battery On
Master Battery Off
Master Battery On
Master Battery Off
Master Battery On
Master Battery Off
Master Battery On
Master Battery Off
Master Battery On
Master Battery Off

Many thanks for you help , It works perfect now, I have been trying to figure out this part for days. Now to scale it up to 9 switches for the first panel.

I have also found the 'copy for forum' option in IDE and the code tags.

Thanks again

Douglas

Your if() statement does not contain {} so only the following statement is connected to it. The following statements always get executed. If you auto format your code (CTRL-T) you will see the difference