0

I wrote code to switch color on a LED strip depending on a value that I set (code below).

When I tried to load the same code on a NodeMCU ESP8266, this code doesn't work anymore. I used the library Adafruit Neopixel, which I thought is supported by the mentioned board.

Any help and/or advice is really welcome.

#include <Adafruit_NeoPixel.h>

//#define ATTINY

#define DIMCOLORE       3
#define NUMCOLORI      (sizeof(colori) / ((DIMCOLORE)*sizeof(byte)))
#define DELTRANSIZIONE (tempoTransizione) / (numOfPixels)

#define SPENTO    0
#define ROSSO     1
#define VERDE     2
#define BLU       3
#define GIALLO    4
#define CIANO     5
#define MAGENTA   6
#define BIANCO    7
#define CELESTINO 8

const byte pinLuci     = 4;
const byte numOfPixels = 51;

const int tresh_1 = 10;  // white
const int tresh_2 = 20;  // blue
const int tresh_3  = 30;  // yellow
const int tresh_4   = 40;  // red

const unsigned long tempoCritico     = 5000;
const unsigned long tempoLampeggio   =  300;
const unsigned long tempoTransizione =  270;

byte colori[][DIMCOLORE] = {
  {  0,   0,   0}, // 0 OF

  {255,   0,   0}, // 1 ROSSO
  {  0, 255,   0}, // 2 VERDE
  {  0,   0, 255}, // 3 BLU

  {255, 130,   0}, // 4 GIALLO
  {  0, 255, 255}, // 5 CIANO
  {255,   0, 255}, // 6 MAGENTA

  {255, 130,  50}, // 7 BIANCO
};

unsigned long inizioAlto = 0;
byte colore              = BIANCO;
byte ultimoColore        = colore;

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(numOfPixels, pinLuci, NEO_GRB + NEO_KHZ800);

void impostaColore(byte indiceColore) {
  for (byte i = 0; i < numOfPixels; i++) pixels.setPixelColor(i, pixels.Color(colori[indiceColore][0], colori[indiceColore][1], colori[indiceColore][2]));
  pixels.show();
}

void impostaColore(byte valR, byte valG, byte valB) {
  for (byte i = 0; i < numOfPixels; i++) pixels.setPixelColor(i, pixels.Color(valR, valG, valB));
  pixels.show();
}

void impostaSequenza(byte indiceColore) {
  for (byte i = 0; i < numOfPixels; i++) {
    pixels.setPixelColor(i, pixels.Color(colori[indiceColore][0], colori[indiceColore][1], colori[indiceColore][2]));
    pixels.show();
    delay(DELTRANSIZIONE);
  }
}

void impostaSequenza(byte valR, byte valG, byte valB) {
  for (byte i = 0; i < numOfPixels ; i++) {
    pixels.setPixelColor(i, pixels.Color(valR, valG, valB));
    pixels.show();
    delay(DELTRANSIZIONE);
  }
}

void lampeggia(byte indiceColore, byte volte, unsigned long tempoLamp) {
  for (byte i = 0 ; i < volte ; i++) {
    impostaColore(0);
    delay(tempoLamp);
    impostaColore(indiceColore);
    delay(tempoLamp);
  }
}


void setup() {
  #ifndef ATTINY
  Serial.begin(115200);
  #endif
  pixels.begin();

  for (byte i = 0; i < 54; i++) {
    pixels.setPixelColor(i, pixels.Color(colori[2][0], colori[2][1], colori[2][2]));
    pixels.show();
  }

  for (byte j = 0 ; j <= NUMCOLORI ; j++) {
    impostaColore(j);
    delay(50);
  }

  impostaColore(BIANCO);
}

void loop() {

  int value = 5;

  #ifndef ATTINY
  Serial.print(millis());
  Serial.print('\t');
  Serial.print(value);
  #endif

  if (value < tresh_1) {
    #ifndef ATTINY
    Serial.print("   0   ");
    #endif
    inizioAlto = 0;
    colore = BIANCO;
  } else if ((value >= tresh_1) && (value < tresh_2) && (value < tresh_3) && (value < tresh_4)) {
    #ifndef ATTINY
    Serial.print(" BASSA ");
    #endif
    inizioAlto = 0;
    colore = BLU;
  } else if ((value >= tresh_2) && (value < tresh_3) && (value < tresh_4)) {
    #ifndef ATTINY
    Serial.print(" MEDIA ");
    #endif
    inizioAlto = 0;
    colore = GIALLO;
  } else if ((value >= tresh_3) && (value < tresh_4)) {
    #ifndef ATTINY
    Serial.print("  ALTA ");
    #endif
    inizioAlto = 0;
    colore = ROSSO;
  } else if (value >= tresh_4)  {
    #ifndef ATTINY
    Serial.print("  MAX  ");
    #endif
    if (inizioAlto == 0) {
      #ifndef ATTINY
      Serial.print(" ----- ");
      #endif
      inizioAlto = millis();
      colore = ROSSO;
    } else if ((millis() - inizioAlto) > tempoCritico) {
      #ifndef ATTINY
      Serial.print(" +++++ ");
      #endif
      lampeggia(ROSSO, 1, tempoLampeggio / 2);
    }
  }

  if (colore != ultimoColore) {
    impostaSequenza(colore);
    ultimoColore = colore;
  }
  #ifndef ATTINY
  Serial.println();
  #endif
  delay(tempoLampeggio / 2);
}

The unique line I changed it is about the pin name:

const byte pinLuci     = D5;

I attached some photos about the hardware connection I made, maybe the problem could be there.

I want to make clear that with Arduino Uno everything works fine.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
  • How do you expect us to see your potentially faulty wiring, if you show us the working one? – gre_gor Jun 30 '18 at 14:05
  • The ESP8266 is a 3.3 V device an the ws2812 LEDs are 5 V devices. There isn't 100% guarantee that it will work. There are workarounds, but that is not a programming solution and is off-topic here. The same for wiring problems. – gre_gor Jun 30 '18 at 14:09
  • I'm voting to close this question as off-topic because it's off-topic as far as Stack Overflow is concerned: https://stackoverflow.com/help/on-topic – Marcel Stör Jun 30 '18 at 18:06
  • I tested the hardware with a lower voltage by using a voltmetro. Then the 3.3 V si not the problem. I shared ma issue because i dont think it si hardware related problem. @marcel – Domenico Vito Scalera Jun 30 '18 at 20:19

1 Answers1

-1

If the error is that i does not compile then post the error from the console.

Else it is properly the wrong pin you are using. The pin numbers and GPIO numbers isn't the same.

If you want to use GPIO04 you should use pin D2 in your sketch. See the following pin mapping

static const uint8_t D0   = 16;
static const uint8_t D1   = 5;
static const uint8_t D2   = 4;
static const uint8_t D3   = 0;
static const uint8_t D4   = 2;
static const uint8_t D5   = 14;
static const uint8_t D6   = 12;
static const uint8_t D7   = 13;
static const uint8_t D8   = 15;
static const uint8_t D9   = 3;
static const uint8_t D10  = 1;
XerXeX
  • 784
  • 4
  • 16
  • the code I posted earlier compiles. I used now the pin declaration with the table you have shown me. So I used: ''static const uint8_t D5 = 14;'' And now I get compiling error. – Domenico Vito Scalera Jun 29 '18 at 23:22
  • amperometroNeopixel_DG:19: error: redefinition of 'const uint8_t D5' static const uint8_t D5 = 14; – Domenico Vito Scalera Jun 29 '18 at 23:28
  • C:\Users\User\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.1\variants\nodemcu/pins_arduino.h:42:22: error: 'const uint8_t D5' previously defined here static const uint8_t D5 = 14; ^ exit status 1 redefinition of 'const uint8_t D5' – Domenico Vito Scalera Jun 29 '18 at 23:36
  • Clearly it looks like the table conversion is alredy defined. So I dealete the declaration line and I simpled use the word ''D5'' where I needed. Now it compiles but ***it still does not illuminate the strip led***. – Domenico Vito Scalera Jun 29 '18 at 23:56