Marble Beats
Description:
Marbles roll down a track and hit force sensors as they roll down towards the bottom. Every time a marble hits one of the 4 force sensors the track attached to the sensor turns on or off.
Code
Arduino
/*
* Musical Instrument Synthesis
*/
// Output
int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
// Program variables
int redVal = 255; // Variables to store the values to send to the pins
int greenVal = 1; // Initial values are Red full, Green and Blue off
int blueVal = 1;
int j = 0; // Loop counter
int wait = 50; // 50ms (.05 second) delay; shorten for faster fades
//fsr
byte fsrPins[] = {0, 1, 2, 3};
int val = 0; // variable to store the value coming from the sensor
//main setup
void setup() {
Serial.begin(9600);
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
//main program
//fsr
void loop() {
for (int i = 0; i < 4; i++) {
val = analogRead(fsrPins[i]);
if (val > 10) {
Serial.println(i);
delay(500);
}
}
//LEDs
j += 1; // Increment counter
if (j < 255) // First phase of fades
{
redVal -= 1; // Red down
greenVal += 1; // Green up
blueVal = 1; // Blue low
}
else if (j < 509) // Second phase of fades
{
redVal = 1; // Red low
greenVal -= 1; // Green down
blueVal += 1; // Blue up
}
else if (j < 763) // Third phase of fades
{
redVal += 1; // Red up
greenVal = 1; // Green low
blueVal -= 1; // Blue down
}
else // Re-set the counter, and start the fades again
{
j = 1;
}
analogWrite(redPin, redVal); // Write current values to LED pins
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
}
Python
#!usr/bin/env python
# encoding: utf-8
"""
synthesis.py
Created by Rowyn McDonald on 2011-04-07
"""
import serial
import audiere
NUM_INPUTS = 4
def getUserInput(prompt = None, help = None):
"""Displays help to user and prompts to enter input"""
if prompt is None:
prompt = '> '
if help:
print help
return raw_input(prompt)
class Synthesizer(object):
def __init__(self, num_tracks):
self.serial = serial.Serial('COM5')
device = audiere.open_device()
self.tracks = []
for track_num in xrange(num_tracks):
filename = 'wav/track%d.wav' % track_num
track = device.open_file(filename)
track.repeating = 1
track.volume = 0
self.tracks.append(track)
def getTrack(self):
track_line = self.serial.readline()
return int(track_line.strip())
def run(self):
audio_on = False
while True:
print "in loop"
track_number = self.getTrack()
print track_number
if self.tracks[track_number].volume:
self.tracks[track_number].volume = 0
else:
self.tracks[track_number].volume = 1
# Only triggered the first time a sensor is hit
if not audio_on:
for track in self.tracks:
track.play()
audio_on = True
if __name__ == '__main__':
synthesizer = Synthesizer(NUM_INPUTS)
synthesizer.run()
(1 vote)