_ project
_ multimedia
_ codes & links
_ references
Pistaduino
/*
* created by Alvaro Fernandez Xeva, https://xeva.hotglue.me/?inicio
*
* PISTADUINO PROJECT
*
* Ultrasonic sensor Pins:
VCC: +5VDC
Trig: Trigger (INPUT) - Pin 5
Echo: Echo (OUTPUT) - Pin 4
GND: GND
*/

int trigPin = 5; //Trig - green Jumper
int echoPin = 4; //Echo - yellow Jumper
long duration, cm, inches;

void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop()
{


// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
cm = (duration/2) / 29.1;
// inches = (duration/2) / 74;

Serial.println(cm);

delay(250);
}

/*
PROCESSING CODE FOR PISTADUINO

import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;
Minim minim;
AudioPlayer vox;
AudioPlayer guitar;
AudioPlayer bass;
AudioPlayer accord;
AudioPlayer pedal;

import processing.serial.*;

float distance; // value from HC-SR04 sensor

PImage guitarrista, contrabajista, acordeonista, vocalista;

Serial myPort;

void setup() {
size(800, 600);

// List all the available serial ports
// if using Processing 2.1 or later, use Serial.printArray()
println(Serial.list());

// I know that the forth port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[3].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[3], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');

minim = new Minim(this);
// Introduce every track of the song and start playing them with no volume
guitar = minim.loadFile("AcGtr.mp3");
guitar.loop();
guitar.mute();
bass = minim.loadFile("Bass_DI.mp3");
bass.loop();
bass.mute();
pedal = minim.loadFile("PedalSteel.mp3");
pedal.loop();
pedal.mute();
accord = minim.loadFile("Accordian.mp3");
accord.loop();
accord.mute();
vox = minim.loadFile("LeadVox.mp3");
vox.loop();
vox.mute();

// Then assign an image to each variable
guitarrista = loadImage("guitarrista.png");
contrabajista = loadImage("contrabajista.png");
acordeonista = loadImage("acordeonista.png");
vocalista = loadImage("vocalista.png");
}

void draw() {
// set the background color depending on the images you want to draw
// My images are 4 white silhouettes so I set a black background.
background(0);

// When the distance is less than 40 cm,
// make the guitar track play and the guitarrist silhouette light up
if (distance < 40) {
guitar.unmute();
tint(255,255,0);
image(guitarrista,0,0,width,height);
}
else if (distance >= 40){
guitar.mute();
tint(255,0);
image(guitarrista,0,0,width,height);
// repeat the proccess for every track and silhouette.
}
if (distance < 30) {
bass.unmute();
pedal.unmute();
tint(255,255,0);
image(contrabajista,0,0,width,height);
}
else if (distance >= 30){
bass.mute();
pedal.mute();
tint(255,0);
image(contrabajista,0,0,width,height);
}
accord.setGain(-15);
if (distance < 20) {
accord.unmute();
tint(255,255,0);
image(acordeonista,0,0,width,height);
}
else if (distance >= 20){
accord.mute();
tint(255,0);
image(acordeonista,0,0,width,height);
}
if (distance < 10) {
vox.unmute();
tint(255,255,0);
image(vocalista,0,0,width,height);
}
else if (distance >= 10){
vox.mute();
tint(255,0);
image(vocalista,0,0,width,height);
}
}

void serialEvent(Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');

if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
//convert the resulting substrings into an integer array:
distance = float(inString);
println(distance);
}
}
*/
Version 1: distance sensor
Version 2: mouse cursor (Processing code)
Test it right now if you lack an arduino!
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;
Minim minim;
AudioPlayer vox;
AudioPlayer guitar;
AudioPlayer bass;
AudioPlayer accord;
AudioPlayer pedal;

import processing.serial.*;

float distance; // value from HC-SR04 sensor

PImage guitarrista, contrabajista, acordeonista, vocalista;

Serial myPort;

void setup() {
size(800, 600);

// List all the available serial ports
// if using Processing 2.1 or later, use Serial.printArray()
println(Serial.list());

// I know that the forth port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[3].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[3], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');

minim = new Minim(this);
// Introduce every track of the song and start playing them with no volume
guitar = minim.loadFile("AcGtr.mp3");
guitar.loop();
guitar.mute();
bass = minim.loadFile("Bass_DI.mp3");
bass.loop();
bass.mute();
pedal = minim.loadFile("PedalSteel.mp3");
pedal.loop();
pedal.mute();
accord = minim.loadFile("Accordian.mp3");
accord.loop();
accord.mute();
vox = minim.loadFile("LeadVox.mp3");
vox.loop();
vox.mute();

// Then assign an image to each variable
guitarrista = loadImage("guitarrista.png");
contrabajista = loadImage("contrabajista.png");
acordeonista = loadImage("acordeonista.png");
vocalista = loadImage("vocalista.png");
}

void draw() {
// set the background color depending on the images you want to draw
// My images are 4 white silhouettes so I set a black background.
background(0);

// When the distance is less than 40 cm,
// make the guitar track play and the guitarrist silhouette light up
if (mouseX < 600) {
guitar.unmute();
tint(255,255,0);
image(guitarrista,0,0,width,height);
}
else if (mouseX >= 600){
guitar.mute();
tint(255,0);
image(guitarrista,0,0,width,height);
// repeat the proccess for every track and silhouette.
}
if (mouseX < 450) {
bass.unmute();
pedal.unmute();
tint(255,255,0);
image(contrabajista,0,0,width,height);
}
else if (mouseX >= 450){
bass.mute();
pedal.mute();
tint(255,0);
image(contrabajista,0,0,width,height);
}
accord.setGain(-15);
if (mouseX < 300) {
accord.unmute();
tint(255,255,0);
image(acordeonista,0,0,width,height);
}
else if (mouseX >= 300){
accord.mute();
tint(255,0);
image(acordeonista,0,0,width,height);
}
if (mouseX < 150) {
vox.unmute();
tint(255,255,0);
image(vocalista,0,0,width,height);
}
else if (mouseX >= 150){
vox.mute();
tint(255,0);
image(vocalista,0,0,width,height);
}
}

void serialEvent(Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');

if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
//convert the resulting substrings into an integer array:
distance = float(inString);
println(distance);
}
}
Take a look to this Fritzing link, where you can find and download every single content of the project:
fritzing.org/projects/pistaduino