Assignment 10.2

posted Jul 30, 2010, 9:07 AM by Unknown user
For this sketch I used the X position of the cursor in the Processing window to control both the brightness of an LED connected to pin 9 of my Arduino and the opacity of a rectangle in the Processing window. Here are my codes:
Arduino:
 
int val = 0;
int ledPin = 9;
void setup() {
  Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
  val = Serial.read();
}
analogWrite(ledPin, val);
delay(20);
}
 
and Processing:
 
import processing.serial.*;
Serial myPort;
int valX;
int valY;
void setup() {
  size(255, 255);
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}
void draw() {
  background(0);
  valX = mouseX;
  myPort.write(valX);
  println(valX);
  delay(20);
  fill(0, 255, 0, valX);
  rect(0, 0, 255, 255);
}
 
I tried to use the Y position of the cursor to control another LED, but I don't know how to split the String of values sent from Processing to Arduino.
 
Comments