Datatype question in context with neopixel

Just a mental blockage - presumably it is at the wrong data type: I get the variable color_h not displayed; Unless I take the RGB value hard-coded into it.

#include <neopixel.h>

//SYSTEM_MODE(SEMI_AUTOMATIC)


// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_COUNT 12
#define PIXEL_PIN D2
#define PIXEL_TYPE WS2812B

int brightness_h = 10;        // 0-256 nonbright to bright
int brightness_m = 10;        // 0-256 nonbright to bright
int delayval = 1000;          // update scene
int min_px;                   // minute-pixel
int h;                        // hour
unsigned color_m;             // minute-color
unsigned color_h;             // hour-color

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);  // Instance the object

void setup() {
  // Connects to a network with a specified authentication procedure.
  // Options are WPA2, WPA, or WEP.
  // WiFi.setCredentials("Ingo Heim", "PASSWORD", WPA2);
    
  // WiFi.connect(); 
  // while(Serial.read() < 0 && millis() < 10000) Particle.process(); // this waits here till you connect via Serial and hit a key (or max 10sec)
  
//  if (!waitFor(WiFi.ready, 10000))
//    Serial.println("Not able to connect to WiFi for 10sec");
//  else
//  {
//    Serial.print("Here we go SSID: ");
//    Serial.println(WiFi.SSID());
//  }
    
    
  Serial.begin(9600);
  Time.zone (+2.00); // setup a timezone > in this case Berlin
  strip.begin();
}

   /* the colors
    * yellow - 128,128,0
    * orange - 255,128,0
    * red    - 255,0,0
    * green  - 0,255,0
    * blue   - 0,0,255
    * purple - 128,0,128
    */

void loop() {
    
    if (Time.hourFormat12() == 12) {  // neopixel not count from 1-12 like the time-function. we need to change 12 o´clock to pixel 0
    h = 0;
    } else {
    h = Time.hourFormat12(); // https://docs.particle.io/reference/firmware/photon/#hourformat12- - returns integer 1-12
    } 
    Serial.println("**********VALIDATION HOUR******************************");
    Serial.print("Device-Function: ");
    Serial.println(Time.hourFormat12());
    Serial.print("Hour: ");
    Serial.println(h);

    int m = Time.minute();
    
    strip.clear(); // all already switched pixel turned off

    if (m<5) {
      min_px = 0;                   // 0 = first of in this case 12 pixel
      color_m = (0,0,255);          // blue
    } else if (m >= 5 && m<10) {
      min_px = 1;        
      color_m = (0,0,255);          // blue
    } else if (m >= 10 && m<15) {
      min_px = 2;        
      color_m = (0,0,255);          // blue
    } else if (m >= 15 && m<20) {
      min_px = 3;        
      color_m = (0,0,255);          // blue
    } else if (m >= 20 && m<25) {   
      min_px = 4;        
      color_m = (0,0,255);          // blue
    } else if (m >= 25 && m<30) {
      min_px = 5;        
      color_m = (0,0,255);          // blue
    } else if (m >= 30 && m<35) {
      min_px = 6;        
      color_m = (0,0,255);          // blue
    } else if (m >= 35 && m<40) {
      min_px = 7;        
      color_m = (0,0,255);          // blue
    } else if (m >= 40 && m<45) {
      min_px = 8;        
      color_m = (0,0,255);          // blue
    } else if (m >= 45 && m<50) {
      min_px = 9;        
      color_m = (0,0,255);          // blue
    } else if (m >= 50 && m<55) {
      min_px = 10;        
      color_m = (0,0,255);          // blue
    } else if (m >= 55) {
      min_px = 11;        
      color_m = (0,0,255);          // blue
    }
    
    if (h==0) {
      color_h = (0,255,0);          // green
    } else if (h==1) {
      color_h = (0,255,0);          // green
    } else if (h==2) {
      color_h = (0,255,0);          // green
    } else if (h==3) {
      color_h = (255,128,0);        // orange
    } else if (h==4) {   
      color_h = (255,128,0);        // orange
    } else if (h==5) {
      color_h = (255,128,0);        // orange
    } else if (h==6) {
      color_h = (128,0,128);        // purple
    } else if (h==7) {
      color_h = (128,0,128);        // purple
    } else if (h==8) {
      color_h = (128,0,128);        // purple
    } else if (h==9) {
      color_h = (0,0,255);          // blue
    } else if (h==10) {
      color_h = (0,0,255);          // blue
    } else if (h==11) {
      color_h = (0,0,255);          // blue
    }
    
    if (h == min_px) {
      Serial.println("***********EXCEPTION CASE: HOUR = Range of Minute-Pixel");
      Serial.print("Hour: ");
      Serial.println(h);
      Serial.print("Minute: ");
      Serial.println(m);
      Serial.print("Minute-Range: ");
      Serial.println(min_px);
      Serial.println("hour and minute-range is equal - you see 1 pixel bright");

      strip.setPixelColor(h,128,128,0); // h: 1 Pixel in Yellow if hour and minute-range is equal      
      strip.setBrightness(brightness_h);    
      strip.show();
    } else {
      Serial.println("**********NORMAL CASE: HOUR <> Range of Minute-Pixel***");
      Serial.print("Hour: ");
      Serial.println(h);
      Serial.print("Minute: ");
      Serial.println(m);
      Serial.print("Minute-Range: ");
      Serial.println(min_px);
      Serial.println("hour and minute-range is NOT equal - you see 2 pixels bright");
         
**// Display Pixel - hardcoded version works, but color_h as variable not? Which data-type is correct to display (0,0,255)?**          
      strip.setPixelColor(h, color_h); // h: 1 Pixel **-> my problem here**
      strip.setBrightness(brightness_h);    
      strip.show();
      
      strip.setPixelColor(min_px, color_m); // min_px: 1 Pixel represents a period from 5 min
      strip.setBrightness(brightness_m);
      strip.show();
    }
      delay(delayval);
}

What exactly does that mean, where should the displaying happen - don’t make us search your code for it

I found a way for me after a break, ScruffR - no more action necessary.
I split the RGB in 3 vars.

1 Like

When you use the version of setPixelColor that takes two arguments (setPixelColor(uint16_t n, uint32_t c), the value you pass for c is the packed 32 bit definition of the color, where the lowest 8 bits are blue, the next 8 bits are green, then the 8 red bits (the top 8 bits are for white, if you have the kind of LEDs that use that). So, for orange, instead of this,

color_h = (255,128,0);

you need this,

color_h = (16744448); // in binary 00000000 11111111 10000000 00000000 (groups are white, red, green blue)
1 Like

Cool - thx for sharing, Ric.

I usually prefer HEX or even BIN representations, as these are a lot easier to visualise

0x00FF8000 or 0b00000000111111111000000000000000

But this is probably one of the more elegant approaches

/* Types required for RGB-LED control */
struct BRGA_COLOR {
	byte B;  // blue  channel
	byte G;  // green channel
	byte R;  // red   channel
	byte A;  // alpha channel (may be ignored)
};

union COLOR {
	BGRA_COLOR bgra;
	unsigned int value;
} color;


COLOR color_h;

... 
  color_h.value = 0x00FF8000;
  // or
  color_h.bgra = { 0, 128, 255, 0 };  // just observe the reverse order BGRA

1 Like

I was going to suggest the union approach, but I wasn’t sure you could populate the struct like you show above (as opposed to having to do color_h.bgra.b = 0, color_h.bgra.g = 128, etc.). I was just about to test whether that was possible when you posted.

1 Like