Getting Processing to send values to Arduino

Hi all!

I'm trying to send values to an Arduino Uno from Processing. I've made my brother a circuit that he can use to control a linear actuator and a DMX light. I'm trying to make it so that he can write his own (beginner) Processing code which sends values which correspond with the light and the actuators. The circuit works fine, and I have a working code where I can upload values to it "manually," to change the color and move the actuators. However, I don't know how to change the 8 variables from processing over serial, and make sure that each value goes to it's correct place in the Arduino code.

I've included the Arduino code, minus any of the nonsense I wrote for the serial port. Instead, I've put a note in the void loop() function, where I need to be changing the variables. You can check out the other functions if you like, but the important thing is that they do work when I put the values I want into them. I just need to change the R, G, B, UV, Ac1, Ac2, Ac3, and dtime values via serial, from processing. Can someone point me in the right direction?

// Include headers 
#include <DmxSimple.h>


// Define what pins and variables are hooked up to what -----------------------------------------------------------------------------------------
int f1 = 13; //Actuator 1 Forward
int b1 = 12; //Actuator 1 Backwards
int f2 = 11; //Actuator 2 Forward
int b2 = 10; //Actuator 2 Backwards
int f3 = 9; //Actuator 3 Forward
int b3 = 8; //Actuator 3 Backwards

//// Initialize all variables to 0
// Colors range from a value of 0 - 255
int R = 0; //Red
int G = 1; //Green
int B = 0; //Blue
int UV = 0; //UV
// 0 is stop, 1 is forward, 2 is backward
int Ac1 = 0; //Actuator 1
int Ac2 = 0; //Actuator 2
int Ac3 = 0; //Actuator 3
// Can be any value, but timing gets better as "dtime" approaches 0.
int dtime = 0; //time to delay in between actions
// intialize an array to put the values into with a loop
int values[8] = {4,2,2,2,2,2,2,2};
//int pvalues[8] = {32,91,99,10,1,2,0,1};
//int trash = 0;


//Setup the program in the setup function-----------------------------------------------------------------------------------------
void setup() 
{
  /* The most common pin for DMX output is pin 3, which DmxSimple
  ** uses by default. If you need to change that, do it here. */
  DmxSimple.usePin(3);

  /* DMX devices typically need to receive a complete set of channels
  ** even if you only need to adjust the first channel. You can
  ** easily change the number of channels sent here. If you don't
  ** do this, DmxSimple will set the maximum channel number to the
  ** highest channel you DmxSimple.write() to. */
  DmxSimple.maxChannel(7);
  
  //Initialize pins to do their thing
  pinMode(f1,OUTPUT);
  pinMode(b1,OUTPUT);
  pinMode(f2,OUTPUT);
  pinMode(b2,OUTPUT);
  pinMode(f3,OUTPUT);
  pinMode(b3,OUTPUT);
  
  //Begin serial communication at 9600 baud
  Serial.begin(9600); 
  
  //Do other setup based stuff in here.
}


//Loop function that goes over and over again-------------------------------------------------------------------------------------
void loop() 
{



  //WE NEED TO CHANGE THE R,G,B,UV,Ac1,Ac2,Ac3, and DTIME values right here!!!!!
  //If the values are changed right here simply by editing their values and reuploading, everything works.
  //I can even change the values in a loop and do color fades, so speed isn't a problem.
  //R,G,B,UV are on a scale of 0-255
  //Ac1, Ac2, and Ac3 are 0 for off, 1 for forwards, 2 for backwards.
  //dtime is in milliseconds.
  //I need to read 8 serial values here, in correct order, and assign them to the R,G,B,etc values.
  


  // Change the color of each one of the channels
  changeto(R, G, B, UV);
  
  // Turn on or off the linear actuators
  moveto(Ac1, Ac2, Ac3);

  //delay for the ever so shortest time
  delay(dtime);
}


//------------------------------------------------------------------------------------------------------------------------------
//--------------------------------Try not to go past here.-----------------------------------
//------------------------------------------------------------------------------------------------------------------------------


//Function that instantly changes the light color--------------------------------------------------------------------------------
void changeto(int R, int G, int B, int UV)
{
 //function that instantly changes to another to another light.
 if(R <=255 && G <=255 && B <= 255 && UV <= 255) //Error Check 
 {
        /* Update DMX channel 2 to new brightness */
    DmxSimple.write(1, R);
        /* Update DMX channel 2 to new brightness */
    DmxSimple.write(2, G);
        /* Update DMX channel 3 to new brightness */
    DmxSimple.write(3, B);
        /* Update DMX channel 4 to new brightness */
    DmxSimple.write(4, UV); 
 }
}

//Check to see if a pre-known array will turn on blink
void checkwithblink()
{
}

//Function that moves the linear actuators---------------------------------------------------------------------------------------
void moveto(int Ac1, int Ac2, int Ac3)
{

  // 0 is stop
  // 1 is forward
  // 2 is backward
  
//Actuator 1-------------------
   if (Ac1 == 1) //Forward
   {
     digitalWrite(f1, HIGH);
     digitalWrite(b1, LOW);
   }
   if (Ac1 == 2) //Backward
   {
     digitalWrite(f1, LOW);
     digitalWrite(b1, HIGH);
   }
   if (Ac1 != 1 && Ac1 != 2)//Stop
   {
     digitalWrite(f1, LOW);
     digitalWrite(b1, LOW);
   }
   
//Actuator 2-------------------
   if (Ac2 == 1) //Forward
   {
     digitalWrite(f2, HIGH);
     digitalWrite(b2, LOW);
   }
   if (Ac2 == 2) //Backward 
   {
     digitalWrite(f2, LOW);
     digitalWrite(b2, HIGH);
   }
   if (Ac2 != 1 && Ac2 != 2) //Stop
   {
     digitalWrite(f2, LOW);
     digitalWrite(b2, LOW);
   }
  
//Actuator 3--------------------
   if (Ac3 == 1) //Forward
   {
     digitalWrite(f3, HIGH);
     digitalWrite(b3, LOW);
   }
   if (Ac3 == 2) //Backward 
   {
     digitalWrite(f3, LOW);
     digitalWrite(b3, HIGH);
   }
   if (Ac3 != 1 && Ac3 != 2) //Stop
   {
     digitalWrite(f3, LOW);
     digitalWrite(b3, LOW);
   }
   
}

If you guys are looking for a good laugh, I can post the processing code - but I'm pretty sure I want to start over on it anyways. It's a steaming pile of dump.

Have a look at Serial input basics - updated for techniques to read serial data and assign values to variables by parsing the received data.

UKHeliBob:
Have a look at Serial input basics - updated for techniques to read serial data and assign values to variables by parsing the received data.

Awesome, I had never seen that! Not only did it help with my current projects, it also cleared up some general questions I had about serial communication - which I consider a weak point of mine. I've already adapted the code to what I need and it works marvelously when sending the values by hand. If anyone is trying to read anything into Arduino, I gotta reccomend that article.

Now I've gotta figure out how to do the reverse on the processing side, and put some data into that format. If anyone's got any tips, lemme know! :grinning:

And thanks, UKHeliBob! May your helicopters fly high!

Now I've gotta figure out how to do the reverse on the processing side, and put some data into that format. If anyone's got any tips, lemme know!

You've defined some format that the data should be in, but not shown us that format.
You've changed the Arduino code to handle that data, but have not shown that code.
You have some Processing code that should send that data, but have not shown that code.

You can't really expect us to tell you how to modify the Processing code.