bla

Dependencies:   FastPWM HCSR04 NeoStrip mbed

Fork of AdrianLysShow by Gert Lauritsen

Revision:
1:d6beb054bef4
Parent:
0:dcef095e5883
--- a/main.cpp	Tue Sep 08 15:23:32 2015 +0000
+++ b/main.cpp	Thu Jan 28 12:40:41 2016 +0000
@@ -20,6 +20,8 @@
 int SectionSize[8]= {500,343,285,270,395,443,16,16};
 int N=2268;
 
+int CombinedSectionSize[] = {121, 61, 36, 40+90, 152, 120, 122, 40+61, 169+36, 38+39, 37+39, 124, 37+35, 60, 91+94, 61, 89, 152+17, 122, 149}; // Sliser der hænger sammen er i samme section 
+
 int TimeToNet=0;
 int CallAgain=0;
 bool ThereAreSomeThing=0;
@@ -128,6 +130,78 @@
     strip.write();
 }
 
+/*void updateInactive()
+{
+    int LEDNr=0;
+    for (int j=0; j<sizeof(CombinedSectionSize)/sizeof(*CombinedSectionSize); j++) {
+        for (int i=0; i<CombinedSectionSize[j]; i++) {
+            if (j == whichSectionToShow) {
+                strip.setPixel(LEDNr,color[0],color[1],color[2]);
+            } else {
+                strip.setPixel(LEDNr,0,0,0);
+            }
+            LEDNr++;
+            strip.setPixel(182,0,0,0);
+            strip.setPixel(1048,0,0,0);
+        }
+    }
+    strip.write();    
+}
+void updateInactiveSection(int whichSectionToShow)
+{
+    int offset = 0;
+    for (int i=0; i<whichSectionToShow; i++) {
+        offset += CombinedSectionSize[i];   
+    }
+    
+    int LEDNr=offset;
+    
+        for (int i=0; i<CombinedSectionSize[whichSectionToShow]; i++) {
+                strip.setPixel(LEDNr,color[0],color[1],color[2]);
+            LEDNr++;
+            strip.setPixel(182,0,0,0);
+            strip.setPixel(1048,0,0,0);
+        }
+        
+    strip.write();    
+}*/
+
+void updateInactiveSections(int section1, int section2, int section3, int section4, int section5)
+{
+    int LEDNr=0;
+    for (int j=0; j<sizeof(CombinedSectionSize)/sizeof(*CombinedSectionSize); j++) {
+        for (int i=0; i<CombinedSectionSize[j]; i++) {
+            if ((section1 > -1 && j == section1) || (section2 > -1 && j == section2) || (section3 > -1 && j == section3) || (section4 > -1 && j == section4) || (section5 > -1 && j == section5)) {
+                strip.setPixel(LEDNr,color[0],color[1],color[2]);
+            } else {
+                strip.setPixel(LEDNr,0,0,0);
+            }
+            LEDNr++;
+            strip.setPixel(182,0,0,0);
+            strip.setPixel(1048,0,0,0);
+        }
+    }
+    strip.write();    
+}
+
+void updateSections(int section1, int section2, int section3)
+{
+    int LEDNr=0;
+    for (int j=0; j<sizeof(SectionSize)/sizeof(*SectionSize); j++) {
+        for (int i=0; i<SectionSize[j]; i++) {
+            if ((section1 > -1 && j == section1) || (section2 > -1 && j == section2) || (section3 > -1 && j == section3)) {
+                strip.setPixel(LEDNr,color[0],color[1],color[2]);
+            } else {
+                strip.setPixel(LEDNr,0,0,0);
+            }
+            LEDNr++;
+            strip.setPixel(182,0,0,0);
+            strip.setPixel(1048,0,0,0);
+        }
+    }
+    strip.write();    
+}
+
 void ReadColorFactor()
 {
     LocalFileSystem local("local");               // Create the local filesystem under the name "local"
@@ -345,6 +419,72 @@
     }
 }
 
+bool AreSame(float a, float b)
+{
+    return fabs(a - b) < 0.04f;
+}
+
+void setGroup(int i) {
+    switch (i) {
+        case 0:
+            updateInactiveSections(0, 17, 14, 2, 5);
+            break;
+        case 1:
+            updateInactiveSections(4, 13, 19, 18, 0);
+            break;
+        case 2:
+            updateInactiveSections(16, 7, 1, 10, 3);
+            break;
+        case 3:
+            updateInactiveSections(3, 9, 18, 8, 11);
+            break;
+        case 4:
+            updateInactiveSections(8, 11, 15, 6, 0);
+            break;
+        case 5:
+            updateInactiveSections(12, 3, 11, 16, 5);
+            break;
+        case 6:
+            updateInactiveSections(5, 6, 16, 15, 7);
+            break;
+        case 7:
+            updateInactiveSections(15, 10, 14, 2, 19);
+            break;
+        case 8:
+            updateInactiveSections(19, 1, 11, 13, 4);
+            break;
+        case 9:
+            updateInactiveSections(3, 12, 0, 8, 9);
+            break;
+        case 10:
+            strip.setBrightness(0.85f);
+            for (int j=0; j<sizeof(CombinedSectionSize)/sizeof(*CombinedSectionSize); j++) {
+                updateInactiveSections(j, -1, -1, -1, -1);
+                wait_ms(300);
+            }
+            break;
+        case 11:
+            strip.setBrightness(0.7f);
+            for (int i=0; i<4; i++) {
+                for (int j=0; j<6; j++) {
+                    if (j == 4) { // brightness for section 4 (old sections) only.
+                        strip.setBrightness(0.5f);
+                    }
+                    if (j == 5) { // sets to normal again immediatly after section 4
+                        strip.setBrightness(0.7f);
+                    }
+                    
+                    updateSections(j, -1, -1);
+                    wait_ms(500);
+                }
+            }
+            break;
+        case 12:
+            SetAllOn();
+            break;
+    }
+}
+
 int main()
 {
     pc.baud(115200);
@@ -364,26 +504,114 @@
     strip.setBrightness(bright);    // set default brightness
 
     SetAll(0);
+    
+    float brightness = 0.0f;
+    strip.setBrightness(brightness);
+    bool fadein = true;
+    
+    bool hold = false;
+    float holdTime = 0.0f;
+    
+    int groupSelected = 0;
+    
+    bool lysshow = true;
 
     while(1) {
+                
         ThereAreSomeThing=0;
         sensor1->Trigger();
         wait(0.05);
-        if (ThereAreSomeThing) ThereAreNothing=0; else ThereAreNothing++;
-        if (ThereAreNothing>6000) { //5 minutter ingenting
-          InAktive=2; //Inaktiv på den måde at denskal lave noget         
+        
+        if (ThereAreSomeThing) {
+            ThereAreNothing=0; 
+        }
+        else {
+            ThereAreNothing++;
+        }
+        
+        if (ThereAreNothing>0) { //5 minutter ingenting før var den 6000. 100 = 5 sekunder
+            InAktive= true; //Inaktiv på den måde at denskal lave noget        
         }
-        if ((ThereAreSomeThing==0) && (InAktive==2)) InAktive=0; //gå tilbage til almindeligt mode
-        if (InAktive) {
-            if (TimeToNet) TimeToNet--;
+        /*if ((ThereAreSomeThing==0) && (InAktive)) {
+            InAktive=false; //gå tilbage til almindeligt mode
+            printf("Something\n");
+        }*/
+        if (ThereAreSomeThing && InAktive) {
+            /*InAktive=false; //gå tilbage til almindeligt mode
+            fadein = true;
+            hold = false;
+            holdTime = 0.0f;
+            brightness = 0.0f;
+            strip.setBrightness(brightness);
+            SetAll(0.0f);
+            printf("Something\n");*/
+        }
+        //if (InAktive) {
+        //    printf("Inaktive");
+        if (lysshow) {
+            /*if (TimeToNet) TimeToNet--;
+            
             if ((InAktive==2) && (TimeToNet==0)) { //gør noget
               LED[rand() % 6]=1; //Tænder for en tilfældig
               //Der skal gå et stykke tid
               TimeToNet=60; //3 sek
               CalcNew();
-            }    
+            }*/
+            //SetAllOn();
+            //updateInactive();
+            //strip.setBrightness(0.35f); 
+            
+            if ((groupSelected == 10) || (groupSelected == 11) || AreSame(holdTime, 0.35f) ) { //Reset if hold time is reached or after whirl effect
+                fadein = true;
+                hold = false;
+                holdTime = 0.0f;
+                brightness = 0.0f;
+                strip.setBrightness(brightness);
+                
+                int groupSelectedNew = rand() % 12; // normal is 15
+                while(groupSelectedNew == groupSelected) { //set new 
+                    groupSelectedNew = rand() % 12; // normal is 15
+                }
+                if ((groupSelectedNew == 13) && (groupSelectedNew == 14)) groupSelectedNew = 12; //Same effect with tri probability
+                groupSelected = groupSelectedNew;
+                printf("Case %d\r\n", groupSelected);
+                
+                SetAll(0.0f);
+            }
+            
+            setGroup(groupSelected);
+            
+            if ((groupSelected == 12 && AreSame(brightness, 0.19f)) ||  //|| AreSame(brightness, 0.6f)) { //normal på 0.8
+                (groupSelected == 0 && AreSame(brightness, 0.28f)) ||
+                (groupSelected == 1 && AreSame(brightness, 0.20f)) ||
+                (groupSelected == 2 && AreSame(brightness, 0.30f)) ||
+                (groupSelected == 3 && AreSame(brightness, 0.25f)) ||
+                (groupSelected == 4 && AreSame(brightness, 0.30f)) ||
+                (groupSelected == 5 && AreSame(brightness, 0.20f)) ||
+                (groupSelected == 6 && AreSame(brightness, 0.33f)) ||
+                (groupSelected == 7 && AreSame(brightness, 0.22f)) ||
+                (groupSelected == 8 && AreSame(brightness, 0.20f)) ||
+                (groupSelected == 9 && AreSame(brightness, 0.30f))) { //Individual group brightness
+                fadein = false;
+                hold = true;
+            } 
+            if (fadein) {
+                //brightness += 0.013f;
+                brightness += 0.037f;
+            }
+            
+            
+            if (!fadein && hold && (groupSelected == 12)) {
+                holdTime += 0.01f;
+            } else if (!fadein && hold) {
+                holdTime += 0.05f;
+            }
+            
+            //printf("%f\n", brightness);
+            
+            strip.setBrightness(brightness);
         }
-        if (InAktive==0) {
+        if (!InAktive) {
             CalcNew();
             LedOnOff[0]=2;
             LedOnOff[1]=2;
@@ -417,9 +645,18 @@
                     break;
                     //case 'a':  InAktive=1;     break;
                 case 'k': {
-                    InAktive=1;
-                    SetAllOn();
-                    printf("All On !!!!\r\n");
+                    if (lysshow) {
+                        lysshow = false;
+                        InAktive=1;
+                        strip.setBrightness(1.0);
+                        SetAllOn();
+                        printf("All On 100% birghtness!!!!\r\n");
+                    } else {
+                        lysshow = true;
+                        InAktive=0;
+                        strip.setBrightness(0.2);
+                        printf("Continuing normal lightshow!!!!\r\n");  
+                    }
                 }
                 break;
                 case 'K':