<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Arduino Serial protocol design patterns</title>
	<atom:link href="http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/</link>
	<description>Random experiments, circuits, code, rapid prototyping, sometimes things to buy, and the odd tune by Tod E. Kurt.</description>
	<lastBuildDate>Fri, 03 Feb 2012 22:54:38 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: todbot</title>
		<link>http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/comment-page-2/#comment-79475</link>
		<dc:creator>todbot</dc:creator>
		<pubDate>Tue, 10 Jan 2012 18:54:39 +0000</pubDate>
		<guid isPermaLink="false">http://todbot.com/blog/?p=601#comment-79475</guid>
		<description>Hi Tim,
What does your code look like that is sending data to your Arduino?</description>
		<content:encoded><![CDATA[<p>Hi Tim,<br />
What does your code look like that is sending data to your Arduino?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tim castelijn</title>
		<link>http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/comment-page-2/#comment-79468</link>
		<dc:creator>tim castelijn</dc:creator>
		<pubDate>Sat, 07 Jan 2012 14:10:00 +0000</pubDate>
		<guid isPermaLink="false">http://todbot.com/blog/?p=601#comment-79468</guid>
		<description>hi guys, 

I&#039;ve read kasper and todbot&#039;s posts about the leds. I am trying to store 4 values in the arduino as well. I am using the code below, in which i expect to see the value for xStep printed. Instead of that I always get 32 returned by the serialmonitor, does anyone know why?

cheers, tim

&lt;code&gt;
#define stepPin 26
#define dirPin 28
#define enablePin 24

byte cmd;
byte xStep;
byte yStep;
byte zStep;
int i = 0;

void setup()
{
  // We set the enable pin to be an output
  pinMode(enablePin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);

  digitalWrite(enablePin, LOW);
  digitalWrite(dirPin, LOW);
  digitalWrite(stepPin, LOW);
  
  Serial.begin(9600);
  Serial.print(&quot;Program Initiated\n&quot;);
}


void loop() {
  if( Serial.available() == 4 ) {
    cmd = Serial.read();
    xStep = Serial.read();
    yStep = Serial.read();
    zStep = Serial.read();
    Serial.println(xStep);
  }


  for(i=0; i&lt;xStep; i++){
  stepx_p();
  delay(10000);
  }

}

void stepx_p(){
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2);
}
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>hi guys, </p>
<p>I&#8217;ve read kasper and todbot&#8217;s posts about the leds. I am trying to store 4 values in the arduino as well. I am using the code below, in which i expect to see the value for xStep printed. Instead of that I always get 32 returned by the serialmonitor, does anyone know why?</p>
<p>cheers, tim</p>
<p><code><br />
#define stepPin 26<br />
#define dirPin 28<br />
#define enablePin 24</p>
<p>byte cmd;<br />
byte xStep;<br />
byte yStep;<br />
byte zStep;<br />
int i = 0;</p>
<p>void setup()<br />
{<br />
  // We set the enable pin to be an output<br />
  pinMode(enablePin, OUTPUT);<br />
  pinMode(stepPin, OUTPUT);<br />
  pinMode(dirPin, OUTPUT);</p>
<p>  digitalWrite(enablePin, LOW);<br />
  digitalWrite(dirPin, LOW);<br />
  digitalWrite(stepPin, LOW);</p>
<p>  Serial.begin(9600);<br />
  Serial.print("Program Initiated\n");<br />
}</p>
<p>void loop() {<br />
  if( Serial.available() == 4 ) {<br />
    cmd = Serial.read();<br />
    xStep = Serial.read();<br />
    yStep = Serial.read();<br />
    zStep = Serial.read();<br />
    Serial.println(xStep);<br />
  }</p>
<p>  for(i=0; i&lt;xStep; i++){<br />
  stepx_p();<br />
  delay(10000);<br />
  }</p>
<p>}</p>
<p>void stepx_p(){<br />
    digitalWrite(stepPin, HIGH);<br />
    delayMicroseconds(2);<br />
    digitalWrite(stepPin, LOW);<br />
    delayMicroseconds(2);<br />
}<br />
</code><code></code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Martin</title>
		<link>http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/comment-page-2/#comment-78549</link>
		<dc:creator>Martin</dc:creator>
		<pubDate>Mon, 28 Nov 2011 18:45:22 +0000</pubDate>
		<guid isPermaLink="false">http://todbot.com/blog/?p=601#comment-78549</guid>
		<description>&quot;How to pass float data ?&quot;
The arduino converts the value according to what it thinks it is, so declaring it a float arduino-side should work just fine..


NICE post! far the most usable I have seen the past few days on the subject, tnx.. Btw making me a wifi robot :)</description>
		<content:encoded><![CDATA[<p>&#8220;How to pass float data ?&#8221;<br />
The arduino converts the value according to what it thinks it is, so declaring it a float arduino-side should work just fine..</p>
<p>NICE post! far the most usable I have seen the past few days on the subject, tnx.. Btw making me a wifi robot :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: LABAT</title>
		<link>http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/comment-page-2/#comment-78153</link>
		<dc:creator>LABAT</dc:creator>
		<pubDate>Wed, 31 Aug 2011 08:28:19 +0000</pubDate>
		<guid isPermaLink="false">http://todbot.com/blog/?p=601#comment-78153</guid>
		<description>Hi, very good post. I try to make an RC plane where serial communication is very important.
I wrote this
&lt;pre&gt;
 long start_time = millis();
 
 while(Serial.available()){

   // Permet de rendre la main au reste du programme
     if((millis() - start_time) &gt; 10 )  { // wait at most 10 ms
          break;
        }
      
      // Lecture du byte arrivé  
      byte theByte = Serial.read();
      //Nouvelle trame et que nous sommes pas dans la construction d&#039;une trame ou la trame met trop longtemps a arrivé
      if((theByte == START_TRAME &amp;&amp; newTrame == false) &#124;&#124; (millis() - DEB_START_TRAME&gt;OUT_OF_TIME_TRAME))
      {
        newTrame = true;  
        nbBuf = -1;
        DEB_START_TRAME = millis();
      }
      else {  
        // Cas ou nous sommes en construction d&#039;une trame
        if(nbBuf&gt;=BUFFER_SIZE)
        {
          // Cas ou le buffer et plein mais nous il ne s&#039;agit pas de la fin de la trame
          // alors on sait que la trame est corrompu pas besoin de la traiter
          if(theByte != END_TRAME)
          {
             newTrame = true; 
             nbBuf = -1;
             
             for(int i = 0;i&lt;BUFFER_SIZE;i++)
             {
               Serial.print(cmdbuf[i]);
             }
             Serial.println(&quot; Erreur de trame - &quot;);
          }
          else {
             // Verification du checksum car la trame est valide
             // On pointe sur le pointeur cmdbuf
   
              if(cmdbuf[11] == checkSum())
              {
                // Checksum ok
                      Serial.println(&quot;Checksum ok&quot;);
                    // Ici la trame est ok donc on peux executer une commande en call back  
                    Serial.print(&quot;Commande : &quot;);
                    Serial.print(cmdbuf[0]);
                    long val  = ((long )cmdbuf[9]) &lt;&lt; 8;
                    val &#124;= cmdbuf[10];
                    Serial.print(&quot;Val : &quot;);
                    Serial.print(cmdbuf[9],DEC);
              }
              else {
                // Checksum error $A0000000080I%
                  Serial.print(&quot;Erreur dans le check sum : &quot;);
                  Serial.println(cmdbuf[11],DEC);
                  Serial.print(&quot;Compute checksum : &quot;);
                
                  Serial.println(checkSum(),DEC);
              }
          }
          break; 
        }
        else {
         //Decodage d&#039;une trame en cours 
          cmdbuf[++nbBuf] = theByte;
          
          Serial.print(nbBuf,DEC);
          Serial.print(&quot; - &quot;);
          Serial.println(theByte);
        } 
      }
  }
&lt;/pre&gt;

In human readable way, the packet is : $A0000000080I%
where $ start trame
A command
0000000080 Data
I Checksum
% end trame

How to pass float data ?</description>
		<content:encoded><![CDATA[<p>Hi, very good post. I try to make an RC plane where serial communication is very important.<br />
I wrote this</p>
<pre>
 long start_time = millis();

 while(Serial.available()){

   // Permet de rendre la main au reste du programme
     if((millis() - start_time) &gt; 10 )  { // wait at most 10 ms
          break;
        }

      // Lecture du byte arrivé
      byte theByte = Serial.read();
      //Nouvelle trame et que nous sommes pas dans la construction d'une trame ou la trame met trop longtemps a arrivé
      if((theByte == START_TRAME &amp;&amp; newTrame == false) || (millis() - DEB_START_TRAME&gt;OUT_OF_TIME_TRAME))
      {
        newTrame = true;
        nbBuf = -1;
        DEB_START_TRAME = millis();
      }
      else {
        // Cas ou nous sommes en construction d'une trame
        if(nbBuf&gt;=BUFFER_SIZE)
        {
          // Cas ou le buffer et plein mais nous il ne s'agit pas de la fin de la trame
          // alors on sait que la trame est corrompu pas besoin de la traiter
          if(theByte != END_TRAME)
          {
             newTrame = true;
             nbBuf = -1;

             for(int i = 0;i&lt;BUFFER_SIZE;i++)
             {
               Serial.print(cmdbuf[i]);
             }
             Serial.println(&quot; Erreur de trame - &quot;);
          }
          else {
             // Verification du checksum car la trame est valide
             // On pointe sur le pointeur cmdbuf

              if(cmdbuf[11] == checkSum())
              {
                // Checksum ok
                      Serial.println(&quot;Checksum ok&quot;);
                    // Ici la trame est ok donc on peux executer une commande en call back
                    Serial.print(&quot;Commande : &quot;);
                    Serial.print(cmdbuf[0]);
                    long val  = ((long )cmdbuf[9]) &lt;&lt; 8;
                    val |= cmdbuf[10];
                    Serial.print(&quot;Val : &quot;);
                    Serial.print(cmdbuf[9],DEC);
              }
              else {
                // Checksum error $A0000000080I%
                  Serial.print(&quot;Erreur dans le check sum : &quot;);
                  Serial.println(cmdbuf[11],DEC);
                  Serial.print(&quot;Compute checksum : &quot;);

                  Serial.println(checkSum(),DEC);
              }
          }
          break;
        }
        else {
         //Decodage d&#039;une trame en cours
          cmdbuf[++nbBuf] = theByte;

          Serial.print(nbBuf,DEC);
          Serial.print(&quot; - &quot;);
          Serial.println(theByte);
        }
      }
  }
</pre>
<p>In human readable way, the packet is : $A0000000080I%<br />
where $ start trame<br />
A command<br />
0000000080 Data<br />
I Checksum<br />
% end trame</p>
<p>How to pass float data ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: FRancois</title>
		<link>http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/comment-page-2/#comment-78151</link>
		<dc:creator>FRancois</dc:creator>
		<pubDate>Wed, 31 Aug 2011 05:29:03 +0000</pubDate>
		<guid isPermaLink="false">http://todbot.com/blog/?p=601#comment-78151</guid>
		<description>Hi, very intresting post. I making Rc plane controle by Arduino and Xbee i try to make a light protocol. How could i do to send over serial floar data like lator lng gps data. Could you help me. Protocol seems like this
Start frame 1 byte
Command 1 byte
Data 10 bytes
Checksum 1 byte
End frame 1 byte

But 10 bytes to pass latitudelike -2.153627728 is a good idea?

Thanks</description>
		<content:encoded><![CDATA[<p>Hi, very intresting post. I making Rc plane controle by Arduino and Xbee i try to make a light protocol. How could i do to send over serial floar data like lator lng gps data. Could you help me. Protocol seems like this<br />
Start frame 1 byte<br />
Command 1 byte<br />
Data 10 bytes<br />
Checksum 1 byte<br />
End frame 1 byte</p>
<p>But 10 bytes to pass latitudelike -2.153627728 is a good idea?</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arduino-EMC Integration: How it Works &#124; Just Milling Around</title>
		<link>http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/comment-page-2/#comment-76377</link>
		<dc:creator>Arduino-EMC Integration: How it Works &#124; Just Milling Around</dc:creator>
		<pubDate>Sun, 21 Nov 2010 21:17:34 +0000</pubDate>
		<guid isPermaLink="false">http://todbot.com/blog/?p=601#comment-76377</guid>
		<description>[...] The best way (I think) is to structure everything as fixed-length messages along the lines of this example. Before you try to talk with EMC, write a Python program that sends and receives messages from the [...]</description>
		<content:encoded><![CDATA[<p>[...] The best way (I think) is to structure everything as fixed-length messages along the lines of this example. Before you try to talk with EMC, write a Python program that sends and receives messages from the [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: todbot</title>
		<link>http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/comment-page-2/#comment-72968</link>
		<dc:creator>todbot</dc:creator>
		<pubDate>Thu, 26 Aug 2010 01:22:39 +0000</pubDate>
		<guid isPermaLink="false">http://todbot.com/blog/?p=601#comment-72968</guid>
		<description>Hi ryuujin,
There is the possibility for the lockup you fear when doing something like: &lt;pre&gt;
while( Serial.available() &lt; 5 ) {  // wait for 5 bytes
  // do nothing
}
&lt;/pre&gt;
If the host never sends 5 characters, it&#039;ll just hang.  One wait to get around this is to have a timeout: &lt;/pre&gt;&lt;pre&gt;
long start_time = millis();
while( Serial.available() &lt; 5 ) {       // wait for 5 bytes
  if( (millis() - start_time) &lt; 3000 )  // wait at most 3 seconds
    break;
}
&lt;/pre&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi ryuujin,<br />
There is the possibility for the lockup you fear when doing something like:
<pre>
while( Serial.available() < 5 ) {  // wait for 5 bytes
  // do nothing
}
</pre>
<p>If the host never sends 5 characters, it'll just hang.  One wait to get around this is to have a timeout: </pre>
<pre>
long start_time = millis();
while( Serial.available() < 5 ) {       // wait for 5 bytes
  if( (millis() - start_time) < 3000 )  // wait at most 3 seconds
    break;
}
</pre>
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: ryuujin</title>
		<link>http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/comment-page-2/#comment-72963</link>
		<dc:creator>ryuujin</dc:creator>
		<pubDate>Thu, 26 Aug 2010 00:04:45 +0000</pubDate>
		<guid isPermaLink="false">http://todbot.com/blog/?p=601#comment-72963</guid>
		<description>no, sorry... I mistaked!!! :)</description>
		<content:encoded><![CDATA[<p>no, sorry&#8230; I mistaked!!! :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ryuujin</title>
		<link>http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/comment-page-2/#comment-72958</link>
		<dc:creator>ryuujin</dc:creator>
		<pubDate>Wed, 25 Aug 2010 22:25:45 +0000</pubDate>
		<guid isPermaLink="false">http://todbot.com/blog/?p=601#comment-72958</guid>
		<description>but
while( Serial.available() &amp;&amp; c!= &#039;\n&#039; ) {
requires that Arduino while is waiting for the endline, cannot do anything other.

I think it is a big problem to solve.

r.</description>
		<content:encoded><![CDATA[<p>but<br />
while( Serial.available() &amp;&amp; c!= &#8216;\n&#8217; ) {<br />
requires that Arduino while is waiting for the endline, cannot do anything other.</p>
<p>I think it is a big problem to solve.</p>
<p>r.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: todbot</title>
		<link>http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/comment-page-2/#comment-72557</link>
		<dc:creator>todbot</dc:creator>
		<pubDate>Tue, 27 Jul 2010 18:18:03 +0000</pubDate>
		<guid isPermaLink="false">http://todbot.com/blog/?p=601#comment-72557</guid>
		<description>Yes, it&#039;s easy to run into many traps when using malloc().  Since the memory space of the chip in Arduino is so small and the tasks being performed are usually fairly simple, I much prefer to use statically-allocated buffers.  You never run the risk of memory leaks then and you can know at compile-time exactly how much RAM you have left.  (Arduino doesn&#039;t yet expose this however, but the &quot;avr-size&quot; command can give you static RAM usage)</description>
		<content:encoded><![CDATA[<p>Yes, it&#8217;s easy to run into many traps when using malloc().  Since the memory space of the chip in Arduino is so small and the tasks being performed are usually fairly simple, I much prefer to use statically-allocated buffers.  You never run the risk of memory leaks then and you can know at compile-time exactly how much RAM you have left.  (Arduino doesn&#8217;t yet expose this however, but the &#8220;avr-size&#8221; command can give you static RAM usage)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Jarvis</title>
		<link>http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/comment-page-2/#comment-72556</link>
		<dc:creator>Matt Jarvis</dc:creator>
		<pubDate>Tue, 27 Jul 2010 18:04:35 +0000</pubDate>
		<guid isPermaLink="false">http://todbot.com/blog/?p=601#comment-72556</guid>
		<description>Hi todbot &amp; Rob -

*Loved* the code for serial communication- many many thanks... 

In using it I noticed it was getting so far and it was freezing.. going through the code (Rob re-wrote) I noticed that the malloc was been run and using up all the memory.. so put

     free(cmdbuf);

somewhere when you are clearing variables and you shouldn&#039;t have the same problem :D</description>
		<content:encoded><![CDATA[<p>Hi todbot &amp; Rob -</p>
<p>*Loved* the code for serial communication- many many thanks&#8230; </p>
<p>In using it I noticed it was getting so far and it was freezing.. going through the code (Rob re-wrote) I noticed that the malloc was been run and using up all the memory.. so put</p>
<p>     free(cmdbuf);</p>
<p>somewhere when you are clearing variables and you shouldn&#8217;t have the same problem :D</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: My Arduino workings &#124; Prodical's Blog</title>
		<link>http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/comment-page-1/#comment-72548</link>
		<dc:creator>My Arduino workings &#124; Prodical's Blog</dc:creator>
		<pubDate>Wed, 21 Jul 2010 09:50:31 +0000</pubDate>
		<guid isPermaLink="false">http://todbot.com/blog/?p=601#comment-72548</guid>
		<description>[...] Todbot’s blog has a post Arduino Serial protocol design patterns which discusses solutions and has example code for this very issue&#8230; so after a bit of [...]</description>
		<content:encoded><![CDATA[<p>[...] Todbot’s blog has a post Arduino Serial protocol design patterns which discusses solutions and has example code for this very issue&#8230; so after a bit of [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Smoker code repository and something more interesting with ProtoThreads &#171; The Arduino Collective</title>
		<link>http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/comment-page-1/#comment-58691</link>
		<dc:creator>Smoker code repository and something more interesting with ProtoThreads &#171; The Arduino Collective</dc:creator>
		<pubDate>Tue, 15 Dec 2009 06:37:06 +0000</pubDate>
		<guid isPermaLink="false">http://todbot.com/blog/?p=601#comment-58691</guid>
		<description>[...] The initial Arduino sketch implements a simple LED blinker that can be controlled over the serial connection.  It starts off blinking the led every second and you can set the delay by sending a setDelay command (like &#8220;setDelay 4&#8243;).  It also reports the current delay every ten seconds.  It&#8217;s not much, but it does demonstrate using ProtoThreads to handle more than one process at a time.  It also implements serial command parsing, with heavy inspiration (and code) from this todbot post. [...]</description>
		<content:encoded><![CDATA[<p>[...] The initial Arduino sketch implements a simple LED blinker that can be controlled over the serial connection.  It starts off blinking the led every second and you can set the delay by sending a setDelay command (like &#8220;setDelay 4&#8243;).  It also reports the current delay every ten seconds.  It&#8217;s not much, but it does demonstrate using ProtoThreads to handle more than one process at a time.  It also implements serial command parsing, with heavy inspiration (and code) from this todbot post. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: todbot</title>
		<link>http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/comment-page-1/#comment-56608</link>
		<dc:creator>todbot</dc:creator>
		<pubDate>Wed, 07 Oct 2009 17:55:24 +0000</pubDate>
		<guid isPermaLink="false">http://todbot.com/blog/?p=601#comment-56608</guid>
		<description>Posted for Kasper, since there&#039;s some weirdness with him posting this.  He says:

I tried the method above and I got it working partly. It works almost perfect when I send a message with a keypress action in Processing. However when I trigger the sending code from the draw() function it fails. I tried several framerates ( 1 loop every second ) but that didn&#039;t work out. 

By the way, with lower framerates (&lt;10) the keyPress function seems more unstable as well. 

It could be Osx Snow Leopard in combination with Processing. Since they fixed serial communication with &quot;duct-tape&quot; for the 1.0.7 release. 

Another thing could have been that I send DMX ( and use sei() and cli() to disable interrupts during that time ), however even turning the DMX sending of ( and let Arduino response with the received message ) didn&#039;t help. 

Maybe you see any flaws ? Code below is working good when I write the array from the keyPressed function. 

Arduino code :
&lt;pre&gt;
int intensity_Array[72];
int arraylength=72;

byte ledNum; 
byte redVal; 
byte grnVal; 
byte bluVal;  

int arrayStartPos;

void setup() 
{ pinMode(11, OUTPUT);
  digitalWrite(13, HIGH);  
  Serial.begin(57600); 
}

void loop()
{ if( Serial.available() == 4 ) {
    ledNum = Serial.read();
    redVal = Serial.read();
    grnVal = Serial.read();
    bluVal = Serial.read();
    
    arrayStartPos = ledNum*3;
    
    intensity_Array[arrayStartPos]   = redVal;
    intensity_Array[arrayStartPos+1] = grnVal;
    intensity_Array[arrayStartPos+2] = bluVal;
  }
  
  if(ledNum==22) 
  { ledNum=0; // set the ledNum to the beginning
    
    for(int i=0; i&lt;arraylength ; i++)
    { Serial.print(intensity_Array[i]);
      Serial.print(&quot;,&quot;);  
    }  
    Serial.println(13);

    /* send frame to led data */
    //cli();    
    //sendFrame(); 
    //sei();
  }
   
}
&lt;/pre&gt;
Processing code :
&lt;pre&gt;
import processing.serial.*;

Serial port;                        
String message = null;              

int counter = 0;

void setup() 
{ //frameRate(10);  
  println(&quot;Opening: &quot;+Serial.list()[0]);
  port = new Serial(this, Serial.list()[0], 57600);     
} 

void draw() 
{  // running writeArray from here doesn&#039;t work
   //writeArray();
  
   // print the reply from Arduino
   message = port.readStringUntil(13); 
   if (message != null) println(message);  
}
    
void keyPressed()
{ writeArray();
}
   
void writeArray()
{ for( int i=0; i&lt; 23; i++ ) 
  { writeLed( i, 200, counter*20, 0 );      
  }
  counter++;
  if(counter&gt;12) counter=0;
}    

void writeLed( int ledNum, int r, int g, int b ) 
{ port.write( (byte) ledNum );
  port.write( (byte) r );
  port.write( (byte) g );
  port.write( (byte) b );
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Posted for Kasper, since there&#8217;s some weirdness with him posting this.  He says:</p>
<p>I tried the method above and I got it working partly. It works almost perfect when I send a message with a keypress action in Processing. However when I trigger the sending code from the draw() function it fails. I tried several framerates ( 1 loop every second ) but that didn&#8217;t work out. </p>
<p>By the way, with lower framerates (&lt;10) the keyPress function seems more unstable as well. </p>
<p>It could be Osx Snow Leopard in combination with Processing. Since they fixed serial communication with &#8220;duct-tape&#8221; for the 1.0.7 release. </p>
<p>Another thing could have been that I send DMX ( and use sei() and cli() to disable interrupts during that time ), however even turning the DMX sending of ( and let Arduino response with the received message ) didn&#8217;t help. </p>
<p>Maybe you see any flaws ? Code below is working good when I write the array from the keyPressed function. </p>
<p>Arduino code :</p>
<pre>
int intensity_Array[72];
int arraylength=72;

byte ledNum;
byte redVal;
byte grnVal;
byte bluVal;  

int arrayStartPos;

void setup()
{ pinMode(11, OUTPUT);
  digitalWrite(13, HIGH);
  Serial.begin(57600);
}

void loop()
{ if( Serial.available() == 4 ) {
    ledNum = Serial.read();
    redVal = Serial.read();
    grnVal = Serial.read();
    bluVal = Serial.read();

    arrayStartPos = ledNum*3;

    intensity_Array[arrayStartPos]   = redVal;
    intensity_Array[arrayStartPos+1] = grnVal;
    intensity_Array[arrayStartPos+2] = bluVal;
  }

  if(ledNum==22)
  { ledNum=0; // set the ledNum to the beginning

    for(int i=0; i&lt;arraylength ; i++)
    { Serial.print(intensity_Array[i]);
      Serial.print(",");
    }
    Serial.println(13);

    /* send frame to led data */
    //cli();
    //sendFrame();
    //sei();
  }

}
</pre>
<p>Processing code :</p>
<pre>
import processing.serial.*;

Serial port;
String message = null;              

int counter = 0;

void setup()
{ //frameRate(10);
  println("Opening: "+Serial.list()[0]);
  port = new Serial(this, Serial.list()[0], 57600);
} 

void draw()
{  // running writeArray from here doesn't work
   //writeArray();

   // print the reply from Arduino
   message = port.readStringUntil(13);
   if (message != null) println(message);
}

void keyPressed()
{ writeArray();
}

void writeArray()
{ for( int i=0; i&lt; 23; i++ )
  { writeLed( i, 200, counter*20, 0 );
  }
  counter++;
  if(counter&gt;12) counter=0;
}    

void writeLed( int ledNum, int r, int g, int b )
{ port.write( (byte) ledNum );
  port.write( (byte) r );
  port.write( (byte) g );
  port.write( (byte) b );
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: todbot</title>
		<link>http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/comment-page-1/#comment-56570</link>
		<dc:creator>todbot</dc:creator>
		<pubDate>Mon, 05 Oct 2009 22:15:31 +0000</pubDate>
		<guid isPermaLink="false">http://todbot.com/blog/?p=601#comment-56570</guid>
		<description>Hi Kasper,
I&#039;ve had good luck with the &quot;if(Serial.available() == len)&quot; before.  I&#039;m not sure why it doesn&#039;t work for you, your example is light on details.  Having the protocol consist of 4 bytes, one for the led number, and 3 bytes for the color, is a sound one.  If I were to do this, it would look something like:
&lt;pre&gt;
void loop() {
  if( Serial.available() == 4 ) {
    ledNum = Serial.read();
    redVal = Serial.read();
    grnVal = Serial.read();
    bluVal = Serial.read();
    set_led( ledNu, redVal, grnVal, bluVal );
  }
}
&lt;/pre&gt;
(where &quot;set_led()&quot; is some function you have in your Arduino sketch that does the actual LED handling).

In Processing, the code to set a particular LED to a particular color would look something like:
&lt;pre&gt;
void setLEDColor( int ledNum, color c ) {
  myPort.write( (byte) ledNum );
  myPort.write( red(c) );
  myPort.write( green(c) );
  myPort.write( blue(c) );
}
void draw() {
  for( int i=0; i&lt; 22; i++ ) { // 22 == number of LEDs
    color c = color( random(), random(), random() );
    setLedColor( i, c );
  }
}
&lt;/pre&gt;

Because this very simple 4-byte protocol has no stop/start byte, it&#039;s possible for the Processing sketch and the Arduino sketch to get out of sync.  In practice, this rarely happens.&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi Kasper,<br />
I&#8217;ve had good luck with the &#8220;if(Serial.available() == len)&#8221; before.  I&#8217;m not sure why it doesn&#8217;t work for you, your example is light on details.  Having the protocol consist of 4 bytes, one for the led number, and 3 bytes for the color, is a sound one.  If I were to do this, it would look something like:</p>
<pre>
void loop() {
  if( Serial.available() == 4 ) {
    ledNum = Serial.read();
    redVal = Serial.read();
    grnVal = Serial.read();
    bluVal = Serial.read();
    set_led( ledNu, redVal, grnVal, bluVal );
  }
}
</pre>
<p>(where &#8220;set_led()&#8221; is some function you have in your Arduino sketch that does the actual LED handling).</p>
<p>In Processing, the code to set a particular LED to a particular color would look something like:</p>
<pre>
void setLEDColor( int ledNum, color c ) {
  myPort.write( (byte) ledNum );
  myPort.write( red(c) );
  myPort.write( green(c) );
  myPort.write( blue(c) );
}
void draw() {
  for( int i=0; i< 22; i++ ) { // 22 == number of LEDs
    color c = color( random(), random(), random() );
    setLedColor( i, c );
  }
}
</pre>
<p>Because this very simple 4-byte protocol has no stop/start byte, it's possible for the Processing sketch and the Arduino sketch to get out of sync.  In practice, this rarely happens.</pre>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic page generated in 0.574 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-02-06 01:56:21 -->

