Parsing Char to Compare Values

Hiya everyone,

Ok, I hope I explain what I’m trying to do - who knows I’m pretty sure that someone would be able to do this better, but lets try.

Background:
I’ve got a pinball table, and a bunch of RGB addressable lights, and I want to program them to do the attract mode so that I can put it up on the wall and it will do patterns.

I’m hoping to use FastLED to accomplish this.

I was thinking if I called a function like SetLED(‘001050000000’); (For 12 lights - there’s about 60 but that’s neither here nor there) it would then go through if it’s a 0 set that pixel to off, a 1 to one colour, a 2 different colour again.

So when it ran the above example would have all lights off, except #3 which would be say LightBlue, and #5 which may have white.

The idea being I could have lots and lots of that calls to set the lights to different patterns etc.

Ok, Here is my crap code :slight_smile:

#include <FastLED.h>

#define NUM_LEDS 12  // Number of LEDS
#define DATA_PIN 14 // Led Pins

// This is an array of leds.  One item for each led in your strip.
CRGB leds[NUM_LEDS];
  
char pattern[NUM_LEDS];

void setup() {
  Serial.begin(9600);
      delay(2000);
      FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
}

void loop(){

ShowLED['000000000000'];
ShowLED['100000000000'];
ShowLED['200000000000'];
ShowLED['100000000000'];
ShowLED['000000000000'];
  
}

void ShowLED(char pattern[]){
int SetLED;
  
  for (SetLED = 0; SetLED < NUM_LEDS; SetLED++){
    if (pattern[SetLED] == '0' ) leds[SetLED] = CRGB::Black;
    if (pattern[SetLED] == '1' ) leds[SetLED] = CRGB::LightBlue;
    if (pattern[SetLED] == '2' ) leds[SetLED] = CRGB::White;
    
  }
  FastLED.show();
  delay(50);

}

It’s been a long time since I’ve programmed - and generally it was more either html, a bit of php, and basic. But I’m still fairly new to Arduino.

Thanks for any help - it will be greatly appreciated.

A question would be good...

Ok, it's not working, I'm sure it's something in either how I'm sending it to the function, or I'm checking each value of the array incorrectly....

Essentially, I'm wanting to find out why it's not changing the light colour.

Can anyone see the obvious mistake? Because surely there must be one, well at least one....

ShowLED['000000000000'];

That's not a valid string or character constant. A character constant ('cc') can only hold two characters. For a string constant, use doublequotes:

ShowLED["000000000000"];

That is also not a valid function call. The function arguments go in parens, not square brackets:

ShowLED("000000000000");

That will get you much closer. :)

void loop()
{
  ShowLED("000000000000");
  ShowLED("100000000000");
  ShowLED("200000000000");
  ShowLED("100000000000");
  ShowLED("000000000000");
}

void ShowLED(const char *pattern)
{

Changed "ShowLED(char pattern[])" to "ShowLED(const char *pattern)" because you get a warning if you pass a string constant to a function that doesn't declare the argument 'const'. The 'const' declaration in the argument is the function's promise to not change the characters in the string constant.

John already explained the essentials. Additionally you can (you should) activate the warnings in the IDE compiler settings.

If your sketch is not doing what you expect, add Serial print to see what the sketch is doing. It enables you to spot your bugs.

Just as a demo:

#include <FastLED.h>

#define NUM_LEDS 12  // Number of LEDS
#define DATA_PIN 14 // Led Pins  was 14

// This is an array of leds.  One item for each led in your strip.
CRGB leds[NUM_LEDS];

char pattern[NUM_LEDS];

void setup() {
  Serial.begin(115200);  // we have 2020 - use a faster serial
  delay(2000);
  FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
}

void loop() {

  ShowLED("000000000000");
  ShowLED("102010201020");
  ShowLED("201010101010");
  ShowLED("100000000000");
  ShowLED("020202020202");
}

void ShowLED(const char *pattern) {
  for (int SetLED = 0; SetLED < NUM_LEDS; SetLED++) {
    if (pattern[SetLED] == '0' ) leds[SetLED] = CRGB::Black;
    if (pattern[SetLED] == '1' ) leds[SetLED] = CRGB::Red;
    if (pattern[SetLED] == '2' ) leds[SetLED] = CRGB::White;
    Serial.print(SetLED); Serial.print (" ");
    Serial.print(pattern[SetLED]); Serial.print (" ");
    Serial.print(leds[SetLED]);
    Serial.println();
  }
  FastLED.show();
  delay(50);

  delay(2000); // just for debugging
}

Oh Thank you!! This works a treat, and when you explained things (Thanks John & Nolasca), I had a few doh moments.

Look forward to sharing some of the results.

For those looking for the code - Please use Nolasca's Code.