Show Posts
Pages: [1]
1  Development / Other Software Development / Re: Is delay() necessary in loop()? on: May 31, 2012, 07:35:45 pm
Thanks for your replies :-)
Best, Jakob
2  Development / Other Software Development / Is delay() necessary in loop()? on: April 28, 2012, 10:35:20 am
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
  }
}

Pages: [1]