Pages: [1]   Go Down
Author Topic: Is delay() necessary in loop()?  (Read 1529 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.
I am working on a project using the Arduino and Max/MSP to control light via DMX.
In the sketch, I need the Arduione to read incoming button presses, midi, and ethernet messages constantly.
Therefore, I would like to have a loop() that runs constantly, checking for incoming data of the
mentioned kinds (with minimum delay). My question is: is it necessary to have a delay(ms) in my loop function?
- will the Arduino overheat, crash, explode or something if I do not have a delay()?
I have pasted my code, at its current state below.
Best regards Jakob

Code:

#include <SPI.h>
#include <Ethernet.h>
#include <ArdOSC.h>
#include <DmxSimple.h>


// Define pin number for midi shield enable
#  define MIDI_EN 17

byte myMac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xFB, 0x00  };
byte myIp[]  = { 192, 168, 1, 177 };
int destPort=7400;
byte destIp[]  = { 192, 168, 1, 176 };
OSCClient client;
//create new osc message
OSCMessage global_mes;
OSCServer server;
int  serverPort  = 7401;

//Button
int buttonPressedLast = 0;

//Max connection
boolean hasConnection = false;
long lastPing = 0;

void setup(void)
{
  Serial.begin(31250); //Midi
  pinMode(MIDI_EN, OUTPUT);
  digitalWrite(MIDI_EN, HIGH);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  Ethernet.begin(myMac ,myIp);
  server.begin(serverPort);
  server.addCallback("DMX",&sendDMX);
  server.addCallback("ping",&pingReceived);
}


void loop()
{
  //Udp receive
  if(server.aviableCheck()>0)
  {
     //printToMax("Server available");
  }
 
 
  //Midi input handling
  uint8_t event, note, velocity;
  if (Serial.available())
  {
    event = Serial.read();
    if (event == 0xFE) return; // System activity heartbeat
    if ((event & 0xE0) == 0x80)
    { // Note-on event or note-off event
      while (! Serial.available()) /* NULL */ ;
      note = Serial.read();
      while (! Serial.available()) /* NULL */ ;
      velocity = Serial.read() ;
      sendNoteEvent(event, note, velocity);
    }
  }
 
  //Read analog in on A0, button presses
   int aIn = analogRead(A0);
   if(aIn > 100)
   {
      buttonPress(aIn);
   }
   else if (buttonPressedLast > 0)
   {
     digitalWrite(6, LOW);
     buttonOff(buttonPressedLast);
     buttonPressedLast = 0;
   }
   
   //If hasConnection, check if connection is lost
   if (hasConnection)
   {
     if ((millis() - lastPing) > 2000) //if 2 seconds have passed since last ping...
     {
       hasConnection = false;
       digitalWrite(5, LOW);
     }
   }
   //delay(some amount of ms ?);
}

void sendDMX(OSCMessage *_mes)
{
  int channel = _mes->getArgInt32(0);
  int value = _mes->getArgInt32(1);
  DmxSimple.write(channel, value);
  printToMax("DMX sent");
}

void printToMax(char str[])
{
    global_mes.setAddress(destIp,destPort);
    global_mes.beginMessage("print");
    global_mes.addArgString(str);
    client.send(&global_mes);
    global_mes.flush(); //object data clear
}

void buttonPress(int val)
{
  int button = 0;
 
  if (val >= 950)
  {
    button = 1;
  }
  else if (val >= 700)
  {
    button = 2;
  }
  else if (val >= 450)
  {
    button = 3;
  }
  else
  {
    button = 4;
  }
  if (button != buttonPressedLast)
  {
    digitalWrite(6, HIGH);
    if (buttonPressedLast != 0)
    {
      buttonOff(buttonPressedLast);
    }
    buttonPressedLast = button;
    global_mes.setAddress(destIp,destPort);
    global_mes.beginMessage("button");
    global_mes.addArgInt32(button);
    global_mes.addArgInt32(1);
    client.send(&global_mes);
    global_mes.flush(); //object data clear
  }
}

void buttonOff(int button)
{
  global_mes.setAddress(destIp,destPort);
  global_mes.beginMessage("button");
  global_mes.addArgInt32(button);
  global_mes.addArgInt32(0);
  client.send(&global_mes);
  global_mes.flush(); //object data clear
}

void pingReceived(OSCMessage *_mes)
{
  lastPing = millis();
  if (! hasConnection)
  {
    hasConnection = true;
    digitalWrite(5, HIGH);
  }
}

// Send note-on or note-off to Max
void sendNoteEvent(uint8_t event, uint8_t note, uint8_t velocity)
{
  // MIDI command 0x80 is note off...also accept note-on with velocity 0 as a note-off event
  if ( ((event & 0xF0) == 0x80) || (velocity==0) )   
  {
    global_mes.setAddress(destIp,destPort);
    global_mes.beginMessage("note");
    global_mes.addArgInt32(note);
    global_mes.addArgInt32(0);
    client.send(&global_mes);
    global_mes.flush(); //object data clear
  }
 
  else if ((event & 0xF0) == 0x90)
  { // Note on...should always be true by this point
     global_mes.setAddress(destIp,destPort);
    global_mes.beginMessage("note");
    global_mes.addArgInt32(note);
    global_mes.addArgInt32(velocity);
    client.send(&global_mes);
    global_mes.flush(); //object data clear
  }
}

Logged

Belgium
Offline Offline
Edison Member
*
Karma: 58
Posts: 1731
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Jacob
Delay does not make the Arduino consume less energy.
A delay is not needed.
A delay is used in many sample programs in combination with the serial communication to avoid the Serial monitor to scroll to fast.
In most of my programs there is no delay in the main loop.
I try to avoid delay as much as possible for performance reasons.
Best regards
Jantje
Logged

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 2
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you need a little hint about how to do things without a delay(), search for a sample called "blink without delay." Typically, delays are not needed or wanted.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your replies :-)
Best, Jakob
Logged

South Texas
Offline Offline
Edison Member
*
Karma: 8
Posts: 1023
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sometimes you need a short delay - Analog Reads need a slight delay between reading any 2 analog pins. You can achieve this with Delay, or by having some other code between the 2 analog reads. Unless you need the delay yuo don't have to put them in, the 328 is still running just as fast, doing just as much work whether you put the delay in or leave it out. If you put the delay in iot is just running ful speed processing the delay. If you leave it out the 328 is running at full speed executing your code. Either way the processor is doing the same amount of work and it doesn' t see any difference because that is all it does - execute code.
Logged

Pages: [1]   Go Up
Jump to: