Arduino Code:
#include <FastLED.h>
#define NUM_LEDS 60
#define DATA_PIN 3
CRGB leds[NUM_LEDS];
int litCount = 0;
void setup() {
Serial.begin(9600);
pinMode(10, INPUT);
pinMode(2, INPUT);
pinMode(4, INPUT);
pinMode(7, INPUT);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(50);
}
void loop() {
int sensor0 = digitalRead(10);
int sensor1 = digitalRead(2);
int sensor2 = digitalRead(4);
int sensor3 = digitalRead(7);
if (sensor0 == 0){
clearLED();
starttoothBrushAnimation();
}
else if (sensor1 == 0) {
clearLED();
startplantAnimation();
}
else if (sensor2 == 0) {
clearLED();
startlaundryAnimation();
}
else if (sensor3 == 0) {
clearLED();
startshowerAnimation();
}
// send the values keeping this format
Serial.print(sensor0);
Serial.print(","); // put comma between sensor values
Serial.print(sensor1);
Serial.print(",");
Serial.print(sensor2);
Serial.print(",");
Serial.print(sensor3);
Serial.println(); // add linefeed after sending the last sensor value
// too fast communication might cause some latency in Processing
// this delay resolves the issue
delay(20);
}
void clearLED(){
for (int i = 0; i < NUM_LEDS; i=i+1) {
leds[i] = CRGB(0, 0, 0);
}
FastLED.show();
//delay(50);
}
void starttoothBrushAnimation() {
for (int i = 0; i < 10; i=i+1) {
leds[i] = CRGB(173, 216, 230);
FastLED.show();
delay(50);
}
}
void startplantAnimation(){
for (int i = 0; i < 50; i=i+1) {
leds[i] = CRGB(173, 216, 230);
FastLED.show();
delay(50);
}
}
void startlaundryAnimation(){
for (int i = 0; i < 40; i=i+1) {
leds[i] = CRGB(173, 216, 230);
FastLED.show();
delay(50);
}
}
void startshowerAnimation(){
for (int i = 0; i < 25; i=i+1) {
leds[i] = CRGB(173, 216, 230);
FastLED.show();
delay(50);
}
}
Processing Code:
import processing.video.*;
import processing.serial.*;
Serial serialPort;
int NUM_OF_VALUES_FROM_ARDUINO = 4;
int arduino_values[] = new int[NUM_OF_VALUES_FROM_ARDUINO];
Movie toothBrush;
Movie plant;
Movie laundry;
Movie shower;
Movie myMovie;
void setup() {
size(1250, 700);
//size(displayWidth, displayHeight);
myMovie = new Movie(this, "toothbrush.mp4");
toothBrush = new Movie(this, "toothbrush.mp4");
plant = new Movie(this, "plant.mp4");
laundry = new Movie(this, "laundry.mp4");
shower = new Movie(this, "shower.mp4");
printArray(Serial.list());
serialPort = new Serial(this, "/dev/cu.usbmodem101", 9600);
}
void draw() {
getSerialData();
if(arduino_values[0] == 0) {
switchVideo(toothBrush);
} else if(arduino_values[1] == 0){
switchVideo(plant);
} else if(arduino_values[2]==0){
switchVideo(laundry);
} else if(arduino_values[3] == 0){
switchVideo(shower);
}
if (myMovie.available()) {
myMovie.read();
}
image(myMovie, 0, 0, width, height);
}
//void keyPressed() {
// if (key == 't') {
// myMovie = new Movie(this, "toothbrush.mp4");
// myMovie.play();
// } else if (key == 'p') {
// myMovie = new Movie(this, "plant.mp4");
// myMovie.play();
// } else if (key == 'l') {
// myMovie = new Movie(this, "laundry.mp4");
// myMovie.play();
// } else if (key == 's') {
// myMovie = new Movie(this, "shower.mp4");
// myMovie.play();
// }
//}
void switchVideo(Movie newMovie) {
if (myMovie != newMovie) {
myMovie.stop();
myMovie = newMovie;
myMovie.loop();
}
}
void getSerialData() {
while (serialPort.available() > 0) {
String in = serialPort.readStringUntil( 10 ); // 10 = '\n' Linefeed in ASCII
if (in != null) {
print("From Arduino: " + in);
String[] serialInArray = split(trim(in), ",");
if (serialInArray.length == NUM_OF_VALUES_FROM_ARDUINO) {
for (int i=0; i<serialInArray.length; i++) {
arduino_values[i] = int(serialInArray[i]);
}
}
}
}
}