Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
complete Theremin Challenge for Sparki
#include <Sparki.h>
const int ULTRASONIC = 0;
const int RIGHT = 1;
const int CENTER = 2;
const int LEFT = 3;
int freqA = 440;
int freqF = 370;
int freqmidC = 261;
int maxPitchCount = 15;
int state = 0;
int countMax = 500;
int delayPerBeep = 10;
void setup(){
}
void display(int state, int val){
sparki.clearLCD();
sparki.print("state: ");
sparki.println(state);
sparki.print("sensor value: ");
sparki.println(val);
sparki.print("counts left:");
sparki.println(countMax - count);
sparki.updateLCD();
}
void ultrasonicTheremin(){
int maxDist = 60;
int minFreq = 50;
int maxFreq = 10000;
int freq = 0;
int ping = sparki.ping();
if ( ping > 0 && ping <= maxDist){
freq = map(ping, 0, maxDist, minFreq, maxFreq);
sparki.beep(freq);
} else {
sparki.noBeep();
}
display(ULTRASONIC, ping);
}
void play(int sensorValue, int sensorLow, int sensorHigh, int freq){
int pitchMultiplier = map(sensorValue, sensorLow, sensorHigh, 1, maxPitchCount);
sparki.beep(pitchMultiplier*freq);
display(state, sensorValue);
delay(delayPerBeep);
}
void loop(){
for(count = 0; count < countMax; count++){
if (state == ULTRASONIC){
sparki.RGB(RGB_BLUE);
ultrasonicTheremin();
}
if( state == RIGHT){
sparki.RGB(RGB_RED);
play(sparki.lightRight(), 30, 368, freqmidC);
}
if( state == CENTER){
sparki.RGB(RGB_YELLOW);
play(sparki.lightCenter(), 10, 239, freqF);
}
if( state == LEFT){
sparki.RGB(RGB_GREEN);
play(sparki.lightLeft(), 21, 349, freqA);
}
}
state++;
if (state >= 4){
state = ULTRASONIC;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment