# Pausing code after my if statement

**URL:** <https://forum.arduino.cc/t/pausing-code-after-my-if-statement/882848>\
**Category:** Programming\
**Created:** [July 8, 2021, 4:08am UTC](https://forum.arduino.cc/t/pausing-code-after-my-if-statement/882848 "2021-07-08T04:08:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![keganmiven](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@keganmiven](https://forum.arduino.cc/u/keganmiven)\
**Post date:** [July 8, 2021, 4:08am UTC](https://forum.arduino.cc/t/pausing-code-after-my-if-statement/882848/1 "2021-07-08T04:08:18Z")

</div>

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:

```cpp
#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);
    
  }
}

```

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [July 8, 2021, 5:03am UTC](https://forum.arduino.cc/t/pausing-code-after-my-if-statement/882848/2 "2021-07-08T05:03:04Z")

</div>

@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](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966) where the use of code tags is described.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [July 8, 2021, 5:51am UTC](https://forum.arduino.cc/t/pausing-code-after-my-if-statement/882848/3 "2021-07-08T05:51:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![keganmiven](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@keganmiven](https://forum.arduino.cc/u/keganmiven)\
**Post date:** [July 8, 2021, 6:11am UTC](https://forum.arduino.cc/t/pausing-code-after-my-if-statement/882848/4 "2021-07-08T06:11:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [July 8, 2021, 6:49am UTC](https://forum.arduino.cc/t/pausing-code-after-my-if-statement/882848/5 "2021-07-08T06:49:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![keganmiven](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@keganmiven](https://forum.arduino.cc/u/keganmiven)\
**Post date:** [July 8, 2021, 7:13am UTC](https://forum.arduino.cc/t/pausing-code-after-my-if-statement/882848/6 "2021-07-08T07:13:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [July 8, 2021, 12:50pm UTC](https://forum.arduino.cc/t/pausing-code-after-my-if-statement/882848/7 "2021-07-08T12:50:55Z")

</div>

you will get more, faster and better help if

## you

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

---

<div class="post-metadata">

**Author:** ![keganmiven](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@keganmiven](https://forum.arduino.cc/u/keganmiven)\
**Post date:** [July 15, 2021, 12:38am UTC](https://forum.arduino.cc/t/pausing-code-after-my-if-statement/882848/8 "2021-07-15T00:38:25Z")

</div>

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!

```cpp
#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 );
}
```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 12, 2021, 12:38am UTC](https://forum.arduino.cc/t/pausing-code-after-my-if-statement/882848/9 "2021-11-12T00:38:59Z")

</div>

Closed on November 12, 2021, 12:38am UTC

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
