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 › Pass String from Processing to ARDUINO
Page Index Toggle Pages: 1
Pass String from Processing to ARDUINO (Read 1899 times)
Pass String from Processing to ARDUINO
Jan 12th, 2010, 3:35am
 
After two days of trying to read a string in ARDUINO environment that has already passed from Processing to ARDUINO my code(based on founded code) works, so I feel posting it to help future similar questions. Maybe it is not the best piece of code but it works and it lights an LED on/off when a certain string passes from Processing to ARDUINO through serial. I send two different strings by pressing and realising the mouse.Here I post seperately the code for ARDUINO and the code for Processing:

ARDUINO code:

char inData[20]; // Allocate some space for the string
char inChar=-1; // Where to store the character read
byte index = 0; // Index into array; where to store the character
int ledPin = 13; // Set the pin to digital I/O 4

void setup(){
 pinMode(ledPin, OUTPUT);
 Serial.begin(9600);
 Serial.write("Power On");
}





char Comp(char* This){

 while(Serial.available() > 0) // Don't read unless
   // there you know there is data
 {
   if(index < 19) // One less than the size of the array
   {
     inChar = Serial.read(); // Read a character
     inData[index] = inChar; // Store it
     index++; // Increment where to write next
     inData[index] = '\0'; // Null terminate the string
   }
 }

 if(strcmp(inData,This)  == 0){
   for(int i=0;i<19;i++){
     inData[i]=0;
   }
   index=0;
   return(0);

 }
 else{
   return(1);


 }
}


void loop()
{
 if(Comp("light on")==0){
       digitalWrite(ledPin, HIGH); // turn the LED on
 }
 if(Comp("light off")==0){
       digitalWrite(ledPin, LOW); // turn the LED off
 }
delay(100); // Wait 100 milliseconds for next reading
}



Processing Code:

import processing.serial.*;
Serial port;
void setup() {
   port = new Serial(this,Serial.list()[1], 9600);
}
void draw() {}
void mousePressed() {
 port.write("light on");
}
void mouseReleased() {
 port.write("light off");
}
Page Index Toggle Pages: 1