Advertisement
franher007

Untitled

Apr 29th, 2016
1,670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.56 KB | None | 0 0
  1. #define GREEN 3
  2. #define BLUE 5
  3. #define RED 6
  4. String readString;
  5. String program = "";
  6. bool shouldBreak = false;
  7.  
  8. float B, HB, QB, TQB;
  9.  
  10. void setup() {
  11.  
  12. pinMode(2, INPUT);
  13. digitalWrite(2, LOW);
  14. pinMode(GREEN, OUTPUT);
  15. pinMode(BLUE, OUTPUT);
  16. pinMode(RED, OUTPUT);
  17. digitalWrite(GREEN, HIGH);
  18. digitalWrite(BLUE, HIGH);
  19. digitalWrite(RED, HIGH);
  20. Serial.begin(9600);
  21. attachInterrupt(0, serialInterrupt, CHANGE);
  22. }
  23.  
  24. void(* resetFunc) (void) = 0; //declare reset function @ address 0
  25.  
  26. void setBPM(float BPM){
  27. B = float(60000) / float(BPM);
  28. HB = B/2;
  29. QB = B/4;
  30. TQB = HB+QB;
  31. shouldBreak = false;
  32. }
  33.  
  34. bool fdelay(float ms){
  35. for(int i = 0; i < ms/100; i +=1){
  36. delay(100);
  37. if(shouldBreak){
  38. Serial.println(" breaking");
  39. shouldBreak = false;
  40. analogWrite(RED, 255);
  41. analogWrite(GREEN, 255);
  42. analogWrite(BLUE, 255);
  43. return true;
  44. }
  45. }
  46. delay( (ms/100.0) - (ms/100) );
  47. return false;
  48. }
  49.  
  50.  
  51. bool ll(int R, int G, int B, int D){
  52. analogWrite(RED, 255-R);
  53. analogWrite(GREEN, 255-G);
  54. analogWrite(BLUE, 255-B);
  55. delay(D);
  56. }
  57.  
  58. bool fade(String color, float D, int MIN, int MAX, int RPT, bool ASC){
  59. int reps = MAX-MIN;
  60. float dd = D/reps;
  61. if(color == "white"){
  62. for( int k = 0; k < RPT; k +=1){
  63. if(ASC){
  64. for( int i = MIN ; i < MAX ; i += 1 ){
  65. analogWrite( RED, 255 - i );
  66. analogWrite( GREEN, 255 - i );
  67. analogWrite( BLUE, 255 - i );
  68. delay(dd);
  69. }
  70. } else {
  71. for( int i = MIN ; i < MAX ; i += 1 ){
  72. analogWrite( RED, i );
  73. analogWrite( GREEN, i );
  74. analogWrite( BLUE, i );
  75. delay(dd);
  76. }
  77. }
  78. }
  79. }
  80.  
  81. if(color == "red"){
  82. for( int k = 0; k < RPT; k +=1){
  83. if(ASC){
  84. for( int i = MIN ; i < MAX ; i += 1 ){
  85. analogWrite( RED, 255 - i );
  86. analogWrite( GREEN, 255 - 0 );
  87. analogWrite( BLUE, 255 - 0 );
  88. delay(dd);
  89. }
  90. } else {
  91. for( int i = MIN ; i < MAX ; i += 1 ){
  92. analogWrite( RED, i );
  93. analogWrite( GREEN, 255-0 );
  94. analogWrite( BLUE, 255-0 );
  95. delay(dd);
  96. }
  97. }
  98. }
  99. }
  100.  
  101. if(color == "green"){
  102. for( int k = 0; k < RPT; k +=1){
  103. if(ASC){
  104. for( int i = MIN ; i < MAX ; i += 1 ){
  105. analogWrite( RED, 255 - 0 );
  106. analogWrite( GREEN, 255 - i );
  107. analogWrite( BLUE, 255 - 0 );
  108. delay(dd);
  109. }
  110. } else {
  111. for( int i = MIN ; i < MAX ; i += 1 ){
  112. analogWrite( RED, 255 );
  113. analogWrite( GREEN, i );
  114. analogWrite( BLUE, 255 );
  115. delay(dd);
  116. }
  117. }
  118. }
  119. }
  120.  
  121. if(color == "blue"){
  122. for( int k = 0; k < RPT; k +=1){
  123. if(ASC){
  124. for( int i = MIN ; i < MAX ; i += 1 ){
  125. analogWrite( RED, 255 - 0 );
  126. analogWrite( GREEN, 255 - 0 );
  127. analogWrite( BLUE, 255 - i );
  128. delay(dd);
  129. }
  130. } else {
  131. for( int i = MIN ; i < MAX ; i += 1 ){
  132. analogWrite( RED, 255 );
  133. analogWrite( GREEN, 255 );
  134. analogWrite( BLUE, i );
  135. delay(dd);
  136. }
  137. }
  138. }
  139. }
  140.  
  141. }
  142.  
  143. void off(float D){
  144. lloff(0,0,0,D,100,1);
  145. }
  146.  
  147. bool lloff(int R, int G, int B, float D, float P, int RPT){
  148. for( int i = 0; i < RPT; i +=1){
  149. analogWrite(RED, 255-R);
  150. analogWrite(GREEN, 255-G);
  151. analogWrite(BLUE, 255-B);
  152. //float dpp = D*P;
  153. //float dppc = D*(100-P);
  154. //if (fdelay( (D*P) /100.0 )) { return true; }
  155. delay( (D*P) /100.0 );
  156. if(P < 100){
  157. analogWrite(RED, 255);
  158. analogWrite(GREEN, 255);
  159. analogWrite(BLUE, 255);
  160. delay( (D*(100-P)) /100.0 );
  161. }
  162.  
  163. }
  164. return false;
  165. }
  166.  
  167. bool lloffr(float D, float P, int RPT){
  168. for( int i = 0; i < RPT; i +=1){
  169. int randr = map(random(2),0,1,0,255);
  170. int randg = map(random(2),0,1,0,255);
  171. int randb = map(random(2),0,1,0,255);
  172. if( randr+randb+randg == 765){randb = 0;}
  173. analogWrite(RED, randr);
  174. analogWrite(GREEN, randg);
  175. analogWrite(BLUE, randb);
  176. //float dpp = D*P;
  177. //float dppc = D*(100-P);
  178. //if (fdelay( (D*P) /100.0 )) { return true; }
  179. delay( (D*P) /100.0 );
  180. if(P < 100){
  181. analogWrite(RED, 255);
  182. analogWrite(GREEN, 255);
  183. analogWrite(BLUE, 255);
  184. delay( (D*(100-P)) /100.0 );
  185. }
  186. }
  187. return false;
  188. }
  189.  
  190. bool lloffrr(float D, float P, int RPT){
  191. for( int i = 0; i < RPT; i +=1){
  192. int randr = random(255);
  193. int randg = random(255);
  194. int randb = random(255);
  195. //if( randr+randb+randg == 765){randb = 0;}
  196. analogWrite(RED, randr);
  197. analogWrite(GREEN, randg);
  198. analogWrite(BLUE, randb);
  199. //float dpp = D*P;
  200. //float dppc = D*(100-P);
  201. //if (fdelay( (D*P) /100.0 )) { return true; }
  202. delay( (D*P) /100.0 );
  203. if(P < 100){
  204. analogWrite(RED, 255);
  205. analogWrite(GREEN, 255);
  206. analogWrite(BLUE, 255);
  207. delay( (D*(100-P)) /100.0 );
  208. }
  209. }
  210. return false;
  211. }
  212.  
  213.  
  214. bool rainbow(float RPT){
  215. int redVal = 255;
  216. int blueVal = 0;
  217. int greenVal = 0;
  218. float d = (B*RPT)/765.0;
  219. for( int i = 0 ; i < 255 ; i += 1 ){
  220. greenVal += 1;
  221. redVal -= 1;
  222. analogWrite( GREEN, 255 - greenVal );
  223. analogWrite( RED, 255 - redVal );
  224. //Serial.println((B*RPT)/765.0);
  225. //if (fdelayVar( d,d)) {return true;}
  226. delay(d);
  227. }
  228.  
  229. redVal = 0;
  230. blueVal = 0;
  231. greenVal = 255;
  232. for( int i = 0 ; i < 255 ; i += 1 ){
  233. blueVal += 1;
  234. greenVal -= 1;
  235. analogWrite( BLUE, 255 - blueVal );
  236. analogWrite( GREEN, 255 - greenVal );
  237.  
  238. delay(d);
  239. }
  240.  
  241. redVal = 0;
  242. blueVal = 255;
  243. greenVal = 0;
  244. for( int i = 0 ; i < 255 ; i += 1 ){
  245. redVal += 1;
  246. blueVal -= 1;
  247. analogWrite( RED, 255 - redVal );
  248. analogWrite( BLUE, 255 - blueVal );
  249.  
  250. delay(d);
  251. }
  252. return false;
  253. }
  254.  
  255.  
  256. void loop() {
  257. //5 START FLASHES
  258. //lloff(20,20,20,1000,50,5);
  259.  
  260. while(1 == 1){
  261.  
  262. if (program == "test"){
  263. setBPM(112.2);
  264. if (lloff(250,250,250,B,80,5)) { return; }
  265. if (fdelay(B*3)) { return; }
  266. if (lloff(250,0,0,B,80,5)) { return; }
  267. if (fdelay(B*3)) { return; }
  268. if (lloff(0,250,0,B,80,5)) { return; }
  269. if (fdelay(B*3)) { return; }
  270. if (lloff(0,0,250,B,80,5)) { return; }
  271. if (fdelay(B*3)) { return; }
  272. program = "";
  273. }
  274. if (program == "none"){
  275. analogWrite(3, 230);
  276. if (fdelay(450)) { return; }
  277. analogWrite(3, 255);
  278. if (fdelay(450)) { return; }
  279. analogWrite(5, 230);
  280. if (fdelay(450)) { return; }
  281. analogWrite(5, 255);
  282. if (fdelay(450)) { return; }
  283. analogWrite(6, 230);
  284. if (fdelay(450)) { return; }
  285. analogWrite(6, 255);
  286. if (fdelay(450)) { return; }
  287. analogWrite(3, 230);
  288. if (fdelay(450)) { return; }
  289. analogWrite(3, 255);
  290. if (fdelay(450)) { return; }
  291. program = "";
  292. }
  293. if (program == "nas"){
  294. setBPM(112.2);
  295. //if (lloff(20,0,0,B,50,94)) { return; }
  296. //if (fdelay(B*2)) { return; }
  297. if (lloff(20,0,0,B,80,1)) { return; }
  298. if (fdelay(B*3)) { return; }
  299. if (lloff(20,0,0,B,50,5)) { return; }
  300. if (fdelay(B*3)) { return; }
  301. if (lloff(20,0,0,B,50,5)) { return; }
  302. if (fdelay(B*3)) { return; }
  303. if (lloff(20,0,0,B,50,7)) { return; }
  304. if (lloff(20,0,0,HB,50,6)) { return; }
  305. if (fdelay(B*2)) { return; }
  306. if (lloff(20,0,0,QB,50,2)) { return; }
  307. if (fdelay(HB)) { return; }
  308. if (lloff(20,0,0,HB,50,1)) { return; }
  309. if (fdelay(HB)) { return; }
  310. if (lloff(20,0,0,HB,50,1)) { return; }
  311. if (fdelay(HB)) { return; }
  312. if (lloff(20,0,0,HB,50,1)) { return; } //1,10min
  313. if (fdelay(B*3)) { return; }
  314. if (lloff(20,0,0,B,50,94)) { return; }
  315. program = "";
  316. }
  317.  
  318. if (program == "aoalt"){
  319. setBPM(120);
  320. for( int i = 0; i< 50; i += 1){
  321. ll(100,0,0,QB);
  322. off(QB);
  323. ll(0,100,0,QB);
  324. off(QB);
  325. ll(0,0,100,QB);
  326. off(QB);
  327. ll(0,100,0,QB);
  328. off(QB);
  329. ll(100,0,0,QB);
  330. off(QB);
  331. ll(0,100,0,QB);
  332. off(TQB);
  333. ll(0,0,100,QB);
  334. off(QB);
  335. //ll(0,0,100,HALFBEAT);
  336. }
  337. program = "";
  338. }
  339.  
  340. if (program == "ahfod"){
  341. setBPM(123.0);
  342. //fade("red",B*6,0,255,1,true);
  343. rainbow(66);
  344. lloffr(B,100,88);
  345. lloff(0,0,0,B,100,6);
  346. off(HB);
  347. lloff(255,255,255,QB,50,6);
  348. lloffrr(B,80,65);
  349. lloff(0,0,0,B,100,31);
  350. rainbow(32);
  351. lloff(20,20,20,B,100,6);
  352. fade("white",B*2,20,255,1,true);
  353. lloff(255,255,255,B,100,5);
  354. fade("white",B*1,20,255,1,false);
  355. fade("white",B*1,20,255,1,true);
  356. fade("white",B*17,0,255,1,false);
  357. lloff(20,20,20,B,100,6);
  358. fade("white",B*2,20,255,1,true);
  359. lloff(255,255,255,B,100,5);
  360. fade("white",B*1,20,255,1,false);
  361. fade("white",B*1,20,255,1,true);
  362. fade("white",B*16,0,255,1,false);
  363. lloffr(QB,50,8);
  364. lloffr(B,50,64);
  365. lloff(255,255,255,B,100,32);
  366. fade("white",B*7,0,255,1,false);
  367. program = "";
  368. }
  369. //program = "";
  370. }
  371. }
  372.  
  373. volatile boolean inService = false;
  374.  
  375. void serialInterrupt()
  376. {
  377. // Trick: since Serial I/O in interrupt driven, we must reenable interrupts while in this Interrupt Service Routine.
  378. // But doing so will cause the routine to be called nestedly, causing problems.
  379. // So we mark we are already in service.
  380.  
  381. // Already in service? Do nothing.
  382. if (inService) return;
  383.  
  384. // You was not in service. Now you are.
  385. inService = true;
  386.  
  387. // Reenable interrupts, to allow Serial to work. We do this only if inService is false.
  388. interrupts();
  389. readString = "";
  390. while (Serial.available()) {
  391. delay(3); //delay to allow buffer to fill
  392. if (Serial.available() >0) {
  393. char c = Serial.read(); //gets one byte from serial buffer
  394.  
  395. if( c == '\n'){
  396. Serial.print("Arduino Received: ");
  397. Serial.print(readString);
  398. program = readString;
  399. shouldBreak = true;
  400. } else {
  401. readString += c; //makes the string readString
  402. }
  403. }
  404. }
  405.  
  406. // Job done.
  407. inService = false;
  408. if (readString == "reset"){resetFunc(); }
  409. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement