We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › Processing to Arduino: sending numbers
Page Index Toggle Pages: 1
Processing to Arduino: sending numbers (Read 2549 times)
Processing to Arduino: sending numbers
Nov 3rd, 2009, 1:26am
 
Hi,

The sketches pasted below send 70 numbers from processing to arduino.  At this point the code contains several irrelevant bits and pieces (it's a work in progress).  However it's simple and it works, and if you're struggling to send stuff from processing to arduino, hopefully it'll be helpful.  

Be sure to upload the arduino code first, with the processing code not running, (or stuff will freeze).  Then run the processing code.

It'd be great to hear comments and suggestions for improvement.  

thanks!  :-)
~Patch

Processing Code:
//This sketch sends 70 numbers to an Arduino board.

import processing.serial.*;
Serial port;

//we want to send the values in the following array to the arduino board.
byte a[]={53, 69, 74, 27, 32, 41, 78, 98,  105, 34,
         52, 70, 75, 28, 33, 42, 79, 99,  106, 35,
         51, 71, 76, 29, 34, 43, 80, 100, 107, 36,
         50, 72, 77, 30, 35, 44, 81, 101, 108, 37,
         49, 73, 78, 31, 36, 45, 82, 102, 109, 38,
         48, 74, 79, 32, 37, 46, 83, 103, 110, 39,
         47, 75, 80, 33, 38, 47, 84, 104, 111, 40};
int n=70;    //(n) is the number of elements in the above array.
int state=1; //(state) keeps track of where we are in the communication process.
int last;    //after each number is sent, arduino sends us the number's index.  we store this in (last).

void setup() {
 size(200,200);
 noStroke();
 fill(255,255,255);
 rect(50,50,100,100);
 port = new Serial(this,Serial.list()[0],9600);
}

void draw() {
 if (state==2) {        //a click is detected- a user wants to send data to arduino
   fill(255,0,0);
   rect(50,50,100,100); //draw a red box (it'll stay red until all data is sent).
   state=3;
 }else if(state==3){    //this is where we actually send data to arduino
   port.clear();        //clear the output channel
                        //we write two bytes (i.e., numbers between 0 and 255) to the output channel.
   port.write(byte(0)); //the first byte is the index.
   port.write(a[0]);    //the second byte is the actual value.
   delay(40);           //now we wait 40 milliseconds
                        //now we repeat this for all the other values in a.  for each value we send
                        //an index, and then the actual value.  after receiving these two values,
                        //arduino sends back the index.  i intended to double check that the right
                        //number was sent, but i haven't done this yet...
   for (int i=1;i<n;i++){
     while (port.available()<1);
     last=port.read();
     if (int(last)==i-1){  //confirm that the last element was received
       port.clear();
       port.write(byte(i));
       port.write(a[i]);
       delay(40);
     }
   }
   state=4;
 }else if(state==4){  //all values have been sent to arduino, so draw a white box.
   fill(255,255,255);
   rect(50,50,100,100);
   state=1;
 }
}

void mousePressed(){
 if ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150)){
   state=2;
 }
}


Arduino Code:
//arduino reads in 70 numbers from a processing sketch
//
//the processing sketch sends numbers to arduino.  we check a few of these numbers,
//and if there are no errors, we flash some LEDs in a recognizable pattern.
//
//to flash LEDs, the arduino board is connected to two simple circuits:
//  arduino digital pin 9  --> red diode    --> resistor --> arduino digital GND
//  arduino digital pin 13 --> yellow diode --> resistor --> arduino digital GND

byte j;
byte a[70];
int  x[10];
int  k=10;
boolean f0 = true;  //true when arduino is waiting for data.
boolean f1 = false; //indicates that x needs to be filled with values
const int redPin    =  9;
const int yellowPin = 13;

void setup()
{
 Serial.begin(9600);
 pinMode(redPin,    OUTPUT); digitalWrite(redPin,    LOW);
 pinMode(yellowPin, OUTPUT); digitalWrite(yellowPin, LOW);
}

void loop(){
 if (f0){
   for (int i=0;i<70;i++){         //read in data for each element of a
     while(Serial.available()<2);  //wait until the buffer contains two bytes
     j        =Serial.read();      //read the first byte (index)
     a[int(j)]=Serial.read();      //read the second byte (actual value)
     Serial.flush();   //flush the output buffer
     Serial.write(j);  //tell arduino that item j was received
   }
   //check a few of the values recieved from processing
   if (a[0]==53 & a[1]==69 & a[2]==74 & a[3]==27 & a[69]==40){
     f0=false;
     f1=true;
   }
 }
 
 if (f1){  //encode a pattern of flashes in x, (1 is red, 2 is yellow)
   x[0]=1;
   x[1]=2;
   x[2]=1;
   x[3]=2;
   x[4]=1;
   x[5]=2;
   x[6]=1;
   x[7]=2;
   x[8]=1;
   x[9]=2;
   f1=false;
   k=0;
 }
 
 if(k<10){
   if(x[k]==1){        
     //flash the red pin
     digitalWrite(redPin,    HIGH); delay(100);
     digitalWrite(redPin,    LOW);  delay(500);
   }else if (x[k]==2){
     //flash the yellow pin
     digitalWrite(yellowPin, HIGH); delay(100);
     digitalWrite(yellowPin, LOW);  delay(500);
   }
   k=k+1;
 }else if (k==10){
   f0=true;
 }
}


Page Index Toggle Pages: 1