Practical Serial Communication

After establishing sound communication links from Arduino to Processing and vice-versa lets notch things up a bit. The basic idea is to simulate and develop a framework for real situations so that we can modify the code accordingly to suit the need.

This is the video –

Here we make a Processing based control pad in the form of a Cartesian plane with the origin being the center of the circle so that in the first quadrant both mouseX and mouseY values are positive , in the second quadrant mouseX is negative but mouseY is positive and so on. The processing code passes a string that displays signs and co-ordinates of the mouse pointer taking (0,0) to be the center of the circle. If x co-ordinate is -ve then that is indicated by the glowing of an orange led  and the position of mouseX controls the brightness of a blue led, similarly if mouseY is -ve then it is indicated by the glowing of a green led and the position of mouseY controls the brightness of the red led.

Processing side code–

import processing.serial.*;
Serial myport;
String plus = "+";
String subt = "-";
String s1 = "", s2= "";
void setup()
{
  size(600, 600);
  smooth();
  String portname = "COM3";
  myport = new Serial(this, portname, 9600);
}
void draw()
{
  background(204);
  ellipse(300, 300, 510, 510);
  line(300, 300, mouseX, mouseY);
  if ((mouseX - 300) >= 0)
  {
    s1 = plus+(mouseX-300);
  } else
  {
    s1 = subt+(300-mouseX);
  }

  if ((300 - mouseY) >= 0)
  {
    s2 = plus+(300-mouseY);
  } else
  {
    s2 = subt+(mouseY-300);
  }
  myport.write(s1+s2+';'+'\n');
  println(s1+s2);
}

Arduino side code–

const int red_led = 10;
const int orange_led = 13;
const int blue_led = 11;
const int green_led = 12;
void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(10, OUTPUT);
  digitalWrite(13, LOW);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  digitalWrite(12, LOW);
  // put your setup code here, to run once:

}

void loop() {
  String rec = Serial.readStringUntil('\n');
  char msg[10];
  rec.toCharArray(msg, 10);
  if (msg[2] == '+' || msg[2] == '-')
  {
    if (msg[2] == '+')
    {
      digitalWrite(green_led, LOW);
    }
    else if (msg[2] == '-')
    {
      digitalWrite(green_led, HIGH);
    }
    if (msg[4] == ';')
    {
      analogWrite(red_led, msg[3]);
    }
    else if (msg[5] == ';')
    {
      analogWrite(red_led, 10 * msg[3] + msg[4]);
    }
    else if (msg[6] == ';')
    {
      analogWrite(red_led, 100 * msg[3] + 10 * msg[4] + msg[5]);
    }
    if (msg[0] == '+')
    {
      digitalWrite(orange_led, LOW);
      analogWrite(blue_led, msg[1]);
    }
    else if (msg[0] == '-')
    {
      digitalWrite(orange_led, HIGH);
      analogWrite(blue_led, msg[1]);
    }
  }
  else if (msg[3] == '+' || msg[3] == '-')
  {
    if (msg[5] == ';')
    {
      analogWrite(red_led, msg[4]);
    }
    else if (msg[6] == ';')
    {
      analogWrite(red_led, 10 * msg[4] + msg[5]);
    }
    else if (msg[7] == ';')
    {
      analogWrite(red_led, 100 * msg[4] + 10 * msg[5] + msg[6]);
    }
    if (msg[3] == '+')
    {
      digitalWrite(green_led, LOW);
    }
    else if (msg[3] == '-')
    {
      digitalWrite(green_led, HIGH);
    }
    if (msg[0] == '+')
    {
      digitalWrite(orange_led, LOW);
      analogWrite(blue_led, 10 * msg[1] + msg[2]);
    }
    else if (msg[0] == '-')
    {
      digitalWrite(orange_led, HIGH);
      analogWrite(blue_led, 10 * msg[1] + msg[2]);
    }
  }
  else if (msg[4] == '+' || msg[4] == '-')
  {
    if (msg[6] == ';')
    {
      analogWrite(red_led, msg[5]);
    }
    else if (msg[7] == ';')
    {
      analogWrite(red_led, 10 * msg[5] + msg[6]);
    }
    else if (msg[8] == ';')
    {
      analogWrite(red_led, 100 * msg[5] + 10 * msg[6] + msg[7]);
    }
    if (msg[4] == '+')
    {
      digitalWrite(green_led, LOW);
    }
    else if (msg[4] == '-')
    {
      digitalWrite(green_led, HIGH);
    }
    if (msg[0] == '+')
    {
      digitalWrite(orange_led, LOW);
      analogWrite(blue_led, 100 * msg[1] + 10 * msg[2] + msg[3]);
    }
    else if (msg[0] == '-')
    {
      digitalWrite(orange_led, HIGH);
      analogWrite(blue_led, 100 * msg[1] + 10 * msg[2] + msg[3]);
    }
  }
  // put your main code here, to run repeatedly:

}
Advertisements

2 thoughts on “Practical Serial Communication

  1. I really like your concept. Have you looked at a big LED display? You could have 2D graphical objects in the processing environment rendered in an LED matrix. There is a big movement toward large scale LED displays like these https://www.youtube.com/watch?v=g6N9Qid8Tqs.
    I myself use an XBOX Kinect to manipulate sound. I am currently working on the idea of using an LED display in conjunction with the KINECT. https://www.youtube.com/watch?v=Z5asQqhQNqk

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s