User Tools

Site Tools


courses:intro.prototyping.fall2013.dec10

Serial communication between Processing and Arduino

Time to start connecting the virtual world with the Physical.

Set up an Arduino with an LED connected to a pin that does PWM (squiggly line by the pin-number on the board).

Processing sending

import processing.serial.*;
 
Serial myPort;  // The serial port
 
void setup() {
  size(800,600);
  background(0);
  // List all the available serial ports
  println(Serial.list());
  // I know that the first port in the serial list on my mac
  // is always my  Keyspan adaptor, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[2], 9600);
  myPort.clear();
}
 
void draw() {
 
}
 
void mousePressed() {
  if (mouseButton == LEFT) 
  {
    myPort.write("180\n");
  }
  else
  {
    myPort.write("20\n");
  }
}

Arduino recieving

// pin for the LED:
 const int ledPin = 11;
 
void setup() {
   // initialize serial:
   Serial.begin(9600);
   // make the pin output:
   pinMode(ledPin, OUTPUT); 
}
 
void loop() {
   // if there's any serial available, read it:
   while (Serial.available() > 0) {
 
     // look for the next valid integer in the incoming serial stream:
     int led = Serial.parseInt(); 
 
     // look for the newline. That's the end of your
     // sentence:
     if (Serial.read() == '\n') {
       // fade the plus-leg of the LED: 
       analogWrite(ledPin, led);
     }
   }
   delay(20);
}

Three values

Processing
import processing.serial.*;
 
Serial myPort;  // The serial port
int gPos = 165;
int rPos = 165;
int yPos = 165;
int gValue = 0, rValue = 0, yValue = 0;
 
void setup() {
  size(700,600);
  background(0);
  // List all the available serial ports
  println(Serial.list());
  // I know that the first port in the serial list on my mac
  // is always my  Keyspan adaptor, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[2], 9600);
  myPort.clear();
  // Throw out the first reading, in case we started reading 
  // in the middle of a string from the sender.
  rectMode(CENTER);
}
 
void draw() {
  background(0);
  fill(128);
  rect(120, 300, 10, 300);
  rect(320, 300, 10, 300);
  rect(520, 300, 10, 300);  
  fill(255);
  rect(120, gPos, 50,50);
  rect(320, rPos, 50,50);
 rect(520, yPos, 50,50); 
}
 
void mouseDragged()
{
  if(mousePressed)
  {
    if(mouseX >95&&mouseX < 145)
    {
      gPos = constrain(mouseY, 165, 440);
      gValue = int(map(gPos,165, 440, 0,220));
    }
 
    if(mouseX > 295&&mouseX < 345)
    {
      rPos = constrain(mouseY, 165, 440);
      rValue = int(map(rPos,165, 440, 0,220));
    }
 
    if(mouseX > 495&&mouseX < 545)
    {
      yPos = constrain(mouseY, 165, 440);
      yValue = int(map(yPos,165, 440, 0,160));      
    }
 
    println(gValue+","+rValue+","+yValue);
    myPort.write(""+gValue+","+rValue+","+yValue);
    myPort.write("\n");
 
  } 
}
Arduino
// pins for the LEDs:
 const int redPin = 11;
 const int greenPin = 10;
 const int bluePin = 9;
 
void setup() {
   // initialize serial:
   Serial.begin(9600);
   // make the pins outputs:
   pinMode(redPin, OUTPUT); 
   pinMode(greenPin, OUTPUT); 
   pinMode(bluePin, OUTPUT); 
 
}
 
void loop() {
   // if there's any serial available, read it:
   while (Serial.available() > 0) {
 
     // look for the next valid integer in the incoming serial stream:
     int red = Serial.parseInt(); 
     // do it again:
     int green = Serial.parseInt();
     // do it again:
     int blue = Serial.parseInt(); 
 
     // look for the newline. That's the end of your
     // sentence:
     if (Serial.read() == '\n') {
       // fade the red, green, and blue legs of the LED: 
       analogWrite(redPin, red);
       analogWrite(greenPin, green);
       analogWrite(bluePin, blue);
 
     }
   }
   delay(20);
}
courses/intro.prototyping.fall2013.dec10.txt · Last modified: 2013/12/17 01:49 by rickard