Homework: Blink, Baby, Blink!

Tried┬ádoing serial communications between Processing and Arduino. The LEDs are blinking according to the bouncing ball through mapping the bouncing ball’s y position to the number of LEDs. This number is sent through Processing to the Arduino board, where it will turn the LED in an array of LEDs with that number on.

The result is kind of buggy, maybe a┬áchange in the positioning of lights would help. But I think part of it is also the large difference in the mapping, so it would probably work much better with a lot more LEDs, which I don’t have. And also there’s probably a slight lag in the serial communication.

Screen Shot 2014-09-11 at 1.01.43 AM

Because Code Snippets is being mean to me, I’m just going to copy and paste the code here:

Processing:

import processing.serial.*;

Serial myPort; // create new serial connection named myPort
int x = 0;
int y = 0;
int xspeed = 10;
int yspeed = 10;

void setup() {
size(900,300);
smooth();
String portName = Serial.list()[17];
myPort = new Serial(this, portName, 9600);
}

void draw() {
background(255);
x = x + xspeed;
y = y + yspeed;
float m = map(y, 0, height, 0, 10);
byte val = byte(m);
myPort.write(val);
// println(val);

if ((x > width) || (x < 0)) {
xspeed = xspeed * -1;
}

if ((y > height) || (y < 0)) {
yspeed = yspeed * -1;
}

// Display circle at x location
noStroke();
fill(random(255),random(255),random(255));
ellipse(x,y,32,32);
}

——————————————————–

Arduino:

char val; // create a variable named val
int ledPins[] = { 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }; // array of LEDs

void setup() {
for(int i=0; i<10; i++) pinMode(ledPins[i], OUTPUT); // declare all pins in array as output
Serial.begin(9600);
}

void loop() {
if (Serial.available()) {
val = Serial.read(); // val equals the number being sent over from Processing
}
digitalWrite(ledPins[val], HIGH); // turn the LED with the current number from Processing on
delay(100); // delay 100
digitalWrite(ledPins[val], LOW); // turn the LED with the current number from Processing off
}

 

Leave a Reply

Your email address will not be published.