Thomas May - Mac Accelerometer via serial to RGB + diffuser.

Submitted by thomas.may on Tue, 02/12/2013 - 22:40

 

The prospect of using the keyboard as an input to control the LEDs on the arduino did not appeal, for which I considered another input group that most mac laptops have, its accelerometer.

The problem was not installing the library nor to retrieve the values from the accelerometer but rather sending them via Serial to the arduino in a way that each axis X,Y,Z  could be assigned to a specific LED. After a long time of trial and error, I came across a Tom Igoe sketch named "Serial RGB controller" The key part of this code to make Processing communicate the accelerometer information in an orderly fashion was with the piece of code below:

// make a comma-separated string:
String colorString = r + "," + g + "," + b + "\n";
// send it out the serial port:
myPort.write(colorString );
}

 

On the arduino side this string was assigned to a int used to give a value to the analog.write.

Components

1 - Arduino

3 - LEDs

3 - 220 Ohm resistors

Assortment of patch cables.

Shot glass

aluminum tape.

Macbook Pro (accelerometer + serial.sender)

Code:Arduino

 

/*
  Serial RGB controller
 
 Reads a serial input string looking for three comma-separated
 integers with a newline at the end. Values should be between 
 0 and 255. The sketch uses those values to set the color 
 of an RGB LED attached to pins 9 - 11.
 
 The circuit:
 * Common-anode RGB LED cathodes attached to pins 9 - 11
 * LED anode connected to pin 13
 
 To turn on any given channel, set the pin LOW.  
 To turn off, set the pin HIGH. The higher the analogWrite level,
 the lower the brightness.
 
 created 29 Nov 2010
 by Tom Igoe
 
 This example code is in the public domain. 
 */
 
String inString = "";    // string to hold input
int currentColor = 0;
int red, green, blue = 0;
 
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
 
  // send an intro:
  Serial.println("\n\nString toInt() RGB:");
  Serial.println();
  // set LED cathode pins as outputs:
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  // turn on pin 13 to power the LEDs:
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
}
 
void loop() {
  int inChar;
 
  // Read serial input:
  if (Serial.available() > 0) {
    inChar = Serial.read();
  }
 
  if (isDigit(inChar)) {
    // convert the incoming byte to a char 
    // and add it to the string:
    inString += (char)inChar; 
  }
 
  // if you get a comma, convert to a number,
  // set the appropriate color, and increment
  // the color counter:
  if (inChar == ',') {
    // do something different for each value of currentColor:
    switch (currentColor) {
    case 0:    // 0 = red
      red = inString.toInt();
      // clear the string for new input:
      inString = ""; 
      break;
    case 1:    // 1 = green:
      green = inString.toInt();
      // clear the string for new input:
      inString = ""; 
      break;
    }
    currentColor++;
  }
  // if you get a newline, you know you've got
  // the last color, i.e. blue:
  if (inChar == '\n') {
    blue = inString.toInt();
 
    // set the levels of the LED.
    // subtract value from 255 because a higher
    // analogWrite level means a dimmer LED, since
    // you're raising the level on the anode:
    analogWrite(11,  red);
    analogWrite(9, green);
    analogWrite(10, blue);
 
    // print the colors:
    Serial.print(",");
    Serial.print(red);
    Serial.print(",");
    Serial.print(green);
    Serial.print(",");
    Serial.println(blue);
 
    // clear the string for new input:
    inString = ""; 
    // reset the color counter:
    currentColor = 0;
  }
 
}
 

Code: Processing

 

import processing.serial.*;
import sms.*;
 
 int segs = 12;
 int steps = 6;
 float rotAdjust = TWO_PI / segs / 2;
 float radius;
 float segWidth;
 float interval = TWO_PI / segs;
 
 Serial myPort;
 
 void setup() {
 size(200, 200);
 background(127);
 smooth();
 ellipseMode(RADIUS);
 noStroke();
 // make the diameter 90% of the sketch area
 radius = min(width, height) * 0.45;
 segWidth = radius / steps;
 
 // swap which line is commented out to draw the other version
 // drawTintWheel();
 
 // open the first serial port in your computer's list
 myPort = new Serial(this, Serial.list()[4], 9600);
 }
 
 
 void draw() {
 int r = int(Unimotion.getSMSX());
 int g = int(Unimotion.getSMSY());
 int b = int(Unimotion.getSMSZ());
print(r);
print(",");
print(g);
print(",");
println(b%255);
 
 // make a comma-separated string:
 String colorString = r + "," + g + "," + b + "\n";
 // send it out the serial port:
 myPort.write(colorString);
 delay(20);
 }

 

IMG_1591.jpg
0
Your rating: None
Drupal theme by Kiwi Themes.