[CART360] Daniel C. - Max -> Arduino Com

Elio Bidinost ebidinost at swpspce.net
Thu Oct 18 00:12:08 EDT 2007


Hi Daniel,

The LEDFADER and Arduino are now talking to each other. Currently, I  
am controlling all 6
PWM's. The code below is far from a "code complete" state, in  
particular the serialUnmarshall()
needs much refinement ....

elio


/*
* Unmarshalling a Max/MSP Message
* by Your Friendly Lab Tech ;)
*
* Prototype code to unmarshall incoming serial data used within the  
Arduino.
*
* Version 0.1a
* 17th October 2007
*/


// Unmarshalling Variables
byte inCnt = 0;
int serialIn = -1;
int sPinNumber = -1;

// Read analogRead(analogPins[0]);
int analogPins[] = {0, 1, 2, 3, 4, 5};

// Read digitalRead(digitalPins[2]); or Write digitalWrite(digitalPins 
[2], HIGH or LOW);
int digitalPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

// Write analogWrite(analogPins[0], someValue);
int pwmPins[] = {3, 5, 6, 9, 10, 11};

// Serial Communication Speed
long serialBAUD[] = {9600, 19200, 28800, 57600, 115200};

int ledPin = 13;   // select the pin for the LED


int getPin(int pin)
{
   int pinRet = -1;

   switch(pin)
   {
   case 0: // do something when A
     pinRet = 9;
     break;
   case 1:  // do something when B
     pinRet = 9;
     break;
   case 2:   // U
     pinRet = 9;
     break;
   case 3:  // X
     pinRet = 9;
     break;
   } // Switch END
}


/* Message Format:
* "110 1 1" is 3 Bytes Long
*/
void serialUnmarshall()
{
   if (Serial.available())
   {
     serialIn = Serial.read();

       //Serial.println(serialIn);
       if (serialIn == 110)
       {
         Serial.println(serialIn);
         sPinNumber = Serial.read();

         // Pusle Width Pin Selector
         if ((sPinNumber >= 0) && (sPinNumber < 6))
         {
              Serial.println(sPinNumber);
              //analogWrite(pwmPins[getPin(sPinNumber)], Serial.read());
              analogWrite(pwmPins[sPinNumber], Serial.read());
         }
       }
   }
}


void setup()
{
   Serial.begin(serialBAUD[4]);

   // Prep PWMPins for output....
   for (int i = 0; i < 6; i++)
   {
     pinMode(pwmPins[i], OUTPUT);
   }
}

void loop()
{
   // Check the serial port for incoming data
   serialUnmarshall();

   /*
   for (int i = 0; i < 255; i++)
   {
     analogWrite(pwmPins[3], i);
     delay(5);
   }

     for (int i = 255; i > 0; i--)
   {
     analogWrite(pwmPins[3], i);
     delay(5);
   }
   */

}





More information about the CART360 mailing list