Created
June 28, 2013 15:11
-
-
Save cowdinosaur/5885413 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Servo.h> | |
#include <math.h> | |
Servo myservo; // create servo object to control a servo | |
int potVal = 0; // variable to read the value from the analog pin | |
int processedPotVal = 0; | |
int photoVal; // variable to read the value from the photoresistor | |
const int ledPin1 = 3; | |
const int ledPin2 = 4; | |
const int ledPin3 = 5; | |
const int ledPin4 = 6; | |
const int servoPin = 9; | |
const int buzzerPin = 10; | |
const int potAPin = A0; | |
const int photoAPin = A1; | |
const int songLength = 20; | |
const int tempo = 20; | |
//int notes[] = { 'C', 'b', 'g', 'C', 'b', 'e', 'R', 'C', 'c', 'g', 'a', 'C' }; | |
char notes[21] = "dafcdadcfdacdafcdaag"; | |
//int beats[] = { 16, 16, 16, 8, 8, 16, 32, 16, 16, 16, 8, 8 }; | |
int beats[] = {8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8}; | |
int lightState = 0; | |
void setup() { | |
pinMode(ledPin1, INPUT); | |
pinMode(ledPin2, INPUT); | |
pinMode(ledPin3, INPUT); | |
pinMode(ledPin4, INPUT); | |
pinMode(potAPin, INPUT); | |
myservo.attach(servoPin); | |
Serial.begin(14400); | |
} | |
void loop() { | |
randomSequence(); | |
potVal = analogRead(potAPin); | |
potVal = map(potVal, 0, 1023, 0, 179); | |
potVal = (potVal / 5 ) * 5; | |
if (potVal != processedPotVal) { | |
Serial.print("Potentiometer: "); | |
Serial.println(potVal); | |
processedPotVal = potVal; | |
myservo.write(processedPotVal); | |
} | |
photoVal = analogRead(photoAPin); | |
//Serial.print("Photo resistor: "); | |
//Serial.println(photoVal); | |
if (photoVal > 1000) { | |
for (int i = 0; i < songLength; i++) { // step through the song arrays | |
int duration = beats[i] * tempo; // length of note/rest in ms | |
if (notes[i] == ' ') { // is this a rest? | |
delay(duration); // then pause for a moment | |
} else { | |
tone(buzzerPin, frequency(notes[i]), duration); | |
delay(duration); // wait for tone to finish | |
} | |
delay(tempo/10); // brief pause between notes | |
} | |
} else { | |
delay(500); | |
} | |
} | |
void randomSequence() { | |
if (lightState == 0) { | |
digitalWrite(ledPin1, HIGH); | |
digitalWrite(ledPin2, HIGH); | |
digitalWrite(ledPin3, HIGH); | |
digitalWrite(ledPin4, HIGH); | |
lightState++; | |
} else if (lightState == 1) { | |
digitalWrite(ledPin1, HIGH); | |
digitalWrite(ledPin2, LOW); | |
digitalWrite(ledPin3, HIGH); | |
digitalWrite(ledPin4, LOW); | |
lightState += 1; | |
} else if (lightState == 2) { | |
digitalWrite(ledPin1, HIGH); | |
digitalWrite(ledPin2, HIGH); | |
digitalWrite(ledPin3, LOW); | |
digitalWrite(ledPin4, LOW); | |
lightState += 1; | |
} else if (lightState == 3) { | |
digitalWrite(ledPin1, LOW); | |
digitalWrite(ledPin2, HIGH); | |
digitalWrite(ledPin3, LOW); | |
digitalWrite(ledPin4, HIGH); | |
lightState = 0; | |
} | |
} | |
int frequency(char note) | |
{ | |
// This function takes a note character (a-g), and returns the | |
// corresponding frequency in Hz for the tone() function. | |
int i; | |
const int numNotes = 8; // number of notes we're storing | |
// For the "char" (character) type, we put single characters | |
// in single quotes. | |
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'}; | |
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523}; | |
// Now we'll search through the letters in the array, and if | |
// we find it, we'll return the frequency for that note. | |
for (i = 0; i < numNotes; i++) { // Step through the notes | |
if (names[i] == note) { // Is this the one? | |
return(frequencies[i]); // Yes! Return the frequency | |
} | |
} | |
return(0); // We looked through everything and didn't find it, | |
// but we still need to return a value, so return 0. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment