Displays a gradient between two colors

Dependencies:   mbed WS2812 PixelArray

Revision:
2:c4a43cc93ca8
Parent:
1:3fa4e131f518
Child:
3:5fd199581f15
--- a/main.cpp	Mon Apr 19 12:08:45 2021 +0000
+++ b/main.cpp	Wed Apr 21 12:40:36 2021 +0000
@@ -1,14 +1,15 @@
 #include "mbed.h"
 #include "WS2812.h"
+#include <stdio.h>
 #include "PixelArray.h"
 
 #define WS2812_BUF 16   //number of LEDs in the array
-#define NUM_COLORS 6  //number of colors to store in the array
+#define NUM_COLORS 14  //number of colors to store in the array
 #define PIN_NUM A2
 #define LIGHT_INTENSITY 30
 
 using namespace std;
-DigitalOut usrLed(LED1);
+
 PixelArray px(WS2812_BUF);
 DigitalIn usrBtn(USER_BUTTON);
 
@@ -37,110 +38,145 @@
     ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
     
     // set up the colours we want to draw with
-    int colorbuf[NUM_COLORS] = {0xff0000,0x0000ff,0x0002f0,0x00002f,0x00002f,0x2f002f};
+    int colorbuf[NUM_COLORS] = {0xff0000,0x00FFFF,0x0000FF,0xFF00FF,0x808080,0x00FF00,
+                                0xFF0000,0xFFFFFF,0xFFFF00,0x008080,0xFF0099,0xFFF0F5,
+                                0x991199,0x808000};
     
     //get starting RGB components for interpolation
     size_t c1 = colorbuf[0];
     size_t r1 = (c1 & 0xff0000) >> 16;
-    size_t g1 = (c1 & 0x00ff00) >> 8;
-    size_t b1 = (c1 & 0x0000ff);
+    size_t g1 = (c1 & 0x00FFFF) >> 8;
+    size_t b1 = (c1 & 0x0000FF);
     
     //get ending RGB components for interpolation
     size_t c2 = colorbuf[1];
-    size_t r2 = (c2 & 0xff0000) >> 16;
-    size_t g2 = (c2 & 0x00ff00) >> 8;
-    size_t b2 = (c2 & 0x0000ff);
+    size_t r2 = (c2 & 0xFF00FF) >> 16;
+    size_t g2 = (c2 & 0x808080) >> 8;
+    size_t b2 = (c2 & 0x00FF00);
     
     for (int i = 0; i <= WS2812_BUF; i++)
     {
         ir = interpolate(r1, r2, i, WS2812_BUF);
         ig = interpolate(g1, g2, i, WS2812_BUF);
         ib = interpolate(b1, b2, i, WS2812_BUF);
-        
         //write the color value for each pixel
         px.Set(i, color_set(ir,ig,ib));
-        
-        //write the II value for each pixel
-        /*px.SetI(4, 30);
-        px.SetI(1, 30);
-        px.SetI(2, 30);
-        px.SetI(7, 30);
-        px.SetI(12, 30);
-        px.SetI(15, 30);*/// смайлик убийца 
-        
-        px.SetI(i, 10);
- 
-        /*px.SetI(10, 15);
-
-        px.SetI(15, 20);
+        //write the II value for each pixel   
+       
 
-        px.SetI(3, 25);
- 
-        px.SetI(6, 30);
+    }
+    
+    // цикл плавного прохода по каждому светодиоду и обратное угасание 
+    while(true){
+    for (int i = 0; i<WS2812_BUF; i++){
+        for (int j = 0; j<30 ; j++){
+             px.SetI(i, j);
+             wait(0.01);
+             ws.write(px.getBuf());
+             }
+    }
+    
+    
+     for (int x = WS2812_BUF; x>=0; x-- )
+         for (int j = 30; j>0 ; j--){
+             px.SetI(x, j);
+             wait(0.01);
+             ws.write(px.getBuf());
+             }
+    }
+    
+    // цикл плавного прохода по диагонали 
+    /*while(true){
+    for (int x = 0; x<=20; x+=5 ){
+            for(int i = 0; i<30; i++){
+                px.SetI(x,i);
+                wait(0.01);
+                ws.write(px.getBuf());
+    }
+    }
+  
+    for (int x = 20; x>=0; x-=5 ){
+          for(int i = 30; i>=0; i--){  
+                px.SetI(x,i);
+                wait(0.01);
+                ws.write(px.getBuf());
+    }
+    }}*/
+    
+    // Цикл плавного крестика
+    /*
+    while(true){
+    for (int x = 0; x<=20; x+=5 ){
+            for(int i = 0; i<30; i++){
+                px.SetI(x,i);
+                wait(0.01);
+                ws.write(px.getBuf());
+    }
+    }
+    for (int x = 3; x<=12; x+=3 ){
+            for(int i = 0; i<30; i++){
+                px.SetI(x,i);
+                wait(0.01);
+                ws.write(px.getBuf());
+    }
+    }
+    for (int x = 12; x>=3; x-=3 ){
+          for(int i = 30; i>=0; i--){  
+                px.SetI(x,i);
+                wait(0.01);
+                ws.write(px.getBuf());
+    }
+    }
+  
+   for (int x = 0; x<=20; x-=5 ){
+          for(int i = 30; i>=0; i--){  
+                px.SetI(x,i);
+                wait(0.01);
+                ws.write(px.getBuf());
+    }
+    }
+    }
+    */
+    /* int d = 20;
+    while(true){
+    for (int x = 0; x<=20; x+=5 ){
+            for(int i = 0; i<30; i++){
+                px.SetI(x,i);
+               wait(0.01);
+                px.SetI(d-=2,i);
+                    wait(0.01);
+                    ws.write(px.getBuf());
+    }
+    }
 
-        px.SetI(9, 15);
-        
-        px.SetI(12, 5);*/
-        
+    for (int x = 12; x>=3; x-=3 ){
+          for(int i = 30; i>=0; i--){  
+                px.SetI(x,i);
+                wait(0.01);
+                ws.write(px.getBuf());
+    }
+    }
+  
+   for (int x = 0; x<=20; x-=5 ){
+          for(int i = 30; i>=0; i--){  
+                px.SetI(x,i);
+                wait(0.01);
+                ws.write(px.getBuf());
     }
-    /*
-    for (int i = WS2812_BUF; i >= 0; i--) 
-    {
-        ws.write(px.getBuf());
-    }*/
+    }
+    }
+    */
     
-        usrLed = 1;
+
+  
+
+
+      /*  
+        for (int i = WS2812_BUF; i >= 0; i--) 
+        {
+            ws.write(px.getBuf());
+        }*/
     
     
 
-    
-   /*
-        while (1) 
-    {
-        if (usrBtn == 0)    //button is pressed
-        { 
-         //write the color value for each pixel
-        px.SetAll(colorbuf[btnState]);
-        //write the II value for each pixel
-        px.SetAllI(64);
-        
-            usrLed = 1;
-            btnState = btnState++;
-            
-            if (btnState == NUM_COLORS) {
-                btnState = 0;
-            }
-        } 
-        else 
-        {
-        px.SetAll(colorbuf[btnState]);
-        px.SetI(0, LIGHT_INTENSITY);
-        
-        px.SetI(5, LIGHT_INTENSITY);
- 
-        px.SetI(10, LIGHT_INTENSITY);
-
-        px.SetI(15, LIGHT_INTENSITY);
-
-        px.SetI(3, LIGHT_INTENSITY);
- 
-        px.SetI(6, LIGHT_INTENSITY);
-
-        px.SetI(9, LIGHT_INTENSITY);
-        
-        px.SetI(12, LIGHT_INTENSITY);
-            
-            usrLed = 0;
-        }
-        
-       
-
-        
-        for (int i = WS2812_BUF; i >= 0; i--) 
-        {
-            ws.write(px.getBuf());
-        }
-    
-    }*/
-
 }