I have rather simple program where photo cells are used to trigger MIDI notes when light drops under the threshold - person blocking the light.
In order to compensate differences of ambient light I added pots to set the threshold at specific location(different rooms). However I have a dedicated strong light source for the setup.
Something is not quit right - the program stops reacting normally after some time(ambient light change?). Like it works perfectly - then after some time or change of ambient light it either triggers notes all the time without human interaction or does not do a thing - regardless of tweaking the threshold pots..
Could someone check this out?
#include <MIDI.h>
byte note = 0;
byte b = 3; //-b that slight change will not affect
byte c = 1;
int val1Pin = 0; //analog pin 0-3 photocell
int val2Pin = 1;
int val3Pin = 2;
int val4Pin = 3;
int val1; //photocell reads
int val2;
int val3;
int val4;
int val5Pin = 8; //analog pin 8-11 pot for treshold
int val6Pin = 9;
int val7Pin = 10;
int val8Pin = 11;
int a1 = 0; // a1-a5 treshold adjustable with pot
int a2 = 0;
int a3 = 0;
int a4 = 0;
void setup() {
Serial.begin(31250); // set MIDI baud rate
MIDI.sendProgramChange (121,c); // set MIDI instrument
}
void loop() {
val1 = analogRead(val1Pin);
a1 = analogRead(val5Pin);
if (val1 <= a1-b) { //if val is under the threshold play notes
MIDI.sendNoteOn(54,127,c);
delay(300);
MIDI.sendNoteOn(58,127,c);
delay(300);
MIDI.sendNoteOn(66,127,c);
delay(100);
MIDI.sendNoteOff(54,0,c);
MIDI.sendNoteOff(58,0,c);
MIDI.sendNoteOff(66,0,c);
}
val2 = analogRead(val2Pin);
a2 = analogRead(val6Pin);
if (val2 <= a2-b) {
MIDI.sendNoteOn(68,127,c);
delay(100);
MIDI.sendNoteOn(70,127,c);
delay(100);
MIDI.sendNoteOn(73,127,c);
delay(100);
MIDI.sendNoteOn(75,127,c);
delay(100);
MIDI.sendNoteOff(68,0,c);
MIDI.sendNoteOff(70,0,c);
MIDI.sendNoteOff(73,0,c);
MIDI.sendNoteOff(75,0,c);
}
val3 = analogRead(val3Pin);
a3 = analogRead(val7Pin);
if (val3 <= a3-b) {
MIDI.sendNoteOn(60,127,c);
delay(200);
MIDI.sendNoteOn(70,127,c);
delay(100);
MIDI.sendNoteOn(68,127,c);
delay(100);
MIDI.sendNoteOn(51,127,c);
delay(300);
MIDI.sendNoteOn(60,127,c);
delay(100);
MIDI.sendNoteOff(60,0,c);
MIDI.sendNoteOff(70,0,c);
MIDI.sendNoteOff(68,0,c);
MIDI.sendNoteOff(51,0,c);
MIDI.sendNoteOff(60,0,c);
}
val4 = analogRead(val4Pin);
a4 = analogRead(val8Pin);
if (val4 <= a4-b) {
MIDI.sendNoteOn(44,127,c);
delay(300);
MIDI.sendNoteOn(51,127,c);
delay(300);
MIDI.sendNoteOn(49,127,c);
delay(100);
MIDI.sendNoteOff(44,0,c);
MIDI.sendNoteOff(51,0,c);
MIDI.sendNoteOff(49,0,c);
}
}