Arduino x LEDs x Processing

Using hover states in Processing and a serial connection to toggle LEDs with Arduino.

Layout

arduino breadboard multiple leds

Arduino code:


int input;
int i,j;

int pin[] = {
  2,3,4,5,6,7,8,9};

void setup() {
  Serial.begin(9600);  
  //my LEDs are connected on pins 2 through 9
  for (i = 2; i < 10; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  if (Serial.available() > 0) {
    input = Serial.read();   

    for (j = 2; j < 10; j++) {
      if (input == j) {
        digitalWrite(pin[j-2], HIGH);
      } 
      else {
        digitalWrite(pin[j-2], LOW);
      }
    }
  }
}

Processing code:


import processing.serial.*;
Serial myPort;

int s = 100;

void setup() {
  size(800, 300);
  background(0);
  //  println("Available serial ports:");
  //  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
  smooth();
  noStroke();
}

void draw() {
  background(0);
  for (int i = 0; i < 8; i++) {
    if ((mouseX) > s*i && (mouseX) < (s*i)+s) { 
      fill(#ffde14); 
      myPort.write(i+2);
    } 
    else { 
      fill(25);
    }
    rect(s*i, 100, s, s);
  }   
}