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!



FastLED Light effec...
 
Share:
Notifications
Clear all

[Solved] FastLED Light effects for Power Rangers

67 Posts
2 Users
1 Reactions
4,774 Views
(@trace)
Estimable Member
Joined: 5 years ago
Posts: 170
Topic starter  

@hans @hans Oh well, the forum freezes again when using the "code" function and my post was empty, I guess. So here is another attempt (the 5th).

The code works well after some corrections from mistakes I have made.

So, now to the next step. How to get this led animation to play only once when a coin is inserted. Just like a user feedback to see, which coin is inserted.

Here is the full code as ino file. Is there another possibility to post the code? I find it easier to handle.

This post was modified 1 year ago by Trace

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2822
 

If your browser allows this (I use Opera), then enable an adblocker. (thanks for being patient)

On to your code: you could fire the effect if a color changes.
Maybe something like this (FastLED reference):

#define NUM_LEDS 26

CRGB leds[NUM_LEDS];

const int Coin_Button_1 = 3; // Coin button 1
const int Coin_Button_2 = 4; // Coin button 2
const int Coin_Button_3 = 5; // Coin button 3

CRGB prevOutsideToCenterColor = CRGB::Black; // added

CRGB outsideToCenterColor = CRGB::Black;
CRGB centerToOutsideColor = CRGB::Black;

...
void loop() {
  ColorSelection();

  if(prevOutsideToCenterColor!=OutsideToCenterColor) {
    OutsideToCenter(1, 50, 100, outsideToCenterColor);
    CenterToOutside(1, 50, 100, centerToOutsideColor);
  }
 }
...

   
ReplyQuote
(@trace)
Estimable Member
Joined: 5 years ago
Posts: 170
Topic starter  

@hans I have enabled the adblocker now. We will see how this influence the forum behaviour.

As far as I understand it, your code checks if the previous-color (set to black) does not match the coin-color and only when this is true (it does not match), executes the animation, right?

I have cleaned up the code a lil bit and added the prevColor part. I have also put it inside the animation void, to keep void loop as clean as as possible. But nothing changed?! Even when I put it in like you did, nothing changes?! What am I missing?

As long as I keep the CoinButtons pressed, the animation continous.

Oh, wait, when prevCoinColor is black and CoinColor is black, this means both matches, so no buttons are pressed and nothing is executed. When I now press CoinButtons (lets say 1 and 2 for red), the prevCoinColor is still black (cause it is set to black) but CoinColor is now red. So (prevCoinColor!=CoinColor) is true and does execute. But then it keeps executing, because the coin buttons are still pressed thus CoinColor is still red and prevCoinColor is set to black so (prevCoinColor!=CoinColor) is still true. Right? So it does the exact opposite of what I want :D

Here is the code (I hope posting with code function works now).

#define FASTLED_INTERNAL  // just used to mute the Pragma messages when compiling
#include "FastLED.h"
#include <EEPROM.h>

#define PIN 6
#define NUM_LEDS 26

CRGB leds[NUM_LEDS];


const int Coin_Button_1 = 3;  // Coin button 1
const int Coin_Button_2 = 4;  // Coin button 2
const int Coin_Button_3 = 5;  // Coin button 3

CRGB prevCoinColor = CRGB::Black;

CRGB CoinColor;

void setup() {
  FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  FastLED.setBrightness(50);
  pinMode(Coin_Button_1, INPUT_PULLUP);
  pinMode(Coin_Button_2, INPUT_PULLUP);
  pinMode(Coin_Button_3, INPUT_PULLUP);
}

void loop() {
  CoinColorSelection();
  CoinInsertAnimation(1, 50, CoinColor);
}

void CoinColorSelection() {
  int button1 = digitalRead(Coin_Button_1) == LOW;
  int button2 = digitalRead(Coin_Button_2) == LOW;
  int button3 = digitalRead(Coin_Button_3) == LOW;

  delay(1000);  // time to wait for buttons getting pressed-> gives time for not  exact simultaneously pressed buttons

  CRGB currentColor;

  // Check for button combinations and assign the corresponding colors to currentColor
  if (button1 && button2) {
    currentColor = CRGB::Red;
  } else if (button1 && button3) {
    currentColor = CRGB::Yellow;
  } else if (button2 && button3) {
    currentColor = CRGB::Blue;
  } else if (button1) {
    currentColor = CRGB::Green;
  } else if (button2) {
    currentColor = CRGB::Pink;
  } else if (button3) {
    currentColor = CRGB::Aqua;
  } else {
    currentColor = CRGB::Black;
  }

  // Update the colors for both effects using the currentColor variable
  CoinColor = currentColor;
}

void CoinInsertAnimation(int EyeSize, int SpeedDelay, CRGB currentColor) {

  if (prevCoinColor!=CoinColor) {

    byte red = currentColor.red;
    byte green = currentColor.green;
    byte blue = currentColor.blue;

    for (int i = 0; i <= ((NUM_LEDS - EyeSize) / 2); i++) {
      setAll(0, 0, 0);

      setPixel(i, red / 10, green / 10, blue / 10);
      for (int j = 1; j <= EyeSize; j++) {
        setPixel(i + j, red, green, blue);
      }
      setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);

      setPixel(NUM_LEDS - i, red / 10, green / 10, blue / 10);
      for (int j = 1; j <= EyeSize; j++) {
        setPixel(NUM_LEDS - i - j, red, green, blue);
      }
      setPixel(NUM_LEDS - i - EyeSize - 1, red / 10, green / 10, blue / 10);

      showStrip();
      delay(SpeedDelay);
    }

    for (int i = ((NUM_LEDS - EyeSize) / 2); i >= 0; i--) {
      setAll(0, 0, 0);

      setPixel(i, red / 10, green / 10, blue / 10);
      for (int j = 1; j <= EyeSize; j++) {
        setPixel(i + j, red, green, blue);
      }
      setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);

      setPixel(NUM_LEDS - i, red / 10, green / 10, blue / 10);
      for (int j = 1; j <= EyeSize; j++) {
        setPixel(NUM_LEDS - i - j, red, green, blue);
      }
      setPixel(NUM_LEDS - i - EyeSize - 1, red / 10, green / 10, blue / 10);

      showStrip();
      delay(SpeedDelay);

      setAll(0, 0, 0);
    }
  }
}

void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
  // NeoPixel
  strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
  // FastLED
  FastLED.show();
#endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
  // NeoPixel
  strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
  // FastLED
  leds[Pixel].r = red;
  leds[Pixel].g = green;
  leds[Pixel].b = blue;
#endif
}

void setAll(byte red, byte green, byte blue) {
  for (int i = 0; i < NUM_LEDS; i++) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}
This post was modified 1 year ago 2 times by Trace

   
ReplyQuote
(@trace)
Estimable Member
Joined: 5 years ago
Posts: 170
Topic starter  

@hans I have an idea. Cant we set something up like "play CoinInsertAnimation" when "CoinColorSelection" changes and check if it is played....if it is not played, play it, if it has already been played, dont play it?

Something like "bool InsertAnimationHasPlayed = false" and setting it "true" after it has been played.....but then it wont play when something changes within "CoinColorSelection" (e.g. another coin gets inserted) cause it already has been played the animation. So how to reset it then?

Also, would it be possible to do it inside the "Void CoinColorSelection"? Because "CoinInsertAnimation" correlates directly to "Void CoinColorSelection". This way, the "Void loop" keeps clean.

Sorry for writing the logic in text, but I cant express it with code language.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2822
 

Forgot to block the ads again and all my typing went down the toilet.
Anyhoo -- I did modify your code a bit. As usual I went a little overboard, even though your code looked very nice!

A few changes I made:

  • Use "define" for the coin buttons (takes no memory, where as a int does take memory)
  • Made more use of the global coinColor (removed from function call and such)
  • Utilized the FastLED library calls instead of my old "universal" calls (setAll, setPixel, etc)
  • Did add "prevcoinColor = coinColor" to store the prev color (probably the only thin you overlooked maybe 😊 )
  • And a few other minor changes ...

It compiles, but I have not tested it. Let me know if that worked 😊 

#define FASTLED_INTERNAL  // just used to mute the Pragma messages when compiling
#include "FastLED.h"
#include <EEPROM.h>

#define PIN 6
#define NUM_LEDS 26

CRGB leds[NUM_LEDS];

#define Coin_Button_1 3  // Coin button 1
#define Coin_Button_2 4  // Coin button 2
#define Coin_Button_3 5  // Coin button 3

CRGB prevcoinColor = CRGB::Black;
CRGB coinColor     = CRGB::Black;

void setup() {
  FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  FastLED.setBrightness(50);
  fill_solid(leds, NUM_LEDS,CRGB::Black);
  FastLED.show();
  
  pinMode(Coin_Button_1, INPUT_PULLUP);
  pinMode(Coin_Button_2, INPUT_PULLUP);
  pinMode(Coin_Button_3, INPUT_PULLUP);
}

void loop() {
  coinColorSelection();
  CoinInsertAnimation(1, 50);
}

void coinColorSelection() {
  int button1 = digitalRead(Coin_Button_1) == LOW;
  int button2 = digitalRead(Coin_Button_2) == LOW;
  int button3 = digitalRead(Coin_Button_3) == LOW;

  delay(1000);  // time to wait for buttons getting pressed-> gives time for not  exact simultaneously pressed buttons

  prevcoinColor = coinColor; // assign old color before determining new color

  // Check for button combinations and assign the corresponding colors to coinColor
  if (button1 && button2) {
    coinColor = CRGB::Red;
  } else if (button1 && button3) {
    coinColor = CRGB::Yellow;
  } else if (button2 && button3) {
    coinColor = CRGB::Blue;
  } else if (button1) {
    coinColor = CRGB::Green;
  } else if (button2) {
    coinColor = CRGB::Pink;
  } else if (button3) {
    coinColor = CRGB::Aqua;
  } else {
    coinColor = CRGB::Black;
  }
}

void CoinInsertAnimation(int EyeSize, int SpeedDelay) {

  if (prevcoinColor!=coinColor) {

    byte red = coinColor.red;
    byte green = coinColor.green;
    byte blue = coinColor.blue;

    for (int i = 0; i <= ((NUM_LEDS - EyeSize)/2); i++) {
      fill_solid(leds, NUM_LEDS,CRGB::Black);
      FastLED.show();

      leds[i] = CRGB(red/10, green/10, blue/10);
      
      for (int j = 1; j <= EyeSize; j++) {
        leds[i + j] = CRGB(red, green, blue);
      }
      leds[i + EyeSize + 1] = CRGB(red/10, green/10, blue/10);

      leds[NUM_LEDS - i] = CRGB(red/10, green/10, blue/10);
      for (int j = 1; j <= EyeSize; j++) {
        leds[NUM_LEDS - i - j] = CRGB(red, green, blue);
      }
      leds[NUM_LEDS - i - EyeSize - 1] = CRGB(red/10, green/10, blue/10);

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

    for (int i = ((NUM_LEDS - EyeSize)/2); i >= 0; i--) {
      fill_solid(leds, NUM_LEDS, CRGB::Black);
      FastLED.show();

      leds[i] = CRGB(red/10, green/10, blue/10);
      for (int j = 1; j <= EyeSize; j++) {
        leds[i + j] = CRGB(red, green, blue);
      }
      leds[i + EyeSize + 1] = CRGB(red/10, green/10, blue/10);

      leds[NUM_LEDS - i] = CRGB(red/10, green/10, blue/10);
      for (int j = 1; j <= EyeSize; j++) {
        leds[NUM_LEDS - i - j] = CRGB(red, green, blue);
      }
      leds[NUM_LEDS - i - EyeSize - 1] = CRGB(red/10, green/10, blue/10);

      FastLED.show();
      delay(SpeedDelay);

      fill_solid(leds, NUM_LEDS, CRGB::Black);
      FastLED.show();
    }
  }
}

   
ReplyQuote
(@trace)
Estimable Member
Joined: 5 years ago
Posts: 170
Topic starter  

@hans Wow, you did not tested it? Well.....it works just flawless 🤩 Thank you very much.

I cant imagine how to be able to modify, clean up and rewrite some noob (me) written code to get it working 🤣 So thanks for your compliment about my code looking nice.

Oh, you got rid of the "neopixel conversion". Of course, never thought about utilizing the fastled library only.

I understand some of your changes, but some not. It is still hard for me to get my head wrapped around some of the logics, which is funny, cause I consider myself a very logical thinking man.

Now that the basic behaviour (color selection) works, I can try to implement more animations for other buttons and also some dfplayer functionality.

Until then, is there an easy way to detect when buttons get released? For instance, I want another animation for when the coinButtons get released (e.g. the coin is getting removed).

To be more specific: The morpher gets opened (which will trigger another button), plays an animation and sound and when this openButton is released (the morpher gets closed), another animation and sound plays. (the sound part is on my side)

Also a bit later, I want to implement two functions for one button. One function for when the button is just clicked and one when the button is getting held.

I have seen some pretty code for this, but I dont know, if this conflicts with other parts of the code yet.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2822
 

Haha, well, feel free to ask if there are parts you'd like to know about.
Also notice: I'm far from perfect, so mistakes will be made and I do not know everything either 😁 

Detecting if a button is released can be done either with an interrupt (see the different modi that can be used in this Arduino document), or by logging when a button was HIGH and seeing when its LOW again. Kind-a like how we log the color change. If that makes sense 😉 


   
ReplyQuote
(@trace)
Estimable Member
Joined: 5 years ago
Posts: 170
Topic starter  

@hans If you could explain in short, how the code keeps track of button change and why it executes the animation just once. In a post earlier, I was thinking about setting a bool with "true" and "false". But your code has a better approach.

I also already managed to implement sound to this effect. At first, the sound kept playing, but I just thought "why not trying to copy and paste the prevCoinColor part and it worked. Of course I had to implement the DFPlayer setup and so on.

Next thing will be the main animation with another button and after that I will try to figure out the hold and release button thing.

You may not be perfect, but for me, it feels close enough :D

  prevcoinColor = coinColor; // assign old color before determining new color

  // Check for button combinations and assign the corresponding colors to coinColor
  if (button1 && button2) {
    coinColor = CRGB::Red;            //Tyranosaurus
    if (prevcoinColor!=coinColor) {
    myDFPlayer.playMp3Folder(1); 
    delay(50);
    }
  } else if (button1 && button3) {
    coinColor = CRGB::White;          //Tigerzord
    if (prevcoinColor!=coinColor) {
    myDFPlayer.playMp3Folder(7); 
    delay(50);
This post was modified 1 year ago by Trace

   
ReplyQuote
(@trace)
Estimable Member
Joined: 5 years ago
Posts: 170
Topic starter  

@hans Did not thought I run into problems that fast 🤣 

The reason for my trouble is, because there is no need to use any neopixel conversion, I dont know how to convert the existing animations to just fastled.

For example:

void MorphAnimation(int SparkleDelay, int SpeedDelay, CRGB currentColor) {

  buttonState = digitalRead(Morph_Button);
  if (buttonState == LOW) {

    byte red = currentColor.red;
    byte green = currentColor.green;
    byte blue = currentColor.blue;

    buttonState = digitalRead(Morph_Button);


    setAll(red, green, blue);

    int Pixel = random(NUM_LEDS);
    setPixel(Pixel, 0xff, 0xff, 0xff);  //Sparkle Color
    showStrip();
    delay(SparkleDelay);
    setPixel(Pixel, red, green, blue);
    showStrip();
    delay(SpeedDelay);
  } else {
    setAll(0, 0, 0);
  }
}

There are a few things I undestand....I think. We dont need CRGB currentColor in the Void argument, because we do it by writing:

void MorphAnimation(int SparkleDelay, int SpeedDelay) {

    byte red = coinColor.red;
    byte green = coinColor.green;
    byte blue = coinColor.blue;

And coinColor gets defined by the CoinColorSelection part, right? This was one of the questions I had before with not understanding some of the new code (why we dont need the CRGB argument anymore).

Also I think instead of buttonState, we can use, what we used before:

int button4 = digitalRead(Morph_Button) == LOW;
if (button4) {

Or is it not necessary?

So I have changed some of the code, but the functions due to neopixel instead of fastled is still wrong, of course:

void MorphAnimation(int SparkleDelay, int SpeedDelay) {
int button4 = digitalRead(Morph_Button) == LOW;

    byte red = coinColor.red;
    byte green = coinColor.green;
    byte blue = coinColor.blue;

if (button4) {

  setAll(red,green,blue);
 
  int Pixel = random(NUM_LEDS);
  setPixel(Pixel,0xff,0xff,0xff); //Sparkle Color
  showStrip();
  delay(SparkleDelay);
  setPixel(Pixel,red,green,blue);
  showStrip();
  delay(SpeedDelay);
}
      else {
    setAll(0, 0, 0);
  }
}

   
ReplyQuote
(@trace)
Estimable Member
Joined: 5 years ago
Posts: 170
Topic starter  

@hans Trial and error is helpfull sometimes. I got it working, but with a lil problem. The SparkleDelay timing is not correct. It is very very slow. Before, with neopixel conversion, it worked great. SparkleDelay and SpeedDelay is set to 20. SparkleDelay looks fine but SpeedDelay looks like 1000. I can change SparkleDelay and it works fine, but changing SpeedDelay does not do anything. So, maybe my code is wrong?

void MorphAnimation(int SparkleDelay, int SpeedDelay) {
  int button4 = digitalRead(Morph_Button) == LOW;

  byte red = coinColor.red;
  byte green = coinColor.green;
  byte blue = coinColor.blue;

  if (button4) {
    fill_solid(leds, NUM_LEDS, coinColor);
    int Pixel = random(NUM_LEDS);
    leds[Pixel] = CRGB(0xff, 0xff, 0xff); // Sparkle Color
    FastLED.show();
    delay(SparkleDelay);
    leds[Pixel] = CRGB(red, green, blue);
    FastLED.show();
    delay(SpeedDelay);
  } 
   else {
    fill_solid(leds, NUM_LEDS, CRGB::Black);
    FastLED.show();
  }
}
This post was modified 1 year ago by Trace

   
ReplyQuote
(@trace)
Estimable Member
Joined: 5 years ago
Posts: 170
Topic starter  

@hans Found the error. It is because of this part:

void coinColorSelection() {
  int button1 = digitalRead(Coin_Button_1) == LOW;
  int button2 = digitalRead(Coin_Button_2) == LOW;
  int button3 = digitalRead(Coin_Button_3) == LOW;

  delay(1000);  // time to wait for buttons getting pressed-> gives time for not  exact simultaneously pressed buttons

The delay for button press causes the problem with sparkle. I know I can handle this with currentMillis and prevMillis, but I have not done that before 😥 


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2822
 

Hi Trace!

Haha, well, that's a bunch of messages, let's see what I can do ...

 

Posted by: @trace
If you could explain in short, how the code keeps track of button change and why it executes the animation just once. In a post earlier, I was thinking about setting a bool with "true" and "false". But your code has a better approach.

Well, basically we start with setting the prevCoinColor and coinColor both to black. Since the color is already black, nothing changed.
Whenever check for what the color should be based on the inserted coin (buttons), we first copy the [current] coinColor to prevCoinColor.
After that we determine what the color should be based on the buttons.
If this new coinColor does not match the prevCoinColor (the color before we determined the new color) then we know that we need to run the effect. 😊 
Hope that makes sense and I hope this is what you meant?

Posted by: @trace
The reason for my trouble is, because there is no need to use any neopixel conversion, I dont know how to convert the existing animations to just fastled.

There are 3 functions that need to be changed ... (note that you can use CRGB::Black or CRGB(0,0,0) - which would be the same)
Using the native FastLED functions are potentially faster and more efficient.
But I see (now) that you already figured that one out 😁

setAll - fill the strip with one color

setAll(red, green, blue); 

now becomes (should be faster and more efficient)

fill_solid(leds, NUM_LEDS, CRGB(red,green,blue));
FastLED.show(); // not sure if this line is needed

 

setPixel - set the color of one LED pixel

setPixel(Pixel, red, green, blue);

becomes

leds[Pixel] = CRGB(red, green, blue);

 

showStrip - Make changes visible

showStrip();

becomes

FastLED.show();

 

Posted by: @trace
The delay for button press causes the problem with sparkle. I know I can handle this with currentMillis and prevMillis, but I have not done that before

And this is where the Arduino shows some limitations since it doesn't do multi tasking. At all.

Ideally you'd want an effect to keep running, while another "task" checks the state of the buttons.

We have a few options here;

You could remove the delay, and call "coinColorSelection" frequent in the effect code. 

In my opinion not an elegant solution, but there will be scenario's where this could work just fine and it would be the simplest to implement.
Downside is that the effect may run slower.

To speed things up, I'd do the button comparison differently. We could use a color array with predefined colors in an array.
I haven't tested this, but as far as I recall, a boolean (one bit) doesn't really exist in Arduino C. Instead they use a byte for this.
Zero means False, anything not zero mean True. (reference)

Now, the code below is not entirely "perfect" but it will probably work (and hopefully makes sense).
As you can see in the void loop, we do not need a bunch of if-statements. Instead we pull the color from a so called multi-dimensional array.
Silly way of putting it: an array with 3 lines, and each line can have 2 values (true or false, or better: 1 or 0).

I have not been able to test this, but the compiler seems to be OK with it.

#include "FastLED.h"

CRGB CoinColors[2][2][2];

void setup() {
  // put your setup code here, to run once:

  CoinColors[0][0][1]=CRGB::Red; // Button 1 and 2 LOW, button 3 HIGH
  CoinColors[0][1][0]=CRGB::Yellow; // Button 1 and 3 LOW, button 2 HIGH
  CoinColors[0][1][1]=CRGB::Blue; 
  CoinColors[1][0][0]=CRGB::Green; 
  CoinColors[0][1][0]=CRGB::Pink; 
  CoinColors[0][0][1]=CRGB::Aqua; 
  CoinColors[1][1][1]=CRGB::Black;
}

void loop() {
  // put your main code here, to run repeatedly:
  bool Button1 = true;
  bool Button2 = true;
  bool Button3 = true;

  coinColor = CoinColors[Button1][Button2][Button3];
}

 Let's start with that, since no matter what the outcome will be, this will be useful 😊 


   
ReplyQuote
(@trace)
Estimable Member
Joined: 5 years ago
Posts: 170
Topic starter  

@hans Hi Hans, and thanks again for your time and patience. And yes, your explanation helped me alot. It is still hard for me to get everything in my head.

Im the type of guy who looks at something, dont know what to do, asking for help and it bothers me to not getting it right by myself. So I then looked up a lot of posts about this and that and tried and tried and tried, until I had an idea and it mysteriously worked.

So here is what I have figured out.
For the sparkle: I have found a neat lil code to avoid delay times and I just added the coin color part:

void MorphAnimation( fract8 chanceOfGlitter) {
   int button4 = digitalRead(Morph_Button) == LOW;

if (button4) {
  fill_solid(leds, NUM_LEDS, coinColor);
    if( random8() < chanceOfGlitter) {
     leds[ random16(NUM_LEDS) ] += CRGB::White;}
    FastLED.show();
}
    else {
    fill_solid(leds, NUM_LEDS, CRGB::Black);
    FastLED.show();
}
}

What do you think? It works and looks good. But I cant figure out, how to set brightness for individual effects. In this case, having the coin color at maybe 100 and the sparkle color at 255. I know I cant use FastLED.setBrightness at all, cause its a global thing. I have also tried CRGB(100,0,0) for the red coin color and CRGB(255,255,255) instead of white. But it does not work. I also have done this with CHSV, which also did not work. 😶 

For the coin delay part:
The compiler says "coinColor was not declared in this scope". And I think I have to read a lot more about basics, cause I know what that means, but I dont know where and how things should be declared (a missing } or ; is not a problem).

And while the array makes sense, Im lost how to implement that. So I just thought, I keep playing with stuff I know and here is my solution:

#include "FastLED.h"

unsigned long previousMillis = 0;
const long interval = 1000;

//the rest of setup and loop//

void coinColorSelection() {

  int button1 = digitalRead(Coin_Button_1) == LOW;
  int button2 = digitalRead(Coin_Button_2) == LOW;
  int button3 = digitalRead(Coin_Button_3) == LOW;

  prevcoinColor = coinColor; // assign old color before determining new color

unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;

//rest of this void//

So I was able to swap delay(1000), which caused problems also with the new sparkle functions, with this lil Millis thing. And it works just fine.

What do you think?

Next thing is to implement another trigger button and also some more sound effects. I will post it when I have found a solution. Until then, I read alot first and just wait before posting too many messages 🤣

btw: enabled adblocker works with posting code


   
ReplyQuote
(@trace)
Estimable Member
Joined: 5 years ago
Posts: 170
Topic starter  

@hans Let me correct something:

But I cant figure out, how to set brightness for individual effects. In this case, having the coin color at maybe 100 and the sparkle color at 255. I know I cant use FastLED.setBrightness at all, cause its a global thing. I have also tried CRGB(100,0,0) for the red coin color and CRGB(255,255,255) instead of white. But it does not work. I also have done this with CHSV, which also did not work.

Of course it works, I just had too low values to be able to see it and I also had the global brightness value still set. So I just have to rewrite all colors with CRGB and can set the sparkle color to 255,255,255. But maybe you have a better, more convenient idea?!

 


   
ReplyQuote
(@trace)
Estimable Member
Joined: 5 years ago
Posts: 170
Topic starter  

@hans Hi Hans and a great Sunday evening. I have a lil problem with using a button as a toggle switch for playing a mp3. I have already I figured out to set two bool values (isPlayingMorph and isPlayingTheme = false). Otherwise it made some problems.

While getting it to work, to have a mp3 file playing only as long as the button is pressed:

//↓↓=============== Morphing Sound ===============↓↓//
void PlayMorph() {

  int button4 = digitalRead(Morph_Button) == LOW;

  if (button4) {
    if (!isPlayingMorph) {              // Play MP3 file when button4 is pressed and if not already playing
      myDFPlayer.loopFolder(2);         // morphing Sound
      delay(50);
      isPlayingMorph = true;
    }
  } else {                              // Stop MP3 file if playing and button4 is released
    if (isPlayingMorph) {
      myDFPlayer.stop();
      delay(50);
      isPlayingMorph = false;
    }
  }
}

I dont get it to work to have a mp3 playing when the button is pushed once and stop when it is pushed again. This was my attempt but it does not work and I dont know why.

//↓↓=============== Theme Song ===============↓↓//
void PlayThemeSong() {

 int button5 = digitalRead(Activate_Button) == LOW;

  if (button5) {
    if (!isPlayingTheme) {              // Play MP3 file when button5 is pressed and if not already playing
      myDFPlayer.playMp3Folder(10);     // ThemeSong
      delay(50);
      isPlayingTheme = true;
    }
      if (isPlayingTheme) {             // Stop MP3 file if already playing and button5 is pressed again
      myDFPlayer.stop();
      delay(50);
      isPlayingTheme = false;
    }
  }
}
//↑↑=============== Theme Song ===============↑↑//

What am I missing? (I also need debounce for this right?)

This post was modified 1 year ago 2 times by Trace

   
ReplyQuote
Page 3 / 5
Share: