Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int lightPin = 0;
- int potPin = 5;
- int lightLED = 9;
- int potLED = 11;
- boolean pentatonic = true;
- boolean debug = false;
- ////////////////////////////
- int minLight;
- int maxLight;
- int lightLevel;
- int lightMap;
- int potMap;
- int toneMap;
- int count;
- int pot;
- void setup() {
- pinMode(potPin, OUTPUT);
- if(debug) {
- Serial.begin(9600); //correct rate fot debugging
- }
- else{
- Serial.begin(31250); //correct rate for MIDI
- pinMode(2, OUTPUT);
- }
- lightLevel = analogRead(lightPin);
- minLight = lightLevel-20;
- maxLight = lightLevel;
- /*
- for(int i=0x00; i < 77; i++) {
- noteOn(0x80, i, 0x00);
- }
- */
- }
- void loop(){
- lightLevel = analogRead(lightPin);
- pot = analogRead(potPin);
- if(minLight > lightLevel) minLight = lightLevel;
- if(maxLight < lightLevel) maxLight = lightLevel;
- lightMap = map(lightLevel, minLight, maxLight, 0, 255);
- potMap = map(pot, 0, 1023, 0, 255);
- analogWrite(lightLED, potMap);
- analogWrite(potLED, lightMap);
- if(debug) {
- Serial.print("lightMap: ");
- Serial.print(lightMap);
- Serial.print(", potMap: ");
- Serial.println(potMap);
- }
- lightMap = map(lightLevel, minLight, maxLight, 0,2);
- potMap = map(pot, 0, 1023, 0, 5000);
- // 4 octaves from bottom 2 from top
- toneMap = map(lightLevel, minLight, maxLight, 46, 104);
- if(pentatonic) {
- int nn = toneMap;
- while(nn >= 12) nn -= 12;
- //0 C 1 C# 2 D 3 Eb 4 E 5 F 6 F# 7 G 8 Ab 9 A 10 Bb 11 B
- // C - D - E - - G A
- if(nn == 1 || nn == 3 || nn == 5 || nn == 8 || nn == 10) toneMap--;
- else if(nn == 6 || nn == 11) toneMap++;
- }
- float toneL = potMap*2;
- if(debug) {
- Serial.print("Tone mapped: ");
- Serial.println(toneMap);
- Serial.print("Note delay: ");
- Serial.println(toneL);
- }
- //tone(8, potMap, lightMap);
- noteOn(0x90, toneMap, 0x45);
- delay(toneL);
- //noTone(8);
- noteOn(0x80, toneMap, 0x00);
- }
- //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
- void noteOn(int cmd, int note, int velocity) {
- if(debug) {
- Serial.print("Note: ");
- Serial.print(note);
- Serial.print(", Velocity: ");
- Serial.println(velocity);
- }
- else{
- Serial.write(cmd);
- Serial.write(note);
- Serial.write(velocity);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement