Created
February 20, 2013 15:10
-
-
Save possan/4996205 to your computer and use it in GitHub Desktop.
hackday radio hardware code
This file contains hidden or 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
int LED = 11; | |
int SWITCH = 10; | |
// int VOLUME = A1; | |
int KNOB0 = 9; | |
int KNOB1 = 8; | |
// 00 -> 10 -> 11 -> 00 | |
int knobstate = 0; | |
int switchstate = 0; | |
long nextbeat = 0; | |
long tempo = 120; | |
long timedelay = 1000 / 120; | |
long lightoff = 0; | |
void setup() { | |
pinMode(LED, OUTPUT); | |
pinMode(SWITCH, INPUT); | |
pinMode(KNOB0, INPUT); | |
pinMode(KNOB1, INPUT); | |
Serial.begin(9600); | |
updatetempo(); | |
for (int i=0; i<4; i++) { | |
digitalWrite(LED, 1); | |
delay(150); | |
digitalWrite(LED, 0); | |
delay(150); | |
} | |
} | |
void updatetempo() { | |
timedelay = 60000 / tempo; | |
nextbeat = millis() + timedelay; | |
Serial.print("timedelay="); | |
Serial.print(timedelay); | |
Serial.print("\n"); | |
} | |
char buf[100]; | |
int bp = 0; | |
void loop() { | |
if (Serial.available()) { | |
char c = Serial.read(); | |
if (c == '\n') { | |
// parse | |
if (strncmp(buf, "bpm=", 4) == 0) { | |
tempo = atoi(buf+4); | |
updatetempo(); | |
} | |
bp = 0; | |
memset(buf, 0, 100); | |
} else { | |
buf[bp++] = c; | |
} | |
} | |
int knob = digitalRead(KNOB0)*2 + digitalRead(KNOB1); | |
if (knob != knobstate) { | |
if (knob == 0 && knobstate == 1) { Serial.print("knob-up\n"); } | |
if (knob == 1 && knobstate == 0) { Serial.print("knob-down\n"); } | |
knobstate = knob; | |
} | |
int sw = digitalRead(SWITCH); | |
if (sw != switchstate) { | |
if (sw == 1) { Serial.print("switch-off\n"); } | |
if (sw == 0) { Serial.print("switch-on\n"); } | |
switchstate = sw; | |
} | |
if (millis() > nextbeat) { | |
// Serial.print("beat\n"); | |
// Serial.println(timedelay); | |
// Serial.println("\n"); | |
nextbeat = millis() + timedelay; | |
digitalWrite(LED, 1); | |
lightoff = millis() + 20; | |
}; | |
digitalWrite(LED, millis() < lightoff); | |
delay(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment