Description
I created an interactive Piet Mondrian painting using Processing, Arduino, a LED and a rubber duck.
First, when I run Processing and click on the artboard, a piece of music will be played, and the color blocks of the Piet Mondrian will appear and start dancing.
Secondly, when I squeeze the rubber duck, a duck image will appear on my Processing artboard and move up and down based on the level of pressure I put on the actual rubber duck. The LED light inside the duck will also increase based on the level of pressure. The FSR inside the rubber duck transfers the pressure into Arduio, from Arduio to the port, from the port to the Processing. In Processing, the level of pressure converts into the position of a duck image.
Components
- 1 Arduino
- 1 LEDs
- 4 Resistors
- 1 FSR
- 1 Breadboard
- 1 Rubber Duck
- 1 Processing
Code
Arduino
int fsrAnalogPin = A0; int LEDpin = 11; int fsrReading; int LEDbrightness; void setup(void) { Serial.begin(9600); pinMode(LEDpin, OUTPUT); } void loop(void) { fsrReading = analogRead(fsrAnalogPin); Serial.println(fsrReading); LEDbrightness = map(fsrReading, 0, 1023, 0, 255); analogWrite(LEDpin, LEDbrightness); delay(100); }
Processing
PImage img; // Declare variable 'img' of type PImage import processing.serial.*; // Change this to the portname your Arduino board String portname = "/dev/cu.usbmodem1411"; // or "COM5" Serial port; String buf=""; int cr = 13; // ASCII return == 13 int lf = 10; // ASCII linefeed == 10 int serialVal = 0; Maxim maxim; AudioPlayer player; float power=0; //float threshold = 0.35;// vary this until the square appears on the beats float threshold = 0.35;// vary this until the square appears on the beats int wait = 0; boolean playit = false; float x = 0; float y = 0; void setup() { size(800,450); frameRate(20); // this is the framerate. Tweak for performance. maxim = new Maxim(this); player = maxim.loadFile("beat1.wav"); player.setLooping(true); player.setAnalysing(true); smooth(); noStroke(); port = new Serial(this, portname, 9600); img = loadImage("duck.png"); } void draw() { background(255); println(wait); stroke(0); strokeWeight(6); line(90,0,90,500); //first line left line(150,0,150,500); // second line left line(200,0,200,80); // third line left line(400,300,400,380); // fourth line left line(460,0,460,500); // middle line line(520,80,520,500); // fourth line right line(700,380,700,500); // third line right line(720,0,720,80); // second line right line(750,0,750,380); // first line right line(0,40,150,40);//first line top line(460,40,750,40); // first line top, middle line(150,80,800,80); //second line top line(460,150,800,150); //third line top line(0,300,460,300); //left, second line bottom line(0,380,800,380); //left, first line bottom line(150,420,700,420); //middle, first line bottom if (playit) { //fill(0); player.play(); power = player.getAveragePower(); rectMode(CENTER); noStroke(); fill(235,215,0); //ellipse(mouseX,mouseY,power*500,power*500); rect(x+120,y+18,32+(power*70),14+(power*70)); //second yellow rect left rect(x+635,y+265,200+(power*70),200+(power*70));//big yellow rect rect(x+776,y+115,25+(power*70),40+(power*70));//first yellow rect right fill(185,0,0); rect(x+305,y+190,250+(power*180),160+(power*180)); //big red rect rect(x+490,y+400,30+(power*70),11+(power*70)); //small red rect, bottom fill(0,40,105); rect(x+43,y+417,77+(power*20),57+(power*20));//left bottom blue rect rect(x+610,y+437,165+(power*20),20+(power*20));//middle bottom blue rect rect(x+590,y+60,242+(power*20),24+(power*20));//top blue rect if (power>threshold && wait < 0) { noStroke(); fill(235,215,0); rect(x+43,y+19,87,37);//first yellow rect left fill(185,0,0); rect(x+153+152,y+383+18,305,36); //long red rect rect(x+735,y+19,25,38); //small red rect, top fill(0,40,105); rect(x+44,y+341,88,75);//left, second bottom blue rect wait=4; } wait--; } int xDuck = int(random(800)); int yDuck = int(random(450)); image(img, (1600-serialVal)/2, (900-serialVal)/2); } // called whenever serial data arrives void serialEvent(Serial p) { int c = port.read(); if (c != lf && c != cr) { buf += char(c); } if (c == lf) { serialVal = int(buf); println("val="+serialVal); buf = ""; } } void mousePressed() { playit = !playit; if (playit) { player.play(); } else { player.stop(); } }