Go Down

Topic: Serial Communication w/ Arduino + Processing (Read 438 times) previous topic - next topic


Apr 03, 2013, 08:01 pm Last Edit: Apr 03, 2013, 10:27 pm by jmcclaire Reason: 1

First of all I am pretty n00b to Arduino and Processing.

To make things simple, I would like to make the background change brightness using a digital sensor.

I am using a 3 pin PING Ultrasonic sensor from parallax.com and an Arduino Mega 2560. I cannot seem to receive a steady measurement when the Processing sketch is running as well as Arduino. I also have no idea how to make "things" happen in Processing because "something" digital is happening in Arduino.  

If someone could give me an example of how to make something happen in Processing (e.g. Grow the size of a circle the closer you are) depending on how close something is to the digital sensor in Arduino that would be very helpful. I have found countless analog examples, but I need a digital example.

Thank you in advance for all your help!

Code: [Select]

import processing.serial.*;
import processing.video.*;
Serial myPort;

// Size of each cell in the grid, ratio of window size to video size
int videoScale = 18;
// Number of columns and rows in our system
int cols, rows;

Capture video;
PImage img;
void setup() {
 myPort = new Serial(this, Serial.list()[1], 9600);
 size(displayWidth, displayHeight);
 img = loadImage ("tma_logo_white.png");

 // Initialize columns and rows
 cols = width/videoScale;
 rows = height/videoScale;
 video = new Capture(this, width, height);

void draw() {
 // Read image from the camera
 if (video.available()) {
 background(mouseX, mouseY, pingValue);
 image (img, 83, 9);

 // Begin loop for columns
 for (int i = 5; i+5 < cols; i++) {
   // Begin loop for rows
   for (int j = 4; j+4 < rows; j++) {

     // Where are we, pixel-wise?
     int x = i*videoScale;
     int y = j*videoScale;
     // Looking up the appropriate color in the pixel array
     color c = video.pixels[x + y*video.width];

     if (mouseY < y) {
       ellipse(x, y, videoScale, videoScale);
     else {
       rect(x-8, y, videoScale-1, videoScale-1);

void serialEvent (Serial myPort) {

Code: [Select]

#include <NewPing.h>

const int pingPin = 8;
int distance = 0;
int pingValue;

void setup()
 // start serial port at 9600 bps:

void loop() {
 //while(Serial.available()>0) Serial.read();
 // establish variables for duration of the ping,
 // and the distance result in inches and centimeters:
 long duration, inches, cm;
 pinMode(pingPin, OUTPUT);
 digitalWrite(pingPin, LOW);
 digitalWrite(pingPin, HIGH);
 digitalWrite(pingPin, LOW);
 pinMode(8, INPUT);   // digital sensor is on digital pin 8
 duration = pulseIn(pingPin, HIGH);
 distance = (duration/2) / 29.1;

 // convert the time into a distance
 inches = microsecondsToInches(duration);
 cm = microsecondsToCentimeters(duration);
 Serial.print("in, ");
//  Serial.print(cm);
//  Serial.print("cm");

long microsecondsToInches(long microseconds)
 // According to Parallax's datasheet for the PING))), there are
 // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
 // second).  This gives the distance travelled by the ping, outbound
 // and return, so we divide by 2 to get the distance of the obstacle.
 // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
 return microseconds / 74 / 2;

long microsecondsToCentimeters(long microseconds)
 // The speed of sound is 340 m/s or 29 microseconds per centimeter.
 // The ping travels out and back, so to find the distance of the
 // object we take half of the distance travelled.
 return microseconds / 29 / 2;
Creative Technologist at The Martin Agency

Go Up