Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //v4 adding state change detection and midi
- //v5 changed tuning of ldrs
- //v6 added poti reading in different interval from LDR reading
- #define numLDRs 12
- #define analogSpacing 25 //this defines the distance between the treshold values for writing the LDR Value flag low or high
- int lowVal;
- int highVal;
- int buttonPin = 12;
- int pinArray[numLDRs] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 15, 16};
- bool stateArray[numLDRs];
- bool previousStateArray[numLDRs];
- int valArray[numLDRs];
- int offsetArray[numLDRs];
- int potPin = 12;
- int middleValue;
- #define ShowHighBaseVal 1150
- #define ShowLowBaseVal 1050
- unsigned long previousMillis1, previousMillis2, previousMillis3;
- #define LDRRefreshRate 100
- #define ButtonRefreshRate 1
- #define PotiRefreshRate 5
- #define interval1 1000/LDRRefreshRate
- #define interval2 1000/ButtonRefreshRate
- #define interval3 1000/PotiRefreshRate
- const int channel = 1;
- //start
- #include <Adafruit_NeoPixel.h>
- #define PIN 6 // On Trinket or Gemma, suggest changing this to 1
- #define NUMPIXELS 12 // Popular NeoPixel ring size
- #define BRIGHTNESS 255 //brighness on a scale of 0-255
- Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
- #define listeningChannel 1
- int lowestNote = 10000;
- int highestNote = 0;
- //end
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(115200);
- pinMode(buttonPin, INPUT_PULLUP);
- pinMode(13, OUTPUT);
- pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
- pixels.setBrightness(BRIGHTNESS);
- calibrateSensors();
- }
- void loop() {
- // put your main code here, to run repeatedly:
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis1 >= interval1) {
- // save the last time you blinked the LED
- previousMillis1 = currentMillis;
- handleLDR();
- }
- if (currentMillis - previousMillis2 >= interval2) {
- // save the last time you blinked the LED
- previousMillis2 = currentMillis;
- readButton();
- }
- if (currentMillis - previousMillis3 >= interval3) {
- // save the last time you blinked the LED
- previousMillis3 = currentMillis;
- readPoti();
- }
- while (usbMIDI.read()) {
- processMIDI();
- }
- }
- void calibrateSensors() {
- digitalWrite(13, HIGH);
- delay(1000);
- digitalWrite(13, LOW);
- int highestSensorVal;
- int lowestSensorVal;
- for (int i = 0; i < numLDRs; i++) {
- int val = analogRead(pinArray[i]);
- offsetArray[i] = val;
- if (val > highestSensorVal) highestSensorVal = val;
- if (val < lowestSensorVal) lowestSensorVal = val;
- }
- middleValue = lowestSensorVal + (highestSensorVal - lowestSensorVal) / 2;
- for (int i = 0; i < numLDRs; i++) {
- offsetArray[i] = middleValue - offsetArray[i];
- //Serial.print(String(offsetArray[i]) + ",");
- }
- Serial.println();
- delay(1000);
- }
- void handleLDR() {
- //Serial.print("1024,0 " + String(potVal) + "," + String(lowVal) + "," + String(highVal));
- Serial.print("1024,0 " + String(lowVal) + "," + String(highVal));
- for (int i = 0; i < numLDRs; i++) {
- valArray[i] = analogRead(pinArray[i]) + offsetArray[i];
- Serial.print(" " + String(valArray[i]) + ",");
- if (valArray[i] > highVal) stateArray[i] = HIGH;
- else if (valArray[i] < lowVal) stateArray[i] = LOW;
- if (stateArray[i])Serial.print(i * (1200 / 13) + 75 + 1200);
- else Serial.print(i * (1200 / 13) + 25 + 1200);
- if (previousStateArray[i] != stateArray[i]) {
- previousStateArray[i] = stateArray[i];
- if (stateArray[i]) usbMIDI.sendNoteOn(60 + i, 99, channel);
- // 60 = C4
- else usbMIDI.sendNoteOff(60 + i, 99, channel); // 60 = C4
- }
- }
- //if (stateArray[0]) Serial.print(",500");
- //else Serial.print(",200");
- Serial.println();
- }
- void readButton() {
- if (!digitalRead(buttonPin)) {
- delay(1);
- if (!digitalRead(buttonPin)) calibrateSensors();
- }
- }
- void readPoti() {
- int potVal = analogRead(potPin);
- potVal = middleValue + map(potVal, 0, 1024, -1024, 1024);
- //potVal = map(potVal, 0, 1024, 1024, 0);
- lowVal = potVal - analogSpacing / 2;
- if (lowVal < 0) lowVal = 0;
- highVal = potVal + analogSpacing / 2;
- if (highVal > 1024) highVal = 1024;
- }
- void processMIDI(void) {
- byte type, channel, data1, data2, cable;
- // fetch the MIDI message, defined by these 5 numbers (except SysEX)
- //
- type = usbMIDI.getType(); // which MIDI message, 128-255
- channel = usbMIDI.getChannel(); // which MIDI channel, 1-16
- data1 = usbMIDI.getData1(); // first data byte of message, 0-127
- data2 = usbMIDI.getData2(); // second data byte of message, 0-127
- cable = usbMIDI.getCable(); // which virtual cable with MIDIx8, 0-7
- // uncomment if using multiple virtual cables
- //Serial.print("cable ");
- //Serial.print(cable, DEC);
- //Serial.print(": ");
- // print info about the message
- //
- data1 = data1 - 60;
- switch (type) {
- case usbMIDI.NoteOff: // 0x80
- if (channel == listeningChannel) {
- setPixelOff(data1);
- Serial.println(String(data1) + " 0");
- if (data1 < lowestNote) {
- lowestNote = data1;
- Serial.println("Lowest note is: " + String(lowestNote));
- }
- else if (data1 > highestNote) {
- highestNote = data1;
- Serial.println("Highest note is: " + String(highestNote));
- }
- }
- break;
- case usbMIDI.NoteOn: // 0x90
- if (channel == listeningChannel) {
- setPixelOn(data1);
- Serial.println(String(data1) + " 1");
- if (data1 < lowestNote) {
- lowestNote = data1;
- Serial.println("Lowest note is: " + String(lowestNote));
- }
- else if (data1 > highestNote) {
- highestNote = data1;
- Serial.println("Highest note is: " + String(highestNote));
- }
- }
- break;
- case usbMIDI.Start: // 0xFA
- Serial.println("Start");
- break;
- case usbMIDI.Continue: // 0xFB
- Serial.println("Continue");
- break;
- case usbMIDI.Stop: // 0xFC
- Serial.println("Stop");
- break;
- }
- }
- void lightPixel(int i, unsigned int t) {
- setPixelOn(i);
- delay(t);
- setPixelOff(i);
- }
- void clearStrip() {
- pixels.clear();
- }
- void setPixelOn(int i) {
- pixels.setPixelColor(i, pixels.Color(255, 0, 0));
- pixels.show();
- }
- void setPixelOff(int i) {
- pixels.setPixelColor(i, pixels.Color(0, 0, 0));
- pixels.show();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement