Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Fade one color to a...
 
Share:
Notifications
Clear all

[Solved] Fade one color to another color Wemos / NodeMCU

18 Posts
2 Users
0 Reactions
6,312 Views
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

@hans

Unfortunately, it didn’t help.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2805
 

Well, you pinned it down to WiFi interfering.

Kind-a odd though ...
I have a very large sketch with a lot of effects on it, which I control over WiFi.
I run that sketch on a ESP8266, just like your Wemos D1, yet not a single effect flickers.

Can you post the bare minimum code with an effect that flickers and WiFi enabled?
My gut tells me that something is triggering an interrupt - just cannot find anything in the code sniplets you have posted before.


   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

Thank you for spending so much time trying to solve my annoying problem.

Here is a stripped version of my code. The 2 functions fadeTo and fadeTo2 are working well without WiFi.

When I WiFi.begin() is enabled the flickering begins.

 

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <FastLED.h>

IPAddress ip(192, 168, 0, 139);
/*
Gateway
Subnet
DNS
*/

bool updateFadeColors1 = false;
bool updateFadeColors2 = false;

#define NUM_LEDS 60
#define DATA_PIN 2              // 2 er D4 på NodeMCU

byte fadeRed = 255;
byte fadeGreen = 0;
byte fadeBlue = 0;
byte fadeRed2 = 0;
byte fadeGreen2 = 255;
byte fadeBlue2 = 0;
byte fadeAmount = 1;
byte fadeDelay = 100;

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(115200);
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); //Typical8mmPixel); // ( TypicalSMD5050 );
  
  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);
 
  if (!client.connected()) {
    reconnect();
  }
  
  setBright(brightness);
  setAll(0,0,255);
  delay(3000);
}

/******************************* START SETUP WIFI**************************************/
void setup_wifi() {
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.hostname(deviceName);
  WiFi.setSleepMode(WIFI_LIGHT_SLEEP);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  WiFi.config(ip, gateway, subnet, dns);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }

  if (WiFi.status() != WL_CONNECTED) {
    delay(1);
    Serial.print("WIFI Disconnected. Attempting reconnection.");
    setup_wifi();
    return;
  }

  client.loop();
  
  // Set true in callback split function
  if (updateFadeColors1 == true) {
    fadeTo(fadeRed, fadeGreen, fadeBlue, fadeDelay, fadeAmount);
  } else if (updateFadeColors2 == true) {
    fadeTo2(fadeRed, fadeGreen, fadeBlue, fadeRed2, fadeGreen2, fadeBlue2, fadeDelay, fadeAmount);
  }
}

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    
    if (client.connect(deviceName, mqtt_username, mqtt_password)) {
      Serial.println("connected");
      di = "modelbane/neopixels/" + neoName;
      di.toCharArray(diSubscribe, 60);
      
      client.subscribe(diSubscribe);
      delay(5000);
    }
  }
}
void callback(char* topic, byte* payload, unsigned int length) {
  // Split received mqtt msg
}

// Helper function that blends one uint8_t toward another by a given amount
void nblendU8TowardU8( uint8_t& cur, const uint8_t target, uint8_t amount)
{
  if( cur == target) return;
  
  if( cur < target ) {
    uint8_t delta = target - cur;
    delta = scale8_video( delta, amount);
    cur += delta;
  } else {
    uint8_t delta = cur - target;
    delta = scale8_video( delta, amount);
    cur -= delta;
  }
}

CRGB fadeOneColor( CRGB& cur, const CRGB& target, uint8_t amount)
{
  nblendU8TowardU8( cur.red,   target.red,   amount);
  nblendU8TowardU8( cur.green, target.green, amount);
  nblendU8TowardU8( cur.blue,  target.blue,  amount);
  return cur;
}

void fadeTowardColor( CRGB* L, uint16_t N, const CRGB& bgColor, uint8_t fadeAmount)
{
  fadeOneColor( L[0], bgColor, fadeAmount); // first LED - calc new color
  if ((bgColor == L[0])) {
    updateFadeColors1 = false;
  }

  for( uint16_t i = 0; i < N; i++) {
     L[i] = L[0]; // copy color from first LED
  }
}

void fadeTowardColor2( CRGB* L, uint16_t N, const CRGB& bgColor, const CRGB& bgColor2, uint8_t fadeAmount)
{
  fadeOneColor( L[0], bgColor, fadeAmount); // Calc new color first LED
  fadeOneColor( L[1], bgColor2, fadeAmount); // and second LED

  if ((bgColor == L[0]) && (bgColor2 == L[1])) {
    updateFadeColors2 = false;
  }
  
  for( uint16_t i = 0; i < N; i++) {
    if ((i % 2) == 0) {
      L[i] = L[0];  // copy 1st LED color
    } else {
      L[i] = L[1]; // copy seconde LED color
    }
  } 
}

void fadeTo(byte toRed, byte toGreen, byte toBlue, int fadeDelay, byte fadeAmount) {       //byte red, byte green, byte blue, byte toRed, byte toGreen, byte toBlue, int fadeDelay, byte fadeAmount) {
  // Syntax: toRed, toGreen, toBlue, fade delay, how much to fade)
    
  CRGB bgColor(toRed, toGreen , toBlue);
  fadeTowardColor( leds, NUM_LEDS, bgColor, fadeAmount);

  FastLED.show();
  FastLED.delay(fadeDelay);  
}

void fadeTo2(byte toRed, byte toGreen, byte toBlue, byte toRed2, byte toGreen2, byte toBlue2, int fadeDelay, byte fadeAmount) {       //byte red, byte green, byte blue, byte toRed, byte toGreen, byte toBlue, int fadeDelay, byte fadeAmount) {
  // Syntax: toRed, toGreen, toBlue, toRed2, toGreen2, toBlue2, fade delay, how much to fade)
    
  CRGB bgColor(toRed, toGreen , toBlue);
  CRGB bgColor2(toRed2, toGreen2, toBlue2);
  fadeTowardColor2( leds, NUM_LEDS, bgColor, bgColor2, fadeAmount);

  FastLED.show();
  FastLED.delay(fadeDelay);  
}

   
ReplyQuote
Page 2 / 2
Share: