Guest User

Laser harp code 1

a guest
May 12th, 2015
118
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Stepper.h>
  2. #include <MIDI.h>
  3.  
  4. #define stepsPerRevolution 200
  5.  
  6. Stepper beamsplitter(stepsPerRevolution, 8, 9);
  7.  
  8. MIDI_CREATE_DEFAULT_INSTANCE();
  9.  
  10. int LaserState = LOW; // The variable that stores the state of the laser beam.
  11. int sensor = 8 ; // Change this value to calibrate your harp's sensor
  12. int delaylaser = 150; // If you increase this, the laser will be brighter, but the harp will be less fluid
  13. int delaymotor = 3;
  14. int motorspeed = 60; // This variable affects the speed, and fluidity of the harp.
  15. int stepsize = 3;
  16.  
  17.  
  18. int LaserPin = 7; // Tell the arduino that the laser is on pin 7
  19.  
  20. int a, b, c, d, e, f, g, h, i = 0; // Iniating the note status markers.
  21.  
  22. void setup()
  23. {
  24.  
  25. pinMode(LaserPin, OUTPUT); // Setup for laser.
  26. pinMode(13, OUTPUT); // Setup for status led.
  27.  
  28. beamsplitter.setSpeed(motorspeed);
  29. MIDI.begin();
  30. Serial.begin(115200); // Start a serial communication channel for MIDI
  31. delay(1500);
  32. }
  33.  
  34. void loop()
  35. {
  36.  
  37. //First beam
  38.  
  39. digitalWrite(LaserPin, HIGH); // Turn on the laser for the 1st beam.
  40. delay(delaylaser);
  41.  
  42. if ( (analogRead(0) > sensor ) && (a == 0) ) // If the sensor gets a signal, and the not is not playing:
  43. {
  44. digitalWrite(13, HIGH); // Switch on status led.
  45. MIDI.sendNoteOn(47, 127, 1); // Play note 1
  46. a++; // Change the status variable to one.
  47. }
  48.  
  49. else if(analogRead(0) < sensor ) // If the sensor does not get a signal:
  50. {
  51. digitalWrite(13, LOW); // Switch off the status led.
  52. MIDI.sendNoteOff(47, 0, 1); // Stop playing note 1.
  53. a = 0; // Change the status variable to zero.
  54. }
  55.  
  56. digitalWrite(LaserPin, LOW); // Turn off the Laser.
  57. beamsplitter.step(stepsize);
  58. delay(delaymotor);
  59.  
  60. //Second beam
  61.  
  62. digitalWrite(LaserPin, HIGH); // Turn on the laser for the 2nd beam.
  63. delay(delaylaser);
  64.  
  65. if( (analogRead(0) > sensor ) && (b == 0) ) // If the sensor gets a signal, and the not is not playing:
  66. {
  67. digitalWrite(13, HIGH); // Switch on status led.
  68. MIDI.sendNoteOn(40, 40, 1); // Play note 2
  69. b++; // Change the status variable to one.
  70. }
  71.  
  72. else if(analogRead(0) < sensor ) // If the sensor does not get a signal:
  73. {
  74. digitalWrite(13, LOW); // Switch off the status led.
  75. MIDI.sendNoteOff(40, 40, 1); // Stop playing note 2.
  76. b = 0; // Change the status variable to zero.
  77. }
  78.  
  79. digitalWrite(LaserPin, LOW); // Turn off the Laser.
  80. beamsplitter.step(stepsize);
  81. delay(delaymotor);
  82.  
  83. //Third beam
  84.  
  85. digitalWrite(LaserPin, HIGH); // Turn on the laser for the 3rd beam.
  86. delay(delaylaser);
  87.  
  88. if( (analogRead(0) > sensor ) && (c == 0) ) // If the sensor gets a signal, and the not is not playing:
  89. {
  90. digitalWrite(13, HIGH); // Switch on status led.
  91. MIDI.sendNoteOn(71, 127, 1); // Play note 3
  92. c++; // Change the status variable to one.
  93. }
  94.  
  95. else if(analogRead(0) < sensor ) // If the sensor does not get a signal:
  96. {
  97. digitalWrite(13, LOW); // Switch off the status led.
  98. MIDI.sendNoteOff(71, 0, 1); // Stop playing note 2.
  99. c = 0; // Change the status variable to zero.
  100. }
  101.  
  102. digitalWrite(LaserPin, LOW); // Turn off the Laser.
  103. beamsplitter.step(stepsize);
  104. delay(delaymotor);
  105.  
  106. //Fourth beam
  107.  
  108. digitalWrite(LaserPin, HIGH); //This is beam 4
  109. delay(delaylaser);
  110.  
  111. if( (analogRead(0) > sensor ) && (d == 0) )
  112. {
  113. digitalWrite(13, HIGH);
  114. MIDI.sendNoteOn(70, 127, 1);
  115. d++;
  116. }
  117.  
  118. else if(analogRead(0) < sensor )
  119. {
  120. digitalWrite(13, LOW);
  121. MIDI.sendNoteOff(70, 0, 1);
  122. d = 0;
  123. }
  124.  
  125. digitalWrite(LaserPin, LOW);
  126. beamsplitter.step(stepsize);
  127. delay(delaymotor);
  128.  
  129. //Fifth beam
  130.  
  131. digitalWrite(LaserPin, HIGH); //This is beam 5
  132. delay(delaylaser);
  133.  
  134. if( (analogRead(0) > sensor ) && (e == 0) )
  135. {
  136. digitalWrite(13, HIGH);
  137. MIDI.sendNoteOn(68, 127, 1);
  138. e++;
  139. }
  140.  
  141. else if(analogRead(0) < sensor )
  142. {
  143. digitalWrite(13, LOW);
  144. MIDI.sendNoteOff(68, 0, 1);
  145. e = 0;
  146. }
  147.  
  148. digitalWrite(LaserPin, LOW);
  149. beamsplitter.step(stepsize);
  150. delay(delaymotor);
  151.  
  152. //Sixth beam
  153.  
  154. digitalWrite(LaserPin, HIGH); //This is beam 6
  155. delay(delaylaser);
  156.  
  157. if( (analogRead(0) > sensor ) && (f == 0) )
  158. {
  159. digitalWrite(13, HIGH);
  160. MIDI.sendNoteOn(66, 127, 1);
  161. f++;
  162. }
  163.  
  164. else if(analogRead(0) < sensor )
  165. {
  166. digitalWrite(13, LOW);
  167. MIDI.sendNoteOff(66, 0, 1);
  168. f = 0;
  169. }
  170.  
  171. digitalWrite(LaserPin, LOW);
  172. beamsplitter.step(stepsize);
  173. delay(delaymotor);
  174.  
  175. //Seventh beam
  176.  
  177. digitalWrite(LaserPin, HIGH); //This is beam 7
  178. delay(delaylaser);
  179.  
  180. if( (analogRead(0) > sensor ) && (g == 0) )
  181. {
  182. digitalWrite(13, HIGH);
  183. MIDI.sendNoteOn(64, 127, 1);
  184. g++;
  185. }
  186.  
  187. else if(analogRead(0) < sensor )
  188. {
  189. digitalWrite(13, LOW);
  190. MIDI.sendNoteOff(64, 0, 1);
  191. g = 0;
  192. }
  193.  
  194. digitalWrite(LaserPin, LOW);
  195. beamsplitter.step(stepsize);
  196. delay(delaymotor);
  197.  
  198. //Eighth beam
  199.  
  200. digitalWrite(LaserPin, HIGH); //This is beam 8
  201. delay(delaylaser);
  202.  
  203. if( (analogRead(0) > sensor ) && (h == 0) )
  204. {
  205. digitalWrite(13, HIGH);
  206. MIDI.sendNoteOn(63, 127, 1);
  207. h++;
  208. }
  209.  
  210. else if(analogRead(0) < sensor )
  211. {
  212. digitalWrite(13, LOW);
  213. MIDI.sendNoteOff(63, 0, 1);
  214. h = 0;
  215. }
  216.  
  217. digitalWrite(LaserPin, LOW);
  218. beamsplitter.step(stepsize);
  219. delay(delaymotor);
  220.  
  221. //Ninth beam
  222.  
  223. digitalWrite(LaserPin, HIGH); //This is beam 9
  224. delay(delaylaser);
  225.  
  226. if( (analogRead(0) > sensor ) && (i == 0) )
  227. {
  228. digitalWrite(13, HIGH);
  229. MIDI.sendNoteOn(61, 127, 1);
  230. i++;
  231. }
  232.  
  233. else if(analogRead(0) < sensor )
  234. {
  235. digitalWrite(13, LOW);
  236. MIDI.sendNoteOff(61, 0, 1);
  237. i = 0;
  238. }
  239.  
  240. digitalWrite(LaserPin, LOW);
  241. beamsplitter.step(-stepsize);
  242. delay(delaymotor);
  243.  
  244. //Eighth beam
  245.  
  246. digitalWrite(LaserPin, HIGH); //This is beam 8
  247. delay(delaylaser);
  248.  
  249. if( (analogRead(0) > sensor ) && (h == 0) )
  250. {
  251. digitalWrite(13, HIGH);
  252. MIDI.sendNoteOn(63, 127, 1);
  253. h++;
  254. }
  255.  
  256. else if(analogRead(0) < sensor )
  257. {
  258. digitalWrite(13, LOW);
  259. MIDI.sendNoteOff(63, 0, 1);
  260. h = 0;
  261. }
  262.  
  263. digitalWrite(LaserPin, LOW);
  264. beamsplitter.step(-stepsize);
  265. delay(delaymotor);
  266.  
  267. //Seventh beam
  268.  
  269. digitalWrite(LaserPin, HIGH); //This is beam 7
  270. delay(delaylaser);
  271.  
  272. if( (analogRead(0) > sensor ) && (g == 0) )
  273. {
  274. digitalWrite(13, HIGH);
  275. MIDI.sendNoteOn(64, 127, 1);
  276. g++;
  277. }
  278.  
  279. else if(analogRead(0) < sensor )
  280. {
  281. digitalWrite(13, LOW);
  282. MIDI.sendNoteOff(64, 0, 1);
  283. g = 0;
  284. }
  285.  
  286. digitalWrite(LaserPin, LOW);
  287. beamsplitter.step(-stepsize);
  288. delay(delaymotor);
  289.  
  290. //Sixth beam
  291.  
  292. digitalWrite(LaserPin, HIGH); //This is beam 6
  293. delay(delaylaser);
  294.  
  295. if( (analogRead(0) > sensor ) && (f == 0) )
  296. {
  297. digitalWrite(13, HIGH);
  298. MIDI.sendNoteOn(66, 127, 1);
  299. f++;
  300. }
  301.  
  302. else if(analogRead(0) < sensor )
  303. {
  304. digitalWrite(13, LOW);
  305. MIDI.sendNoteOff(66, 0, 1);
  306. f = 0;
  307. }
  308.  
  309. digitalWrite(LaserPin, LOW);
  310. beamsplitter.step(-stepsize);
  311. delay(delaymotor);
  312.  
  313. //Fifth beam
  314.  
  315. digitalWrite(LaserPin, HIGH); //This is beam 5
  316. delay(delaylaser);
  317.  
  318. if( (analogRead(0) > sensor ) && (e == 0) )
  319. {
  320. digitalWrite(13, HIGH);
  321. MIDI.sendNoteOn(68, 127, 1);
  322. e++;
  323. }
  324.  
  325. else if(analogRead(0) < sensor )
  326. {
  327. digitalWrite(13, LOW);
  328. MIDI.sendNoteOff(68, 0, 1);
  329. e = 0;
  330. }
  331.  
  332. digitalWrite(LaserPin, LOW);
  333. beamsplitter.step(-stepsize);
  334. delay(delaymotor);
  335.  
  336. //Fourth beam
  337.  
  338. digitalWrite(LaserPin, HIGH); //This is beam 4
  339. delay(delaylaser);
  340.  
  341. if( (analogRead(0) > sensor ) && (d == 0) )
  342. {
  343. digitalWrite(13, HIGH);
  344. MIDI.sendNoteOn(70, 127, 1);
  345. d++;
  346. }
  347.  
  348. else if(analogRead(0) < sensor )
  349. {
  350. digitalWrite(13, LOW);
  351. MIDI.sendNoteOff(70, 0, 1);
  352. d = 0;
  353. }
  354.  
  355. digitalWrite(LaserPin, LOW);
  356. beamsplitter.step(-stepsize);
  357. delay(delaymotor);
  358.  
  359. //Third beam
  360.  
  361. digitalWrite(LaserPin, HIGH); //This is beam 3
  362. delay(delaylaser);
  363.  
  364. if( (analogRead(0) > sensor ) && (c == 0) )
  365. {
  366. digitalWrite(13, HIGH);
  367. MIDI.sendNoteOn(71, 127, 1);
  368. c++;
  369. }
  370.  
  371. else if(analogRead(0) < sensor )
  372. {
  373. digitalWrite(13, LOW);
  374. MIDI.sendNoteOff(71, 0, 1);
  375. c = 0;
  376. }
  377.  
  378. digitalWrite(LaserPin, LOW);
  379. beamsplitter.step(-stepsize);
  380. delay(delaymotor);
  381.  
  382. //Second beam
  383.  
  384. digitalWrite(LaserPin, HIGH); //This is beam 2
  385. delay(delaylaser);
  386.  
  387. if( (analogRead(0) > sensor ) && (b == 0) )
  388. {
  389. digitalWrite(13, HIGH);
  390. MIDI.sendNoteOn(40, 127, 1);
  391. b++;
  392. }
  393.  
  394. else if(analogRead(0) < sensor )
  395. {
  396. digitalWrite(13, LOW);
  397. MIDI.sendNoteOff(40, 0, 1);
  398. b = 0;
  399. }
  400.  
  401. digitalWrite(LaserPin, LOW);
  402. beamsplitter.step(-stepsize);
  403. delay(delaymotor); // Wait microseconds
  404.  
  405. // Back to top.
  406.  
  407. }
RAW Paste Data