DarkStick

Assignment: Input / Output Coincidence Lab Assignment

Collaborators:

Assignment: Input / Output Coincidence Lab Assignment
Collaborators:

Description

The SithStick may look harmless from the outside...

Looks harmless

...but put your eye to it and you're suddenly gazing down the throat of the abyss. From the flashing lights and the sound of Darth Vader's theme—or is it the Halloween theme song?—you know you're headed for trouble.

Into the abyss

But wait! These dark things are afraid of the light side... shine your flashlight down the abyss and the lights stop, the evil music fades, and you're back in South Hall preparing for another TUI lab......

The SithStick uses a light sensor inside the tube to determine when to play the music and flash the lights. On the side is a potentiometer, which the intrepid user can use to speed or slow the music. And a force-sensitive resistor allows for song selection.

Components

  • Arduino/board
  • Shipping tube
  • 1 FSR
  • 3 LEDs
  • 1 Piezo
  • 1 Potentiometer

Code

#include <Tone.h>

// Plays notes from the scale. Speed is controlled by the pot.
// Music only played when it's dark in the tube. FSR selects music
// (Imperial March or Halloween theme).

Tone freq1;
int piezoPin = 7;
int potPin = 0;
int lightPin = 1;
int fsrPin = 2;
int noteLength = 0; //initial note length
int ledCount = 3;
int ledPins[] = {9,10,11}; // an array of pin numbers to which LEDs are attached
int currentLED = 0;
int howBright = 0;
int brightnessThreshold = 5;
int theForce = 0;

//Scale definitions
const int harmonicMinorScale[] = { 0, NOTE_C4, NOTE_D4, NOTE_DS4, NOTE_F4, NOTE_G4, NOTE_GS4, NOTE_B4, NOTE_C5 };
const int fullScale[] = { 0, NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4, NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5};

//Song definitions
uint8_t marchSequence[] = { 5,5,5,1,8,5,1,8,5,12,12,12,13,8,4,1,8,5,17,5,5,17,16,15,14,13,14,6,11,10,9,8,7,8,1,4,1,8,5,1,8,5,0};
uint8_t marchLen[] = { 3,3,3,2,1,3,2,1,6, 3, 3, 3, 2,1,3,2,1,6, 3,2,1, 3, 2, 1, 1, 1, 3,1, 3,2,1,1,1,3,1,3,2,1,3,2,1,6,24};
uint8_t hwSequence[] = { 8,1,1,8,1,1,8,1,9,1,8,1,1,8,1,1,8,1,9,1,8,1,1,8,1,1,8,1,9,1,8,1,1,8,1,1,8,1,9,1}; //halloween
uint8_t hwLen[] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};

void setup()
{
freq1.begin(piezoPin);
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}

void playNote(uint8_t number, long noteLength)
{
freq1.play(fullScale[number], noteLength*.9);
}

void loop()
{
int i;
theForce = analogRead(fsrPin);
if (theForce < 600) {
for(i = 0; i < sizeof(marchSequence); i ++)
{
noteLength = analogRead(potPin);
noteLength = noteLength / 5 * marchLen[i];
howBright = analogRead(lightPin);
Serial.print(marchSequence[i], 7);
Serial.print(' ');
if(howBright < brightnessThreshold){
digitalWrite(ledPins[currentLED], HIGH);
playNote(marchSequence[i], noteLength);
}
delay(noteLength);
digitalWrite(ledPins[currentLED], LOW);
currentLED = (currentLED + 1) % ledCount;
delay(10);
}
} else {
for(i = 0; i < sizeof(hwSequence); i ++){
noteLength = analogRead(potPin);
noteLength = noteLength / 5 * hwLen[i];
howBright = analogRead(lightPin);
Serial.print(hwSequence[i], 7);
Serial.print(' ');
if(howBright < brightnessThreshold){
digitalWrite(ledPins[currentLED], HIGH);
playNote(hwSequence[i], noteLength);
}
delay(noteLength);
digitalWrite(ledPins[currentLED], LOW);
currentLED = (currentLED + 1) % ledCount;
delay(10);
}
}
Serial.println();
delay(0);
}