Pausing code after my if statement

Hi guys. I made this code to send UDP via ethernet with an ethernet shield. to send a packet to a to a cue software to tell it which cue to to "go" depending on what distance the viewer is at. I'm using an ultrasonic sensor and since the ultrasonic sensor is constantly sending data when the viewer is in the same position the arduino will constantly be sending a packet to the software which is not what I want. I used delays to stop the arduino from sending too many packets but I basically just want the arduino to send a packet once until the distance is changed. So even if the viewer stays at a distance, only one packet will be sent. This is my code:

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

EthernetUDP Udp;

//the Arduino's IP
IPAddress ip(192, 168, 1, 21 );  
//destination IP
IPAddress outIp(192, 168, 1, 20);
const unsigned int outPort = 53000;
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // you can find this written on the board of some Arduino Ethernets or shields

const int trig = 9;
const int echo = 8;
int duration = 0;
int distance = 0;

void setup() 
{
  Ethernet.begin(mac,ip);
  Udp.begin(53000);
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);
  Serial.begin(9600);

}

void loop()
{
  digitalWrite(trig , HIGH);
  delayMicroseconds(1000);
  digitalWrite(trig , LOW);


  duration = pulseIn(echo , HIGH);
  distance = (duration/2) / 28.5 ;
  Serial.println(distance);
  

  if ( distance <= 7 )
  { 
    delay(1);
    //insert message here
    OSCMessage msg("/cue/1/start");
    msg.add((int32_t)analogRead(0));
  
    Udp.beginPacket(outIp, outPort);
    msg.send(Udp); // send the bytes to the SLIP stream
    Udp.endPacket(); // mark the end of the OSC Packet
    msg.empty(); // free space occupied by message
    delay(2600);
    
  }
 
  else if ( distance <= 15 )
  { 
    delay(1);
    //insert message here
    OSCMessage msg("/cue/2/start");
    msg.add((int32_t)analogRead(0));
  
    Udp.beginPacket(outIp, outPort);
    msg.send(Udp); // send the bytes to the SLIP stream
    Udp.endPacket(); // mark the end of the OSC Packet
    msg.empty(); // free space occupied by message
    delay(3000);
    
    
  }
  else if ( distance <= 23 )
  {  
    delay(1);
    //insert message here
    OSCMessage msg("/cue/3/start");
    msg.add((int32_t)analogRead(0));
  
    Udp.beginPacket(outIp, outPort);
    msg.send(Udp); // send the bytes to the SLIP stream
    Udp.endPacket(); // mark the end of the OSC Packet
    msg.empty(); // free space occupied by message
    delay(6600);
    
   
  }
 
  else if ( distance <= 31 )
  {
    delay(1);
    //insert message here
    OSCMessage msg("/cue/4/start");
    msg.add((int32_t)analogRead(0));
  
    Udp.beginPacket(outIp, outPort);
    msg.send(Udp); // send the bytes to the SLIP stream
    Udp.endPacket(); // mark the end of the OSC Packet
    msg.empty(); // free space occupied by message
    delay(7000);
    
  }
}

@keganmiven, please edit your post, select all code and click the </> button to apply code tags; next save your post. That way we don't have to look at things like byte mac[] but it will be properly rendered as byte mac[] and indentations will be preserved. It also makes it easier to read and copy.

Next please read How to get the best out of this forum where the use of code tags is described.

1 Like

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

1 Like

Thanks guys. I've edited my post. Sorry about that.

1 Like

You define a new variable of type boolean.
After sending the first UDP-message the variable gets set to true

You code an if-condition that makes sending UDP-message dependend on that boolean variable

if( ! UDP_Msg_sended) {
sendUDP-message
}

if the distance changes the variable gets set false again

Is this rough description enough to get along or do you have more questions?
best regards Stefan

Hey Stefan. Thanks for the help. I've come up with something like this but am not sure where exactly to place it in my code. Thanks again.

you will get more, faster and better help if

you

post your best attempt how you tried to code it
best regards Stefan

Hey Stefan, Sorry about the long wait. Haven't had any time to touch my arduino up till now. I just managed to do it. Thank you for your help!

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

EthernetUDP Udp;

//the Arduino's IP
IPAddress ip(192, 168, 1, 21 );
//destination IP
IPAddress outIp(192, 168, 10, 118);
const unsigned int outPort = 53000;
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
}; // you can find this written on the board of some Arduino Ethernets or shields

const int trig = 9;
const int echo = 8;
int duration = 0;
int distance = 0;
int cueNumber = 0;

void setup()
{
  Ethernet.begin(mac, ip);
  Udp.begin(53000);
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);
  Serial.begin(9600);

}

void loop()
{
  digitalWrite(trig , HIGH);
  delayMicroseconds(1000);
  digitalWrite(trig , LOW);


  duration = pulseIn(echo , HIGH);
  distance = (duration / 2) / 28.5 ;
  Serial.println(distance);



  if ( distance <= 7 )
  { if ( cueNumber != 1 )
    {
      delay(1);
      //insert message here
      OSCMessage msg("/cue/1/start");
      msg.add((int32_t)analogRead(0));

      Udp.beginPacket(outIp, outPort);
      msg.send(Udp); // send the bytes to the SLIP stream
      Udp.endPacket(); // mark the end of the OSC Packet
      msg.empty(); // free space occupied by message
      cueNumber = 1;
      delay(2600);
    }
  }

  else if ( distance <= 15 )
  { if ( cueNumber != 2 )
    {
      delay(1);
      //insert message here
      OSCMessage msg("/cue/2/start");
      msg.add((int32_t)analogRead(0));

      Udp.beginPacket(outIp, outPort);
      msg.send(Udp); // send the bytes to the SLIP stream
      Udp.endPacket(); // mark the end of the OSC Packet
      msg.empty(); // free space occupied by message
      cueNumber = 2;
      delay(3000);
    }
  }



  else if ( distance <= 23 )
  { if ( cueNumber != 3 )
    {
      delay(1);
      //insert message here
      OSCMessage msg("/cue/3/start");
      msg.add((int32_t)analogRead(0));

      Udp.beginPacket(outIp, outPort);
      msg.send(Udp); // send the bytes to the SLIP stream
      Udp.endPacket(); // mark the end of the OSC Packet
      msg.empty(); // free space occupied by message
      cueNumber = 3;
      delay(6600);

    }
  }

  else if ( distance <= 31 )
  { if ( cueNumber != 4 )
    {
      delay(1);
      //insert message here
      OSCMessage msg("/cue/4/start");
      msg.add((int32_t)analogRead(0));

      Udp.beginPacket(outIp, outPort);
      msg.send(Udp); // send the bytes to the SLIP stream
      Udp.endPacket(); // mark the end of the OSC Packet
      msg.empty(); // free space occupied by message
      cueNumber = 4;
      delay(7000);

    }
  }
  else ( cueNumber = 0 );
}