Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define GREEN 3
- #define BLUE 5
- #define RED 6
- String readString;
- String program = "";
- bool shouldBreak = false;
- float B, HB, QB, TQB;
- void setup() {
- pinMode(2, INPUT);
- digitalWrite(2, LOW);
- pinMode(GREEN, OUTPUT);
- pinMode(BLUE, OUTPUT);
- pinMode(RED, OUTPUT);
- digitalWrite(GREEN, HIGH);
- digitalWrite(BLUE, HIGH);
- digitalWrite(RED, HIGH);
- Serial.begin(9600);
- attachInterrupt(0, serialInterrupt, CHANGE);
- }
- void(* resetFunc) (void) = 0; //declare reset function @ address 0
- void setBPM(float BPM){
- B = float(60000) / float(BPM);
- HB = B/2;
- QB = B/4;
- TQB = HB+QB;
- shouldBreak = false;
- }
- bool fdelay(float ms){
- for(int i = 0; i < ms/100; i +=1){
- delay(100);
- if(shouldBreak){
- Serial.println(" breaking");
- shouldBreak = false;
- analogWrite(RED, 255);
- analogWrite(GREEN, 255);
- analogWrite(BLUE, 255);
- return true;
- }
- }
- delay( (ms/100.0) - (ms/100) );
- return false;
- }
- bool ll(int R, int G, int B, int D){
- analogWrite(RED, 255-R);
- analogWrite(GREEN, 255-G);
- analogWrite(BLUE, 255-B);
- delay(D);
- }
- bool fade(String color, float D, int MIN, int MAX, int RPT, bool ASC){
- int reps = MAX-MIN;
- float dd = D/reps;
- if(color == "white"){
- for( int k = 0; k < RPT; k +=1){
- if(ASC){
- for( int i = MIN ; i < MAX ; i += 1 ){
- analogWrite( RED, 255 - i );
- analogWrite( GREEN, 255 - i );
- analogWrite( BLUE, 255 - i );
- delay(dd);
- }
- } else {
- for( int i = MIN ; i < MAX ; i += 1 ){
- analogWrite( RED, i );
- analogWrite( GREEN, i );
- analogWrite( BLUE, i );
- delay(dd);
- }
- }
- }
- }
- if(color == "red"){
- for( int k = 0; k < RPT; k +=1){
- if(ASC){
- for( int i = MIN ; i < MAX ; i += 1 ){
- analogWrite( RED, 255 - i );
- analogWrite( GREEN, 255 - 0 );
- analogWrite( BLUE, 255 - 0 );
- delay(dd);
- }
- } else {
- for( int i = MIN ; i < MAX ; i += 1 ){
- analogWrite( RED, i );
- analogWrite( GREEN, 255-0 );
- analogWrite( BLUE, 255-0 );
- delay(dd);
- }
- }
- }
- }
- if(color == "green"){
- for( int k = 0; k < RPT; k +=1){
- if(ASC){
- for( int i = MIN ; i < MAX ; i += 1 ){
- analogWrite( RED, 255 - 0 );
- analogWrite( GREEN, 255 - i );
- analogWrite( BLUE, 255 - 0 );
- delay(dd);
- }
- } else {
- for( int i = MIN ; i < MAX ; i += 1 ){
- analogWrite( RED, 255 );
- analogWrite( GREEN, i );
- analogWrite( BLUE, 255 );
- delay(dd);
- }
- }
- }
- }
- if(color == "blue"){
- for( int k = 0; k < RPT; k +=1){
- if(ASC){
- for( int i = MIN ; i < MAX ; i += 1 ){
- analogWrite( RED, 255 - 0 );
- analogWrite( GREEN, 255 - 0 );
- analogWrite( BLUE, 255 - i );
- delay(dd);
- }
- } else {
- for( int i = MIN ; i < MAX ; i += 1 ){
- analogWrite( RED, 255 );
- analogWrite( GREEN, 255 );
- analogWrite( BLUE, i );
- delay(dd);
- }
- }
- }
- }
- }
- void off(float D){
- lloff(0,0,0,D,100,1);
- }
- bool lloff(int R, int G, int B, float D, float P, int RPT){
- for( int i = 0; i < RPT; i +=1){
- analogWrite(RED, 255-R);
- analogWrite(GREEN, 255-G);
- analogWrite(BLUE, 255-B);
- //float dpp = D*P;
- //float dppc = D*(100-P);
- //if (fdelay( (D*P) /100.0 )) { return true; }
- delay( (D*P) /100.0 );
- if(P < 100){
- analogWrite(RED, 255);
- analogWrite(GREEN, 255);
- analogWrite(BLUE, 255);
- delay( (D*(100-P)) /100.0 );
- }
- }
- return false;
- }
- bool lloffr(float D, float P, int RPT){
- for( int i = 0; i < RPT; i +=1){
- int randr = map(random(2),0,1,0,255);
- int randg = map(random(2),0,1,0,255);
- int randb = map(random(2),0,1,0,255);
- if( randr+randb+randg == 765){randb = 0;}
- analogWrite(RED, randr);
- analogWrite(GREEN, randg);
- analogWrite(BLUE, randb);
- //float dpp = D*P;
- //float dppc = D*(100-P);
- //if (fdelay( (D*P) /100.0 )) { return true; }
- delay( (D*P) /100.0 );
- if(P < 100){
- analogWrite(RED, 255);
- analogWrite(GREEN, 255);
- analogWrite(BLUE, 255);
- delay( (D*(100-P)) /100.0 );
- }
- }
- return false;
- }
- bool lloffrr(float D, float P, int RPT){
- for( int i = 0; i < RPT; i +=1){
- int randr = random(255);
- int randg = random(255);
- int randb = random(255);
- //if( randr+randb+randg == 765){randb = 0;}
- analogWrite(RED, randr);
- analogWrite(GREEN, randg);
- analogWrite(BLUE, randb);
- //float dpp = D*P;
- //float dppc = D*(100-P);
- //if (fdelay( (D*P) /100.0 )) { return true; }
- delay( (D*P) /100.0 );
- if(P < 100){
- analogWrite(RED, 255);
- analogWrite(GREEN, 255);
- analogWrite(BLUE, 255);
- delay( (D*(100-P)) /100.0 );
- }
- }
- return false;
- }
- bool rainbow(float RPT){
- int redVal = 255;
- int blueVal = 0;
- int greenVal = 0;
- float d = (B*RPT)/765.0;
- for( int i = 0 ; i < 255 ; i += 1 ){
- greenVal += 1;
- redVal -= 1;
- analogWrite( GREEN, 255 - greenVal );
- analogWrite( RED, 255 - redVal );
- //Serial.println((B*RPT)/765.0);
- //if (fdelayVar( d,d)) {return true;}
- delay(d);
- }
- redVal = 0;
- blueVal = 0;
- greenVal = 255;
- for( int i = 0 ; i < 255 ; i += 1 ){
- blueVal += 1;
- greenVal -= 1;
- analogWrite( BLUE, 255 - blueVal );
- analogWrite( GREEN, 255 - greenVal );
- delay(d);
- }
- redVal = 0;
- blueVal = 255;
- greenVal = 0;
- for( int i = 0 ; i < 255 ; i += 1 ){
- redVal += 1;
- blueVal -= 1;
- analogWrite( RED, 255 - redVal );
- analogWrite( BLUE, 255 - blueVal );
- delay(d);
- }
- return false;
- }
- void loop() {
- //5 START FLASHES
- //lloff(20,20,20,1000,50,5);
- while(1 == 1){
- if (program == "test"){
- setBPM(112.2);
- if (lloff(250,250,250,B,80,5)) { return; }
- if (fdelay(B*3)) { return; }
- if (lloff(250,0,0,B,80,5)) { return; }
- if (fdelay(B*3)) { return; }
- if (lloff(0,250,0,B,80,5)) { return; }
- if (fdelay(B*3)) { return; }
- if (lloff(0,0,250,B,80,5)) { return; }
- if (fdelay(B*3)) { return; }
- program = "";
- }
- if (program == "none"){
- analogWrite(3, 230);
- if (fdelay(450)) { return; }
- analogWrite(3, 255);
- if (fdelay(450)) { return; }
- analogWrite(5, 230);
- if (fdelay(450)) { return; }
- analogWrite(5, 255);
- if (fdelay(450)) { return; }
- analogWrite(6, 230);
- if (fdelay(450)) { return; }
- analogWrite(6, 255);
- if (fdelay(450)) { return; }
- analogWrite(3, 230);
- if (fdelay(450)) { return; }
- analogWrite(3, 255);
- if (fdelay(450)) { return; }
- program = "";
- }
- if (program == "nas"){
- setBPM(112.2);
- //if (lloff(20,0,0,B,50,94)) { return; }
- //if (fdelay(B*2)) { return; }
- if (lloff(20,0,0,B,80,1)) { return; }
- if (fdelay(B*3)) { return; }
- if (lloff(20,0,0,B,50,5)) { return; }
- if (fdelay(B*3)) { return; }
- if (lloff(20,0,0,B,50,5)) { return; }
- if (fdelay(B*3)) { return; }
- if (lloff(20,0,0,B,50,7)) { return; }
- if (lloff(20,0,0,HB,50,6)) { return; }
- if (fdelay(B*2)) { return; }
- if (lloff(20,0,0,QB,50,2)) { return; }
- if (fdelay(HB)) { return; }
- if (lloff(20,0,0,HB,50,1)) { return; }
- if (fdelay(HB)) { return; }
- if (lloff(20,0,0,HB,50,1)) { return; }
- if (fdelay(HB)) { return; }
- if (lloff(20,0,0,HB,50,1)) { return; } //1,10min
- if (fdelay(B*3)) { return; }
- if (lloff(20,0,0,B,50,94)) { return; }
- program = "";
- }
- if (program == "aoalt"){
- setBPM(120);
- for( int i = 0; i< 50; i += 1){
- ll(100,0,0,QB);
- off(QB);
- ll(0,100,0,QB);
- off(QB);
- ll(0,0,100,QB);
- off(QB);
- ll(0,100,0,QB);
- off(QB);
- ll(100,0,0,QB);
- off(QB);
- ll(0,100,0,QB);
- off(TQB);
- ll(0,0,100,QB);
- off(QB);
- //ll(0,0,100,HALFBEAT);
- }
- program = "";
- }
- if (program == "ahfod"){
- setBPM(123.0);
- //fade("red",B*6,0,255,1,true);
- rainbow(66);
- lloffr(B,100,88);
- lloff(0,0,0,B,100,6);
- off(HB);
- lloff(255,255,255,QB,50,6);
- lloffrr(B,80,65);
- lloff(0,0,0,B,100,31);
- rainbow(32);
- lloff(20,20,20,B,100,6);
- fade("white",B*2,20,255,1,true);
- lloff(255,255,255,B,100,5);
- fade("white",B*1,20,255,1,false);
- fade("white",B*1,20,255,1,true);
- fade("white",B*17,0,255,1,false);
- lloff(20,20,20,B,100,6);
- fade("white",B*2,20,255,1,true);
- lloff(255,255,255,B,100,5);
- fade("white",B*1,20,255,1,false);
- fade("white",B*1,20,255,1,true);
- fade("white",B*16,0,255,1,false);
- lloffr(QB,50,8);
- lloffr(B,50,64);
- lloff(255,255,255,B,100,32);
- fade("white",B*7,0,255,1,false);
- program = "";
- }
- //program = "";
- }
- }
- volatile boolean inService = false;
- void serialInterrupt()
- {
- // Trick: since Serial I/O in interrupt driven, we must reenable interrupts while in this Interrupt Service Routine.
- // But doing so will cause the routine to be called nestedly, causing problems.
- // So we mark we are already in service.
- // Already in service? Do nothing.
- if (inService) return;
- // You was not in service. Now you are.
- inService = true;
- // Reenable interrupts, to allow Serial to work. We do this only if inService is false.
- interrupts();
- readString = "";
- while (Serial.available()) {
- delay(3); //delay to allow buffer to fill
- if (Serial.available() >0) {
- char c = Serial.read(); //gets one byte from serial buffer
- if( c == '\n'){
- Serial.print("Arduino Received: ");
- Serial.print(readString);
- program = readString;
- shouldBreak = true;
- } else {
- readString += c; //makes the string readString
- }
- }
- }
- // Job done.
- inService = false;
- if (readString == "reset"){resetFunc(); }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement