Johanneszockt1

code_combined

Apr 29th, 2021
22
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //v4 adding state change detection and midi
  2. //v5 changed tuning of ldrs
  3. //v6 added poti reading in different interval from LDR reading
  4. #define numLDRs 12
  5. #define analogSpacing 25 //this defines the distance between the treshold values for writing the LDR Value flag low or high
  6. int lowVal;
  7. int highVal;
  8. int buttonPin = 12;
  9. int pinArray[numLDRs] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 10, 11};
  10. bool stateArray[numLDRs];
  11. bool previousStateArray[numLDRs];
  12. int valArray[numLDRs];
  13. int offsetArray[numLDRs];
  14. int potPin = 12;
  15. int middleValue;
  16. #define ShowHighBaseVal 1150
  17. #define ShowLowBaseVal 1050
  18. unsigned long previousMillis1, previousMillis2, previousMillis3;
  19.  
  20. #define LDRRefreshRate 100
  21. #define ButtonRefreshRate 1
  22. #define PotiRefreshRate 5
  23. #define interval1 1000/LDRRefreshRate
  24. #define interval2 1000/ButtonRefreshRate
  25. #define interval3 1000/PotiRefreshRate
  26.  
  27.  
  28. const int channel = 1;
  29.  
  30. #include <Adafruit_NeoPixel.h>
  31.  
  32. #define PIN 6 // On Trinket or Gemma, suggest changing this to 1
  33. #define NUMPIXELS 12 // Popular NeoPixel ring size
  34. #define BRIGHTNESS 255 //brighness on a scale of 0-255
  35. Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  36.  
  37.  
  38. #define listeningChannel 1
  39. int lowestNote = 10000;
  40. int highestNote = 0;
  41.  
  42. void setup() {
  43. // put your setup code here, to run once:
  44. Serial.begin(115200);
  45. pinMode(buttonPin, INPUT_PULLUP);
  46. pinMode(13, OUTPUT);
  47. pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  48. pixels.setBrightness(BRIGHTNESS);
  49. calibrateSensors();
  50. }
  51.  
  52. void loop() {
  53. // put your main code here, to run repeatedly:
  54. unsigned long currentMillis = millis();
  55.  
  56. if (currentMillis - previousMillis1 >= interval1) {
  57. // save the last time you blinked the LED
  58. previousMillis1 = currentMillis;
  59. handleLDR();
  60. }
  61.  
  62. if (currentMillis - previousMillis2 >= interval2) {
  63. // save the last time you blinked the LED
  64. previousMillis2 = currentMillis;
  65. readButton();
  66. }
  67.  
  68. if (currentMillis - previousMillis3 >= interval3) {
  69. // save the last time you blinked the LED
  70. previousMillis3 = currentMillis;
  71. readPoti();
  72. }
  73. while (usbMIDI.read()) {
  74. processMIDI();
  75. }
  76. }
  77.  
  78. void calibrateSensors() {
  79. digitalWrite(13, HIGH);
  80. delay(1000);
  81. digitalWrite(13, LOW);
  82. int highestSensorVal;
  83. int lowestSensorVal;
  84. for (int i = 0; i < numLDRs; i++) {
  85. int val = analogRead(pinArray[i]);
  86. offsetArray[i] = val;
  87. if (val > highestSensorVal) highestSensorVal = val;
  88. if (val < lowestSensorVal) lowestSensorVal = val;
  89. }
  90. middleValue = lowestSensorVal + (highestSensorVal - lowestSensorVal) / 2;
  91. for (int i = 0; i < numLDRs; i++) {
  92. offsetArray[i] = middleValue - offsetArray[i];
  93. //Serial.print(String(offsetArray[i]) + ",");
  94. }
  95. Serial.println();
  96. delay(1000);
  97. }
  98.  
  99. void handleLDR() {
  100. //Serial.print("1024,0 " + String(potVal) + "," + String(lowVal) + "," + String(highVal));
  101. Serial.print("1024,0 " + String(lowVal) + "," + String(highVal));
  102.  
  103. for (int i = 0; i < numLDRs; i++) {
  104. valArray[i] = analogRead(pinArray[i]) + offsetArray[i];
  105. Serial.print(" " + String(valArray[i]) + ",");
  106. if (valArray[i] > highVal) stateArray[i] = HIGH;
  107. else if (valArray[i] < lowVal) stateArray[i] = LOW;
  108. if (stateArray[i])Serial.print(i * (1200 / 13) + 75 + 1200);
  109. else Serial.print(i * (1200 / 13) + 25 + 1200);
  110. if (previousStateArray[i] != stateArray[i]) {
  111. previousStateArray[i] = stateArray[i];
  112. if (stateArray[i]) usbMIDI.sendNoteOn(60 + i, 99, channel); // 60 = C4
  113. else usbMIDI.sendNoteOff(60 + i, 99, channel); // 60 = C4
  114. }
  115. }
  116. //if (stateArray[0]) Serial.print(",500");
  117. //else Serial.print(",200");
  118. Serial.println();
  119. }
  120. void readButton() {
  121. if (!digitalRead(buttonPin)) {
  122. delay(1);
  123. if (!digitalRead(buttonPin)) calibrateSensors();
  124. }
  125. }
  126.  
  127. void readPoti() {
  128. int potVal = analogRead(potPin);
  129. potVal = middleValue + map(potVal, 0, 1024, -1024, 1024);
  130. //potVal = map(potVal, 0, 1024, 1024, 0);
  131. lowVal = potVal - analogSpacing / 2;
  132. if (lowVal < 0) lowVal = 0;
  133. highVal = potVal + analogSpacing / 2;
  134. if (highVal > 1024) highVal = 1024;
  135. }
  136.  
  137. void processMIDI(void) {
  138. byte type, channel, data1, data2, cable;
  139.  
  140. // fetch the MIDI message, defined by these 5 numbers (except SysEX)
  141. //
  142. type = usbMIDI.getType(); // which MIDI message, 128-255
  143. channel = usbMIDI.getChannel(); // which MIDI channel, 1-16
  144. data1 = usbMIDI.getData1(); // first data byte of message, 0-127
  145. data2 = usbMIDI.getData2(); // second data byte of message, 0-127
  146. cable = usbMIDI.getCable(); // which virtual cable with MIDIx8, 0-7
  147.  
  148. // uncomment if using multiple virtual cables
  149. //Serial.print("cable ");
  150. //Serial.print(cable, DEC);
  151. //Serial.print(": ");
  152.  
  153. // print info about the message
  154. //
  155.  
  156. //data1 = data1 - 60;
  157.  
  158. switch (type) {
  159. case usbMIDI.NoteOff: // 0x80
  160. if (channel == listeningChannel) {
  161. setPixelOff(data1);
  162. Serial.println(String(data1) + " 0");
  163. if (data1 < lowestNote) {
  164. lowestNote = data1;
  165. Serial.println("Lowest note is: " + String(lowestNote));
  166. }
  167. else if (data1 > highestNote) {
  168. highestNote = data1;
  169. Serial.println("Highest note is: " + String(highestNote));
  170. }
  171. }
  172. break;
  173.  
  174. case usbMIDI.NoteOn: // 0x90
  175. if (channel == listeningChannel) {
  176. setPixelOn(data1);
  177. Serial.println(String(data1) + " 1");
  178. if (data1 < lowestNote) {
  179. lowestNote = data1;
  180. Serial.println("Lowest note is: " + String(lowestNote));
  181. }
  182. else if (data1 > highestNote) {
  183. highestNote = data1;
  184. Serial.println("Highest note is: " + String(highestNote));
  185. }
  186. }
  187. break;
  188. case usbMIDI.Start: // 0xFA
  189. Serial.println("Start");
  190. break;
  191. case usbMIDI.Continue: // 0xFB
  192. Serial.println("Continue");
  193. break;
  194. case usbMIDI.Stop: // 0xFC
  195. Serial.println("Stop");
  196. break;
  197. }
  198. }
  199.  
  200. void lightPixel(int i, unsigned int t) {
  201. setPixelOn(i);
  202. delay(t);
  203. setPixelOff(i);
  204. }
  205.  
  206. void clearStrip() {
  207. pixels.clear();
  208. }
  209.  
  210. void setPixelOn(int i) {
  211. pixels.setPixelColor(i, pixels.Color(255, 255, 255));
  212. pixels.show();
  213.  
  214. }
  215. void setPixelOff(int i) {
  216. pixels.setPixelColor(i, pixels.Color(0, 0, 0));
  217. pixels.show();
  218. }
RAW Paste Data