Johanneszockt1

code_combined2

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