Go Down

Topic: Send commands to Arduino from Processing (Read 896 times) previous topic - next topic

mech_eng

Hey all, I'm having issue sending commands to an Arduino via processing. The commands take the form of <case><mag>. Right now I'm just trying to vary the brightness of an LED but will eventually use this to control an ROV. If i send <1><255> to the Arduino from the Arduino Serial Monitor it works fine. Sending from processing is not working however. I've attached the code below.

Arduino:
Code: [Select]

int dir;
int mag;
const int led = 5;

const char EOP = '>';
const char SOP = '<';
int i = 0;


void setup()
{
  Serial.begin(9600);
  pinMode(led, OUTPUT);
}

void loop()
{
  if (Serial.available() > 0)
{
   process_input();

   if ( i == 2) //wait to receive two EOP markers
   {
   if ( dir == 400 ) {analogWrite(led , mag);}
   if ( dir == 401 ) {digitalWrite(led, LOW);}
   i = 0;
   }
}
}


//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

void process_input()
{
  static int receivedNumber = 0;
  byte c = Serial.read ();   
  switch (c)
  {
  case EOP: 
    process_number (receivedNumber);
    i++;

  case SOP:
    receivedNumber = 0;
    break;

  case '0' ... '9':
    receivedNumber *= 10;         
    receivedNumber += c -'0';         
    break;
  }
}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

void process_number(int x) 
{
  if ( x < 256 )    // if number is less than 255 then is must correspond to magnitude
  {
    mag = x;
  }

  if ( x == 400 || x == 401)  // if number is 400 or 401 it corresponds to direction
  {
    dir = x;
  }
}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>



Processing (just draw function shown let me know if whole code is needed)


Code: [Select]


void draw()
{
  background(255);
  fill(0);
  ellipse(x,y, 20, 20);
  fill(0);
  line(width/2, 0, width/2, height);
  line(0, height/2, width, height/2);
  ellipseMode(CENTER);
  noFill();
  ellipse(width/2,height/2, height - 20 , width - 20);
 
  x1 = map(x, 10 , 740 , -255, 255);
  y1 = map(y, 10 , 740 , -255, 255);
 
  R = sqrt(x1*x1 + y1*y1);
 
  if ( x >= width/2 && y <= height/2)  //Q1
  {theta = -atan(y1/x1)*180/pi; Q = 1;}
 
  if ( x < width/2 && y < height/2) //Q2
  {theta = 180 - atan(y1/x1)*180/pi; Q = 2;}
 
  if ( x < width/2 && y > height/2) //Q3
  {theta = 180 - atan(y1/x1)*180/pi; Q = 3;}
 
  if ( x >= width/2 && y >= height/2) //Q4
  {theta = 360 - atan(y1/x1)*180/pi; Q = 4;}
 

  //display commands
  text("R         " + (int)R, 2/8*width , 50);
  text("Theta   " + (int)theta, 2/8*width,70);
  text("Q" + (int)Q, 2/8*width , 90);


  arduino.write("<");
  arduino.write("1");
  arduino.write(">");
  arduino.write("<");
  arduino.write((byte)R);
  arduino.write(">");
 

}


mech_eng

Got it to work by creating a string:

  String message = ( "<" + (int)q + ">" + "<" + (int)r + ">" );
  arduino.write(message);
  println(message);

PaulS

Code: [Select]
  arduino.write((byte)R);
Unlike the Serial class on the Arduino, the Serial class in Processing does not distinguish between ASCII data and binary data with separate functions. Processing has one method, write(). If you write() a byte, it is binary data. If you write() an int, it is binary data. If you write() a string, it is still binary data, but that binary data contains ASCII values.

It's good that you figured out a solution. I just thought you might to want to know why you had the problem.

Go Up