Assignment 10.3

posted Aug 1, 2010, 8:01 PM by Unknown user
I spent about 4 hours trying to get either a call-and-response or punctuation system to work when going from processing to the arduino but nothing I tried would work - it would only set on of the 3 leds, flash constantly between the 3 values, just stay bright white, pretty much do anything other than what I wanted to, and being unable to find any good information on what I was trying to do, I wound up having to split up the byte being sent into 3 parts for each of the RGB values. In processing this image (called "colorWheel.png") is displayed:
 
 

 
and the color data of the pixel that the mouse is currently over is remapped so that red values take up the lower third of a byte (0 - 84), greens take up the middle (85 - 170), and blues the highest third (171 - 255). These vales are then sent to arduino via serial, which it uses to set the level of each LED in the RGB LED. Technically, each piece of information is sent separately, their unique ranges allow the receiver to interpret them properly, requiring no punctuation or call-and-response.

PROCESSING CODE:

 
import processing.serial.*;  //import all serial libraries
Serial serPort;              // Create object from Serial class
PImage colorWheel;
 
void setup() {
  colorWheel = loadImage("colorWheel.png");
  size(colorWheel.width, colorWheel.height);
  serPort = new Serial(this, Serial.list()[0], 9600);
  image(colorWheel, 0, 0);
}
void draw() {
  // get color data of pixel currently being moused over
  color mousedColor = get(mouseX, mouseY);
    
  // print color values to terminal (for debugging purposes)
  print("red = ");
  println(byte(red(mousedColor)-128)+128);
  print("green = ");
  println(byte(green(mousedColor)-128)+128);
  print("blue = ");
  println(byte(blue(mousedColor)-128)+128);    

  // send remapped RGB values to serial:
  // (the +128 - 128 thing is there to make the number signed so it can go through byte(), and then make it unsigned again)
  serPort.write(byte(map(red(mousedColor), 0, 255, 0, 84)-128)+128);      //write red value to serial
  serPort.write(byte(map(green(mousedColor), 0, 255, 85, 170)-128)+128);    //write green value to serial
  serPort.write(byte(map(blue(mousedColor), 0, 255, 171, 255)-128)+128);     //write blue value to serial  

  delay(50);
}

ARDUINO CODE:

 
const int led[3] = {9, 10, 11};

void setup() {
  Serial.begin(9600);
}

void loop() {
  //if bytes are available
  if (Serial.available() > 0) {
    int inByte = Serial.read();    //save last serial byte
    if(inByte > 0 && inByte < 85)  //red
      analogWrite(led[0], map(inByte, 0, 84, 0, 255));  
    if(inByte > 84 && inByte < 171)  //green
      analogWrite(led[1], map(inByte, 85, 170, 0, 255));  
    if(inByte > 170 && inByte < 256)  //blue
      analogWrite(led[2], map(inByte, 171, 255, 0, 255));
  }
}
 

 

Comments