Multipl LED functions, all coming on at once, not one by one.

Hi all, Still very new and learning the language as I go. I am trying to use Arduino to run a few separate functions based on position of a joystick. I have separated each task into a function to help me keep the code clearer (for me).

What I am noticing is the LEDS do not turn off after the joystick position is reset. I have tried FastLED.clear(true); but maybe I have been putting it in the wrong place?

Additionally, once I get that working, it would be good to have an Everything on at once option. I assume I can do this with a separate function, however not sure as it appears I can't separate FastLED.addleds for each function...(

EDIT I have since discovered this is because the NUM_LEDS and DATA_PIN need to be constant. How can I get around this, Ie do a new FastLED instance per target LEDS?

  FastLED.addLeds<WS2812, Smoke_LED, GRB>(leds, NUM_LEDS);  //2 lEDS
  FastLED.addLeds<WS2812, Head_LED, GRB>(leds, NUM_LEDS);  // 2 LEDS
  FastLED.addLeds<WS2812, Jaw_LED, GRB>(leds, NUM_LEDS);  // 3 LEDS

So the current situation is the LEDS all come on and blink at the same time. I need to separate them

Thank you.

#include "FastLED.h"


// Arduino Input PIN numbers
const int Switch_Pin = 2; // digital pin connected to switch output
const int X_Pin = 0; // analog pin connected to X output
const int Y_Pin = 1; // analog pin connected to Y output

//Outputs
const int Smoke_Pin = 3; // pin for smoke  uses digital pins
const int Smoke_LED = 4; // For Smoke LED
const int Head_Pin = 9; // pin for Y direction up (0)
const int Jaw_Pin = 6; // pin for Y directio

void setup() 
{
    randomSeed(analogRead(0));
  // put your setup code here, to run once:

// initialize digital pins
  pinMode(Smoke_Pin, OUTPUT);
  pinMode(Switch_Pin, INPUT);
  digitalWrite(Switch_Pin, HIGH);
  digitalWrite(Smoke_Pin, LOW);
  digitalWrite(Smoke_LED, OUTPUT);
  digitalWrite(Smoke_LED, LOW);
  digitalWrite(Head_Pin, OUTPUT);
  digitalWrite(Head_Pin, LOW);
  digitalWrite(Jaw_Pin, OUTPUT);
  digitalWrite(Jaw_Pin, LOW);
  Serial.begin(115200);


}

void loop() 
{

//Monitor - 
Serial.print("=====================INPUTS=====================");
Serial.print("\n");
Serial.print("Switch_Pin :");
Serial.print(digitalRead(Switch_Pin));
Serial.print("\n");
Serial.print("X Value :");
Serial.print(analogRead(X_Pin));
Serial.print("\n");
Serial.print("Y Value :");
Serial.print(analogRead(Y_Pin));
Serial.print("\n");

Serial.print("\n");
Serial.print("\n");

Serial.print("=====================OUTPUTS=====================");
Serial.print("\n");
Serial.print("Smoke_Pin :");
Serial.print(analogRead(Smoke_Pin));
Serial.print("\n");
Serial.print("Smoke_LED :");
Serial.print(analogRead(Smoke_LED));
Serial.print("\n");
Serial.print("Head_Pin :");
Serial.print(analogRead(Head_Pin));
Serial.print("\n");
Serial.print("Jaw_Pin :");
Serial.print(analogRead(Jaw_Pin));
Serial.print("\n");

 

////////////////////Monitor Joystick / switches here

//SMOKE PIN PRESSED

  if (digitalRead(Switch_Pin == 0))
  {
      
    Smoke();
    
  }



  
//JAW PIN PRESSED

  if (analogRead(Y_Pin) > 1000)
  {

    Jaw();

  }

  
//HEAD PIN PRESSED

  if  (analogRead(Y_Pin) < 50) 
  {

    Head();
  
  }
FastLED.clear(true);

}





//**********FUNCTIONS***********


//SMOKE////////////////////////

void Smoke() 
{
  CRGB leds[2];
  
  FastLED.addLeds<WS2812, Smoke_LED, GRB>(leds, 2);  //Pin number 4
  
  int8_t H = random(0,20); //HUE
  int8_t S = random(250,256); //SAT
  int8_t V = random(250,256); //BRIGHT
  
  leds[0] = CHSV(H,S,V);
  leds[1] = CHSV(H,S,V);
  FastLED.show();
  
//makes white run quicker
if ((H <= 30) && (S >= 0 && S <= 200) && (V <= 200))
{
   // white
   delay(1);
} 
else 
{
   // not white
   delay(500);
}

 // Activates smoke IE activatea pin 3 as active
  digitalWrite(Smoke_Pin, HIGH);  
  delay(1000);
} 

//SMOKE///////////////////////



///////////////HORNS - JAW/////////////////////////////////////////////////

  
void Jaw() 
{
  CRGB leds[3];
  FastLED.addLeds<WS2812, Jaw_Pin, GRB>(leds, 3);  //Pin number 6
  
  int8_t H = random(0,50); //HUE
  int8_t S = random(60,256); //SAT
  int8_t V = random(200,256); //BRIGHT
 
  //middle horn
  
  leds[1] = CHSV(H,S,V);
  FastLED.show();
  
  // below for edge horns to be same
  H = random(0,50); //HUE
  S = random(150,256); //SAT
  V = random(180,230); //BRIGHT
  
  leds[0] = CHSV(H,S,V);
  leds[2] = CHSV(H,S,V);
  FastLED.show();
  
//makes white run quicker
if ((H <= 30) && (S >= 0 && S <= 200) && (V <= 200))
{
   // white
   delay(1);
} 
else 
{
   // not white
   delay(70);
}
}

//////HORNS JAW////////////////////////////////////////////////////////

///////////////HORNS - HEAD/////////////////////////////////////////////////

  
void Head() 
{
  CRGB leds[2];
  FastLED.addLeds<WS2812, Head_Pin, GRB>(leds, 2);  //Pin number 9
  
  int8_t H = random(0,20); //HUE
  int8_t S = random(250,256); //SAT
  int8_t V = random(250,256); //BRIGHT
  
  leds[0] = CHSV(H,S,V);
  leds[1] = CHSV(H,S,V);
  FastLED.show();
  
//makes white run quicker
if ((H <= 30) && (S >= 0 && S <= 200) && (V <= 200))
{
   // white
   delay(1);
} 
else 
{
   // not white
   delay(500);
}
}

//////HORNS JAW////////////////////////////////////////////////////////

After the first two you forgot to change "digitalWrite" to "pinMode" when setting the pin mode.

// initialize digital pins
  pinMode(Smoke_Pin, OUTPUT);
  pinMode(Switch_Pin, INPUT);
  digitalWrite(Switch_Pin, HIGH);
  digitalWrite(Smoke_Pin, LOW);
  digitalWrite(Smoke_LED, OUTPUT);
  digitalWrite(Smoke_LED, LOW);
  digitalWrite(Head_Pin, OUTPUT);
  digitalWrite(Head_Pin, LOW);
  digitalWrite(Jaw_Pin, OUTPUT);
  digitalWrite(Jaw_Pin, LOW);

Cheers, nice pickup :slight_smile:

Changed to:

// initialize digital pins
  pinMode(Smoke_Pin, OUTPUT);
  pinMode(Smoke_LED, OUTPUT);
  pinMode(Switch_Pin, INPUT);
  pinMode(Jaw_Pin, OUTPUT);
  pinMode(Head_Pin, OUTPUT);
  
  digitalWrite(Switch_Pin, HIGH);
  digitalWrite(Smoke_Pin, LOW);
  digitalWrite(Smoke_LED, LOW);
  digitalWrite(Head_Pin, LOW);
  digitalWrite(Jaw_Pin, LOW);
  Serial.begin(115200);

Still having same issue with the LEDS staying on though :frowning:

  if (digitalRead(Switch_Pin == 0))

Classic stupid mistake. Work out on paper just which pin you are reading from. It is probably not the pin you think you are reading from.

Parentheses ( are ( not meant ( to ) be ( splattered ) wherever you feel ) like putting them.

PaulS:

  if (digitalRead(Switch_Pin == 0))

Classic stupid mistake. Work out on paper just which pin you are reading from. It is probably not the pin you think you are reading from.

Parentheses ( are ( not meant ( to ) be ( splattered ) wherever you feel ) like putting them.

Cheers for the super constructive feedback. It seems your style of reply doesn't just apply to me.

I'm new, and trying to learn...It's people like you that ruin it for everyone else...imho

I'm new, and trying to learn...It's people like you that ruin it for everyone else...imho

The two halves of that statement are diametric opposed.

const int Switch_Pin = 2; 
...
...
if (digitalRead(Switch_Pin == 0))

2 does not, and never will, equal 0.

So, the result of the expression is 'false', aka zero.
Do a digital read on pin zero.

Take a close look at your board - what does it say next to pin zero?

Cheers, makes sense.

Must have thrown the Const back into that one when I was mucking around with the Smoke_Pin & NUM_LEDS.

Serial monitor confirms the switch changes from 0 to 1 when pressed but as you say sounds like its a false positive?

The #0 is RX and #1 is TS. I assume I use that one instead RX? How do I assign it to a variable?

Still trying to figure out the ability to set different NUM_LED andLED_PINS :frowning:

the ) on mine has RX beside it.I';; try that pin instead.

Don't do that. You do not want to read from the hardware serial pin.

@Venomouse.
As you agreed with #6, go back and re-read #5 and #3 and you'll find your comments in #4 are uncalled for. I believe you owe Paul and apology. He was attempting, as we all do, to lead you into discovery of your error rather than spoon-feed you a solution.

While I agree with that, it was done in a rather bad tone.

This is not a forum for the faint of heart. We offer guidance and you choose to attend.

Hi,
Can I suggest you read any inputs at the start of the loop and store them in variables, then use the variable to do your comparisons.
For example.

 if (digitalRead(Switch_Pin == 0))

Can be;

byte switchState = 0;
.
void setup()
.
void loop()
 
switchState = digitalRead(Switch_Pin);
Serial.print(switchState);
if (switchState == 0);

if (switchState == 1);

and anytime you want to use switchState.... rather than digitalRead(Switch_Pin)

Looks neater, reading all your inputs at the start, takes a snapshot of your project for you code to work on.

Tom... :slight_smile:

How are the switches wired? Using a pin mode of INPUT_PULLUP is almost always easier.