Processing:
import processing.video.*;
import processing.serial.*;
import processing.sound.*;
Movie myMovie;
ArrayList<Heart> hearts;
boolean spawnHearts = false;Serial serialPort;
SoundFile laugh; //sound
SoundFile mb;
SoundFile sad;
int NUM_OF_VALUES_FROM_PROCESSING = 1;
int processing_values[] = new int[NUM_OF_VALUES_FROM_PROCESSING];
int NUM_OF_VALUES_FROM_ARDUINO = 1;
int arduino_values[] = new int[NUM_OF_VALUES_FROM_ARDUINO];
void setup() {
size(1080,1560);
myMovie = new Movie(this, "mambo.mp4");
myMovie.loop();
hearts = new ArrayList<Heart>();
laugh = new SoundFile(this, "laugh.MP3"); //sound
mb = new SoundFile(this, "mb.MP3");
sad = new SoundFile(this, "sad.mp3");
printArray(Serial.list());
serialPort = new Serial(this, "COM3", 9600);
}
void draw() {
if (myMovie.available()) {
myMovie.read();
}
if (mousePressed) {
//HAPPY
if (mouseY>870 && mouseY<11166){
processing_values[0]=1; //state 1 is happy
laugh.play(); //sound
}
//SAD
if (mouseY<870){
processing_values[0]=2; //state 2 is sad
sad.play();
}
//ANGRY
if (mouseY>1166){
processing_values[0]=3; //state 3 is angry
mb.play();
}
}
sendSerialData();
image(myMovie, 0, 0);
if (spawnHearts) {
for (int i = 0; i < 5; i++) {
hearts.add(new Heart(random(width), random(height), random(5, 15)));
}
}
for (Heart h : hearts) {
h.display();
}
}
void mousePressed() {
if (mouseX > width / 3 && mouseX < 2 * width / 3 &&
mouseY > height / 3 && mouseY < 2 * height / 3) {
spawnHearts = true;
}
}
void mouseReleased() {
spawnHearts = false;
}
class Heart {
float x, y, size;
Heart(float x, float y, float size) {
this.x = x;
this.y = y;
this.size = size;
}
void display() {
fill(255, 0, 0);
noStroke();
beginShape();
vertex(x, y);
bezierVertex(x - size / 2, y - size / 2, x - size, y + size / 3, x, y + size);
bezierVertex(x + size, y + size / 3, x + size / 2, y - size / 2, x, y);
endShape(CLOSE);
}
}
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]);
}
}
}
}
}
// the helper function below sends the variables
// in the "processing_values" array to a connected Arduino
// running the "serial_read_and_write_arduino" sketch
// (You won't need to change this code.)
void sendSerialData() {
String data = "";
for (int i=0; i<processing_values.length; i++) {
data += processing_values[i];
// if i is less than the index number of the last element in the values array
if (i < processing_values.length-1) {
data += ","; // add splitter character "," between each values element
}
// if it is the last element in the values array
else {
data += "\n"; // add the end of data character "n"
}
}
// write to Arduino
serialPort.write(data);
print("To Arduino: " + data); // this prints to the console the values going to Arduino
}
Arduino:
#include <FastLED.h>
#define NUM_LEDS 60 // How many leds on your strip?
#define DATA_PIN 3
#include <Servo.h>
#define NUM_OF_VALUES_FROM_PROCESSING 1 /* CHANGE THIS ACCORDING TO YOUR PROJECT */
Servo myservo_happy;
Servo myservo_sad;
Servo myservo_angry;
int processing_values[NUM_OF_VALUES_FROM_PROCESSING];
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(50);
Serial.begin(9600);
pinMode(2, INPUT); //ROMANTIC
myservo_happy.attach(9); //1-HAPPY
myservo_sad.attach(10); //2-SAD
myservo_angry.attach(11); //3-ANGRY
}
void loop() {
getSerialData();
if (processing_values[0] == 1) {
happy();
} else if (processing_values[0] == 2) {
sad();
} else if (processing_values[0] == 3) {
angry();
}
}
void happy() {
for (int i = 0; i < NUM_LEDS; i = i + 1) {
leds[i] = CRGB(0, 255, 0);
}
FastLED.show();
myservo_angry.write(180);
delay(200);
myservo_angry.write(0);
delay(200);
myservo_sad.write(180);
delay(200);
myservo_sad.write(0);
delay(200);
}
void sad() {
for (int i = 0; i < NUM_LEDS; i = i + 1) {
leds[i] = CRGB(0, 0, 255);
}
FastLED.show();
myservo_happy.write(180);
myservo_happy.write(0);
delay(200);
myservo_sad.write(180);
myservo_sad.write(0);
delay(200);
myservo_angry.write(180);
myservo_angry.write(0);
delay(200);
}
void angry() {
for (int i = 0; i < NUM_LEDS; i = i + 1) {
leds[i] = CRGB(255, 0, 0);
}
FastLED.show();
myservo_angry.write(180);
delay(200);
myservo_angry.write(0);
delay(200);
myservo_sad.write(180);
delay(200);
myservo_sad.write(0);
delay(200);
myservo_happy.write(180);
delay(200);
myservo_happy.write(0);
delay(200);
}
void getSerialData() {
static int tempValue = 0; // the "static" makes the local variable retain its value between calls of this function
static int tempSign = 1;
static int valueIndex = 0;
while (Serial.available()) {
char c = Serial.read();
if (c >= '0' && c <= '9') {
// received a digit:
// multiply the current value by 10, and add the character (converted to a number) as the last digit
tempValue = tempValue * 10 + (c - '0');
} else if (c == '-') {
// received a minus sign:
// make a note to multiply the final value by -1
tempSign = -1;
} else if (c == ',' || c == '\n') {
// received a comma, or the newline character at the end of the line:
// update the processing_values array with the temporary value
if (valueIndex < NUM_OF_VALUES_FROM_PROCESSING) { // should always be the case, but double-check
processing_values[valueIndex] = tempValue * tempSign;
}
// get ready for the new data by resetting the temporary value and sign
tempValue = 0;
tempSign = 1;
if (c == ',') {
// move to dealing with the next entry in the processing_values array
valueIndex = valueIndex + 1;
} else {
// except when we reach the end of the line
// go back to the first entry in this case
valueIndex = 0;
}
}
}
}