Go Down

Topic: ESP8266 fastled delay help - Stranger things light wall (Read 605 times) previous topic - next topic

2micron

Hello all!
Noob here.... working on a simple ESP8266 project with WS2811 leds.
Making a Stranger Things light wall - control 50 led  string via Wifi.
original project here:
https://www.hackster.io/rajington/stranger-things-lights-sign-costume-14132e#toc-standalone--no-backend--version-demo-2
.
props to the original code writer!!!
.
Code: [Select]
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include "FastLED.h"

// WIFI settings
#define WIFI_SSID "dreossi"

// Network settings
#define DNS_PORT 53
#define WEB_PORT 80

IPAddress apIP(192, 168, 1, 1);
DNSServer dnsServer;
ESP8266WebServer webServer(WEB_PORT );

#define PAGE \
  "<!DOCTYPE html>"\
  "<html>"\
  "<head>"\
  "<title>dreossi</title>"\
  "<meta name='viewport' content='initial-scale=1, maximum-scale=1'>"\
  "</head>"\
  "<body style='text-align: center;'>"\
  "<h3>DREOSSI</h3>"\
  "<form>"\
  "<p>Enter your message below!</p>"\
  "<input type='text' name='message'><br>"\
  "<input type='submit' value='Send via space'>"\
  "</form>"\
  "</body>"\
  "</html>"


// the milliseconds to give each letter
#define MILLIS_PER_LETTER 1300

// number of LEDs in the strip
#define NUM_LEDS 50

// the data pin the green wire from the LEDs are connected to
#define DATA_PIN 4

// an array to keep track of the LEDs
CRGB leds[NUM_LEDS];

// the message we will display
String message;

// the time we received the message
unsigned long received;

// the default millis per letter
//int millis_per_letter;

// we'll use all 26 letters of the alphabet
#define NUM_LETTERS 26

// the LED number (start counting from 0) that we light up to show our message
const int LETTER_LEDS[NUM_LETTERS] = {
  /*A*/  7
  ,/*B*/  8
  ,/*C*/  9
  ,/*D*/  10
  ,/*E*/  11
  ,/*F*/  12
  ,/*G*/  13
  ,/*H*/  14
  ,/*I*/  32
  ,/*J*/  31
  ,/*K*/  30
  ,/*L*/  29
  ,/*M*/  28
  ,/*N*/  26
  ,/*O*/  25
  ,/*P*/  24
  ,/*Q*/  23
  ,/*R*/  38
  ,/*S*/  39
  ,/*T*/  40
  ,/*U*/  41
  ,/*V*/  42
  ,/*W*/  44
  ,/*X*/  45
  ,/*Y*/  46
  ,/*Z*/  47
};

// how many colors to cycle through for the lights
#define NUM_COLORS 4

void setup() {
  // send print statements at 9600 baud
  Serial.begin(9600);

  // initialize the LEDS
  FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);

  // set them all to be off
  fill_solid(leds, NUM_LEDS, CRGB::Black);
  FastLED.show();

  // create the wifi network
  Serial.print("Creating the Network");
  Serial.println(WIFI_AP);
  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
  WiFi.softAP(WIFI_SSID);

  // if DNSServer is started with "*" for domain name, it will reply with
  // provided IP to all DNS request
  dnsServer.start(DNS_PORT, "*", apIP);

  // replay to all requests with same HTML
  webServer.onNotFound([]() {
    Serial.println(webServer.uri());
    Serial.print("message: ");
    String data = webServer.arg("message");
    if (data.length()) {
      webServer.send(200, "text/html", PAGE);
      data.toLowerCase();
      Serial.println(data);

      // remember the message and the time it came in
      message = data;
      received = millis();
    }
    webServer.send(200, "text/html", PAGE);
  });
  webServer.begin();

  // this message will show until it is overwritten
  message = "the quick brown fox jumps over the lazy dog";
  received = millis();
}


void loop() {
  dnsServer.processNextRequest();
  webServer.handleClient();

  // how many milliseconds have elapsed since the last message came in
  unsigned long elapsed = millis() - received;

  // assuming MILLIS_PER_LETTER, what letter (index) ofthe message should we be on?
  int index = (elapsed / MILLIS_PER_LETTER) % message.length();

  // get the character letter we should print
  char letter = message.charAt(index);

  // if the character is between 'a' and 'z' (no numbers, spaces, or punctuations)
  if (letter >= 'a' && letter <= 'z') {
    // how bright to make this LED from 0 to 1, this is what makes them fade in and out
    // it calculates what percent we are completed with the letter, and makes it fade in from 0-50% and fade out from 50-100%
    // the formula can be visualized here: https://www.desmos.com/calculator/5qk8imeny4
    float brightness = 1 - abs((2 * (elapsed % MILLIS_PER_LETTER) / ((float)MILLIS_PER_LETTER)) - 1);
    uint8_t value = 255 * brightness;

    // get the LED number the letter should be in, assuming our array starts at 'a' and ends at 'z'
    int letter_index = letter - 'a';
    int led = LETTER_LEDS[letter_index];

    // get a rotation of colors, so that every NUM_COLORS lights, it loops
    // e.g. red, yellow, green, blue, red, yellow green blue
    uint8_t hue = (letter_index % NUM_COLORS * 255) / NUM_COLORS;

    // set that LED to the color
    leds[led] = CHSV(hue, 255, value);
    FastLED.show();
    // set it to black so we don't have to remember the last LED we turned on
    leds[led] = CRGB::Black;

    Serial.print(letter);
    Serial.print("\t!");
    Serial.print(led);
    Serial.print("\t=");
    Serial.print(brightness);
    Serial.print("\t@");
    Serial.print(elapsed);
    Serial.println();
  } else {
    // if the letter wasn't a-z then, we just turn off all the leds
    FastLED.show();
  }
}

.
It is up and running, but I need some help please to make a couple adjustments.
When you ask for a set of the same letters, example aaaaaaaaaaa, there is no delay between the letters. I would like to have the same delay - 1000 when the same led letter is called. otherwise, it simply stay on the way it is now.
.
Also, can we make all the 50 leds flash or blink between messages?
Thank you in advance,
Andrew

PaulRB

+1 karma for using code tags in your first post.

2micron

Thanks Paul!
lets get some more smart people to help with this!
Andrew

PaulRB

Quote
When you ask for a set of the same letters, example aaaaaaaaaaa, there is no delay between the letters. I would like to have the same delay - 1000 when the same led letter is called.
Hmmm. I'm having trouble spotting why the code would do that. There's no code that says "if the next character is different/same as the last character, do something different...". What makes you convinced that's what it's doing?

2micron

Right now, when you call aaaaaaaa, that identified Led will simply stay lit, with no delay or dimming.
It will delay and dim (1000) when I input a space in the call up, like this:
a a a a a a a a

PaulRB

Ok, that's strange.

Can you post the output you see on serial monitor? (Use code tags again please).

2micron


2micron

Ok, having trouble posting the output from the serial monitor.... but did run it on aaaaaaaaa
Shows:
-> a     !7       =1.00    @57835
.
Same every line, just different time stamp.
Not positive if this is what you asked for?

2micron

ok, ran ababaaaaa
Code: [Select]
14:58:24.136 -> a !7 =1.00 @362665
14:58:24.190 -> a !7 =1.00 @362686
14:58:24.190 -> b !8 =1.00 @362707
14:58:24.237 -> b !8 =1.00 @362728
14:58:24.237 -> b !8 =1.00 @362749
14:58:24.237 -> b !8 =1.00 @362769
14:58:24.290 -> b !8 =1.00 @362790
14:58:24.290 -> b !8 =1.00 @362811
14:58:24.337 -> b !8 =1.00 @362832
14:58:24.337 -> b !8 =1.00 @362853
14:58:24.390 -> b !8 =1.00 @362874
14:58:24.390 -> b !8 =1.00 @362894
14:58:24.390 -> b !8 =1.00 @362915
14:58:24.437 -> b !8 =1.00 @362936
14:58:24.437 -> b !8 =1.00 @362957
14:58:24.490 -> b !8 =1.00 @362978
14:58:24.490 -> b !8 =1.00 @362998
14:58:24.490 -> b !8 =1.00 @363019
14:58:24.537 -> b !8 =1.00 @363040
14:58:24.537 -> b !8 =1.00 @363061
14:58:24.591 -> b !8 =1.00 @363082
14:58:24.591 -> b !8 =1.00 @363103
14:58:24.591 -> b !8 =1.00 @363123
14:58:24.638 -> b !8 =1.00 @363144
14:58:24.638 -> b !8 =1.00 @363165
14:58:24.691 -> b !8 =1.00 @363186
14:58:24.691 -> b !8 =1.00 @363207
14:58:24.738 -> b !8 =1.00 @363228
14:58:24.738 -> b !8 =1.00 @363248
14:58:24.738 -> b !8 =1.00 @363269
14:58:24.791 -> b !8 =1.00 @363290
14:58:24.791 -> b !8 =1.00 @363311
14:58:24.838 -> b !8 =1.00 @363332
14:58:24.838 -> b !8 =1.00 @363353
14:58:24.892 -> b !8 =1.00 @363374
14:58:24.892 -> b !8 =1.00 @363394
14:58:24.892 -> b !8 =1.00 @363415
14:58:24.938 -> b !8 =1.00 @363436
14:58:24.938 -> b !8 =1.00 @363457
14:58:24.992 -> b !8 =1.00 @363478
14:58:24.992 -> b !8 =1.00 @363499
14:58:24.992 -> b !8 =1.00 @363519
14:58:25.039 -> b !8 =1.00 @363540
14:58:25.039 -> b !8 =1.00 @363561
14:58:25.092 -> b !8 =1.00 @363582
14:58:25.092 -> b !8 =1.00 @363603
14:58:25.092 -> b !8 =1.00 @363623
14:58:25.139 -> b !8 =1.00 @363644
14:58:25.139 -> b !8 =1.00 @363665
14:58:25.192 -> b !8 =1.00 @363686
14:58:25.192 -> b !8 =1.00 @363707
14:58:25.228 -> b !8 =1.00 @363728
14:58:25.228 -> b !8 =1.00 @363748
14:58:25.275 -> b !8 =1.00 @363769
14:58:25.275 -> b !8 =1.00 @363790
14:58:25.308 -> b !8 =1.00 @363811
14:58:25.308 -> b !8 =1.00 @363832
14:58:25.355 -> b !8 =1.00 @363853
14:58:25.355 -> b !8 =1.00 @363873
14:58:25.393 -> b !8 =1.00 @363894
14:58:25.393 -> b !8 =1.00 @363915
14:58:25.440 -> b !8 =1.00 @363936
14:58:25.440 -> b !8 =1.00 @363957
14:58:25.493 -> b !8 =1.00 @363978
14:58:25.493 -> b !8 =1.00 @363998
14:58:25.493 -> a !7 =1.00 @364019
14:58:25.540 -> a !7 =1.00 @364040
14:58:25.540 -> a !7 =1.00 @364061
14:58:25.593 -> a !7 =1.00 @364082
14:58:25.593 -> a !7 =1.00 @364103
14:58:25.593 -> a !7 =1.00 @364123
14:58:25.640 -> a !7 =1.00 @364144
14:58:25.640 -> a !7 =1.00 @364165
14:58:25.694 -> a !7 =1.00 @364186
14:58:25.694 -> a !7 =1.00 @364207
14:58:25.740 -> a !7 =1.00 @364228
14:58:25.740 -> a !7 =1.00 @364248
14:58:25.740 -> a !7 =1.00 @364269
14:58:25.794 -> a !7 =1.00 @364290
14:58:25.794 -> a !7 =1.00 @364311
14:58:25.841 -> a !7 =1.00 @364332
14:58:25.841 -> a !7 =1.00 @364353
14:58:25.894 -> a !7 =1.00 @364373
14:58:25.894 -> a !7 =1.00 @364394
14:58:25.894 -> a !7 =1.00 @364415
14:58:25.941 -> a !7 =1.00 @364436
14:58:25.941 -> a !7 =1.00 @364457
14:58:25.994 -> a !7 =1.00 @364478
14:58:25.994 -> a !7 =1.00 @364498
14:58:25.994 -> a !7 =1.00 @364519
14:58:26.041 -> a !7 =1.00 @364540
14:58:26.041 -> a !7 =1.00 @364561
14:58:26.095 -> a !7 =1.00 @364582
14:58:26.095 -> a !7 =1.00 @364603
14:58:26.095 -> a !7 =1.00 @364623
14:58:26.141 -> a !7 =1.00 @364644
14:58:26.141 -> a !7 =1.00 @364665
14:58:26.195 -> a !7 =1.00 @364686
14:58:26.195 -> a !7 =1.00 @364707
14:58:26.242 -> a !7 =1.00 @364728
14:58:26.242 -> a !7 =1.00 @364748
14:58:26.242 -> a !7 =1.00 @364769
14:58:26.295 -> a !7 =1.00 @364790
14:58:26.295 -> a !7 =1.00 @364811
14:58:26.342 -> a !7 =1.00 @364832
14:58:26.342 -> a !7 =1.00 @364853
14:58:26.342 -> a !7 =1.00 @364873
14:58:26.395 -> a !7 =1.00 @364894
14:58:26.395 -> a !7 =1.00 @364915
14:58:26.442 -> a !7 =1.00 @364936
14:58:26.442 -> a !7 =1.00 @364957
14:58:26.496 -> a !7 =1.00 @364978
14:58:26.496 -> a !7 =1.00 @364998
14:58:26.496 -> a !7 =1.00 @365019
14:58:26.543 -> a !7 =1.00 @365040
14:58:26.543 -> a !7 =1.00 @365061
14:58:26.596 -> a !7 =1.00 @365082
14:58:26.596 -> a !7 =1.00 @365103
14:58:26.596 -> a !7 =1.00 @365123
14:58:26.643 -> a !7 =1.00 @365144
14:58:26.643 -> a !7 =1.00 @365165
14:58:26.696 -> a !7 =1.00 @365186
14:58:26.696 -> a !7 =1.00 @365207
14:58:26.696 -> a !7 =1.00 @365228
14:58:26.743 -> a !7 =1.00 @365248
14:58:26.743 -> a !7 =1.00 @365269
14:58:26.796 -> a !7 =1.00 @365290
14:58:26.796 -> b !8 =1.00 @365311
14:58:26.843 -> b !8 =1.00 @365332
14:58:26.843 -> b !8 =1.00 @365353
14:58:26.843 -> b !8 =1.00 @365373
14:58:26.897 -> b !8 =1.00 @365394
14:58:26.897 -> b !8 =1.00 @365415
14:58:26.944 -> b !8 =1.00 @365436
14:58:26.944 -> b !8 =1.00 @365457
14:58:26.997 -> b !8 =1.00 @365478
14:58:26.997 -> b !8 =1.00 @365498
14:58:26.997 -> b !8 =1.00 @365519
14:58:27.044 -> b !8 =1.00 @365540
14:58:27.044 -> b !8 =1.00 @365561
14:58:27.097 -> b !8 =1.00 @365582
14:58:27.097 -> b !8 =1.00 @365603
14:58:27.097 -> b !8 =1.00 @365623
14:58:27.144 -> b !8 =1.00 @365644
14:58:27.144 -> b !8 =1.00 @365665
14:58:27.198 -> b !8 =1.00 @365686
14:58:27.198 -> b !8 =1.00 @365707
14:58:27.198 -> b !8 =1.00 @365728
14:58:27.244 -> b !8 =1.00 @365748
14:58:27.244 -> b !8 =1.00 @365769
14:58:27.298 -> b !8 =1.00 @365790
14:58:27.298 -> b !8 =1.00 @365811
14:58:27.334 -> b !8 =1.00 @365832
14:58:27.334 -> b !8 =1.00 @365853
14:58:27.381 -> b !8 =1.00 @365873
14:58:27.381 -> b !8 =1.00 @365894
14:58:27.429 -> b !8 =1.00 @365915
14:58:27.429 -> b !8 =1.00 @365936
14:58:27.429 -> b !8 =1.00 @365957
14:58:27.476 -> b !8 =1.00 @365978
14:58:27.476 -> b !8 =1.00 @365998
14:58:27.514 -> b !8 =1.00 @366019
14:58:27.514 -> b !8 =1.00 @366040
14:58:27.561 -> b !8 =1.00 @366061
14:58:27.561 -> b !8 =1.00 @366082
14:58:27.599 -> b !8 =1.00 @366103
14:58:27.599 -> b !8 =1.00 @366123
14:58:27.645 -> b !8 =1.00 @366144
14:58:27.645 -> b !8 =1.00 @366165
14:58:27.699 -> b !8 =1.00 @366186
14:58:27.699 -> b !8 =1.00 @366207
14:58:27.699 -> b !8 =1.00 @366227
14:58:27.746 -> b !8 =1.00 @366248
14:58:27.746 -> b !8 =1.00 @366269
14:58:27.799 -> b !8 =1.00 @366290
14:58:27.799 -> b !8 =1.00 @366311
14:58:27.846 -> b !8 =1.00 @366332
14:58:27.846 -> b !8 =1.00 @366353
14:58:27.846 -> b !8 =1.00 @366373
14:58:27.899 -> b !8 =1.00 @366394
14:58:27.899 -> b !8 =1.00 @366415
14:58:27.946 -> b !8 =1.00 @366436
14:58:27.946 -> b !8 =1.00 @366457
14:58:27.946 -> b !8 =1.00 @366478
14:58:28.000 -> b !8 =1.00 @366498
14:58:28.000 -> b !8 =1.00 @366519
14:58:28.046 -> b !8 =1.00 @366540
14:58:28.046 -> b !8 =1.00 @366561
14:58:28.100 -> b !8 =1.00 @366582
14:58:28.100 -> a !7 =1.00 @366602
14:58:28.100 -> a !7 =1.00 @366623
14:58:28.147 -> a !7 =1.00 @366644
14:58:28.147 -> a !7 =1.00 @366665
14:58:28.200 -> a !7 =1.00 @366686
14:58:28.200 -> a !7 =1.00 @366707



Paul__B

When you ask for a set of the same letters, example aaaaaaaaaaa, there is no delay between the letters. I would like to have the same delay - 1000 when the same led letter is called. otherwise, it simply stay on the way it is now.
Is that perhaps because you never provided any delay between letters?

Also, can we make all the 50 leds flash or blink between messages?
Yes.

You will need code to keep track of the last letter displayed, and if the next letter is the same, blank the display for your preferred amount of time before actually displaying that letter.

david_2018

Look at the variables being used for the calculation of elapsed and brightness, something seems wrong there. I can't replicate the problem here, cutting out the parts for the ESP8266 and running it on a nano seems to work correctly.

2micron

"You will need code to keep track of the last letter displayed, and if the next letter is the same, blank the display for your preferred amount of time before actually displaying that letter."
Hey Paul, thank you!
Sounds simple...... for you.
Would you be so kind to lend an example?

Paul__B

Oooh, well, sorry but .. I would have to actually learn the whole of your code to do that - a bit busy at the present I am afraid.  :smiley-eek:

SteveMann

#13
Dec 24, 2019, 05:25 am Last Edit: Dec 24, 2019, 05:30 am by SteveMann
Very creative use of millis.  I will have to remember this one.

Here is what I would try.
Delay between same sequential letters:
At the top of the loop, after
Code: [Select]
void loop() {
  dnsServer.processNextRequest();
  webServer.handleClient();

 
Create a function to "fix" message.  Scan the message for sequential letters, and if found, insert a period or space or something not a-z.

Flash between messages:
Append a unique character at the end of message, like "~".

When the character is not a-z, in your else
Code: [Select]
} else {
    // if the letter wasn't a-z then, we just turn off all the leds

See if it is the EOF flag, "~".  If it is, then you can flash the whole string.


Now I have a nit.
Code: [Select]
String message;
After you get it working, you should really learn to use c-strings instead of Arduino String class.  You are simply extracting a char from the String, so converting to doing it right should not be much of an exercise.

Here's a good read about the Evil Arduino String.

2micron

Hey Steve, great hints, I am trying!
Sounds like a simple thing to fix..... but it's kicking my but!!!

Go Up