go_away

Author Topic: Send commands to Arduino from Processing  (Read 806 times)

0 Members and 1 Guest are viewing this topic.

Offline MECH_ENGTopic starter

  • Jr. Member
  • **
  • Posts: 14
  • Helpful? 0
Send commands to Arduino from Processing
« on: March 20, 2013, 07:48:57 PM »
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.

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;
  }
}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>/code]

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



[code]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(">");
 

}/code]

Offline Azraels

  • Full Member
  • ***
  • Posts: 57
  • Helpful? 1
Re: Send commands to Arduino from Processing
« Reply #1 on: March 20, 2013, 11:53:41 PM »
If I had not  been coding all day I may have been able to actually sort through this but my brain is ready for some rest. But just wanted to make sure you know Arduino has examples included for dimming an LED with Processing in the Communication Folder. Its a pretty simple code. Im assuming all that math in your code has more to do with your ROV than just dimming that LED.
If your first post is, "I want to build a super complex robot with object recognition, etc..but I have never done programming or electronics...etc." 
Your doing it wrong. Start Simple and Work Up.

 


Get Your Ad Here