Arduino Forum

Using Arduino => LEDs and Multiplexing => Topic started by: JetPackCat on May 28, 2019, 07:04 am

Title: NeoPixel + LED running off of Two switches
Post by: JetPackCat on May 28, 2019, 07:04 am
Hi there all. Relatively new to Arduino and working on coding what I thought should be a simple script for a project I am working on. I'm trying to start out with the basics that I know, and work my way up from there as I learn to understand how the code works.

About the project

I need to run a Neopixel and an LED. I have 2 switches on the circuit with one being the master input, locking out all LEDs and other Input until it goes High, and a second switch that makes my NeoPixel flicker when pressed.

The basic LED should remain static while the main switch is High and the Neopixel should be off while it's low, and static while high.

I might be over explaining it but I hope someone can help. If this has been answered before I'll take better care to look for it and the solve. Thanks in advance.

Code: [Select]

#include <FastLED.h>

#define NUM_LEDS 1
const int triggerPIN = 2;
const int togglePIN = 3;

#define RGB_PIN 12
#define LED_PIN 13

int toggleState = 0;
int triggerState = 0;

CRGB led[NUM_LEDS];

void setup (){
pinMode (LED_PIN, OUTPUT);
pinMode (RGB_PIN, OUTPUT);
pinMode (togglePIN, INPUT);
pinMode (triggerPIN, INPUT);

  FastLED.addLeds<NEOPIXEL, RGB_PIN>(led, NUM_LEDS);

  for (int i = 0; i < NUM_LEDS; i++) {
      led[i] = CRGB(255, 0 , 0);
  }
  FastLED.show();
}

void setRed(int val) {
  for (int i = 0; i < NUM_LEDS; i++){
    led[i] = CRGB(val, 0, 0);
  }
FastLED.show();
}

void loop (){
  toggleState = digitalRead(togglePIN);
  triggerState = digitalRead(triggerPIN);
 if (toggleState == HIGH) {
  digitalWrite(LED_PIN, HIGH);
  if (triggerState == HIGH){
      for (int i = 255; i > 0; i--) {
      setRed(i);
      delay(5);
    }
    for (int i = 0; i < 256; i++) {
      setRed(i);
      delay(5);
      }
  }
      else{
  for (int i = 0; i < NUM_LEDS; i++) {
      led[i] = CRGB(255, 0 , 0);
  }
  FastLED.show();
}
 }
  else{
    digitalWrite(LED_PIN, LOW);
      for (int i = 0; i < NUM_LEDS; i++) {
      led[i] = CRGB(0, 0 , 0);
  }
}
}

  



Title: Re: NeoPixel + LED running off of Two switches
Post by: PaulRB on May 28, 2019, 07:46 am
You got qoutation tags mixed up with code tags, so the forum has corrupted your code. Please modify your post and fix that.

Also please perform an Auto Format on the code before you re-post it.

Equally importantly, you should explain what your code is doing now, and how that is different to what you wanted.
Title: Re: NeoPixel + LED running off of Two switches
Post by: JetPackCat on May 28, 2019, 07:51 am
thanks for the heads up. I didn't see the code option previously. Should be fixed.

At the moment, The code only sets the Neopixel to red, and I can turn on the switch LED, but the SPST I'm using doesn't do anything at all at the moment. I'm currently still experiementing with moving the code around to see if it helps.
Title: Re: NeoPixel + LED running off of Two switches
Post by: PaulRB on May 28, 2019, 08:02 am
+1 Karma for fixing the code tags. Auto Format?
Title: Re: NeoPixel + LED running off of Two switches
Post by: JetPackCat on May 28, 2019, 08:18 am
No, I just manually changed it by typing in code in place of quote
Title: Re: NeoPixel + LED running off of Two switches
Post by: PaulRB on May 28, 2019, 01:07 pm
I meant please Auto Format your code, like I asked before.
Title: Re: NeoPixel + LED running off of Two switches
Post by: JetPackCat on May 29, 2019, 06:39 am
My bad, i wasnt sure what you were asking. Should be good to go now.

Code: [Select]


#include <FastLED.h>

#define NUM_LEDS 1
const int triggerPIN = 2;
const int togglePIN = 3;

#define RGB_PIN 12
#define LED_PIN 13

int toggleState = 0;
int triggerState = 0;

CRGB led[NUM_LEDS];

void setup () {
  pinMode (LED_PIN, OUTPUT);
  pinMode (RGB_PIN, OUTPUT);
  pinMode (togglePIN, INPUT);
  pinMode (triggerPIN, INPUT);

  FastLED.addLeds<NEOPIXEL, RGB_PIN>(led, NUM_LEDS);

  for (int i = 0; i < NUM_LEDS; i++) {
    led[i] = CRGB(255, 0 , 0);
  }
  FastLED.show();
}

void setRed(int val) {
  for (int i = 0; i < NUM_LEDS; i++) {
    led[i] = CRGB(val, 0, 0);
  }
  FastLED.show();
}

void loop () {
  toggleState = digitalRead(togglePIN);
  triggerState = digitalRead(triggerPIN);
  if (toggleState == HIGH) {
    digitalWrite(LED_PIN, HIGH);
    if (triggerState == HIGH) {
      for (int i = 255; i > 0; i--) {
        setRed(i);
        delay(5);
      }
      for (int i = 0; i < 256; i++) {
        setRed(i);
        delay(5);
      }
    }
    else {
      for (int i = 0; i < NUM_LEDS; i++) {
        led[i] = CRGB(255, 0 , 0);
      }
      FastLED.show();
    }
  }
  else {
    digitalWrite(LED_PIN, LOW);
    for (int i = 0; i < NUM_LEDS; i++) {
      led[i] = CRGB(0, 0 , 0);
    }
  }
}


Title: Re: NeoPixel + LED running off of Two switches
Post by: PaulRB on May 29, 2019, 04:49 pm
Code: [Select]
  else {
    digitalWrite(LED_PIN, LOW);
    for (int i = 0; i < NUM_LEDS; i++) {
      led[i] = CRGB(0, 0 , 0);
    }

I think you need another FastLED.show() after the above. Alternatively, remove the for-loop and replace it with setRed(0).
Title: Re: NeoPixel + LED running off of Two switches
Post by: JetPackCat on May 29, 2019, 08:33 pm
That solved some of my issue. Now there is a steady red blink on the neopixel, but it's constant while the main toggle switch is off, and goes solid when the SPST is hit.

I need it to be off, then turn on with the toggle along side the LED, and then pulse/flicker when the SPST is hit.

I tried both fixes, but the FastLED added to the bottom of the For Loop worked.
Title: Re: NeoPixel + LED running off of Two switches
Post by: PaulRB on May 29, 2019, 10:43 pm
I'm still finding your description of the effects of the switches confusing. Can you describe in very simple language? Don't use terms like "locked out", "goes solid", they are ambiguous.

There are 2 switches, so 4 states. Describe, for each led, if they are on, off or pulsing in each state.
Title: Re: NeoPixel + LED running off of Two switches
Post by: JetPackCat on May 30, 2019, 06:35 am
Alright. I'll try to be a bit clearer.

The 1st switch is the master switch. I need the script to not run while it's low. Once it goes High it should set the Neopixel and LED High (static) until the second switch, now enabled by the first, is set High. The LED needs to remain static while the Neopixel pulses.
Title: Re: NeoPixel + LED running off of Two switches
Post by: PaulRB on May 30, 2019, 08:05 am
No better. You are still using ambiguous terms like "remains static". Is that on or off?

Maybe draw a table. 4 rows corresponding to the 4 switch states

2 columns for the 2 LEDs

In each position in the table, the condition of the led in the state: on, off, pulsing

toggle switchmomentary switchNeoPixel ringother led
LOWLOW??
LOWHIGH??
HIGHLOW??
HIGHHIGH??
Title: Re: NeoPixel + LED running off of Two switches
Post by: JetPackCat on May 30, 2019, 09:52 am
Hope this clears things up. Basic graphic attached

(https://forum.arduino.cc/index.php?action=dlattach;topic=618382.0;attach=310513)
Title: Re: NeoPixel + LED running off of Two switches
Post by: PaulRB on May 30, 2019, 04:41 pm
Well, it looks to me like your sketch (plus my correction) should reflect what's shown your diagram.

How are the switches connected? (Schematic, please)

Title: Re: NeoPixel + LED running off of Two switches
Post by: JetPackCat on May 30, 2019, 09:02 pm
Switches connected to Pins 2 and 3. LED on 13, and NeoPixel on 12, connected to 5v and Ground.

(https://forum.arduino.cc/index.php?action=dlattach;topic=618382.0;attach=310579)
Title: Re: NeoPixel + LED running off of Two switches
Post by: PaulRB on May 30, 2019, 09:27 pm
Then your input pins are floating and won't give a reliable signal. Use INPUT_PULLUP. When the switches are closed, the pins will read LOW, not HIGH as your code currently expects.
Title: Re: NeoPixel + LED running off of Two switches
Post by: JetPackCat on Jun 01, 2019, 09:19 am
No luck. Errors out when I Try to compile.

Code: [Select]


Arduino: 1.8.9 (Windows Store 1.8.21.0) (Windows 10), Board: "Arduino/Genuino Uno"

In file included from C:\Users\lockdown\Documents\Arduino\RGB_THrower_Code\RGB_THrower_Code.ino:2:0:

C:\Users\lockdown\Documents\Arduino\libraries\FastLED/FastLED.h:14:21: note: #pragma message: FastLED version 3.002.006

 #    pragma message "FastLED version 3.002.006"

                     ^

C:\Users\lockdown\Documents\Arduino\libraries\FastLED/controller.h: In member function 'clearLeds':

C:\Users\lockdown\Documents\Arduino\libraries\FastLED/controller.h:76:82: internal compiler error: Segmentation fault

  virtual void clearLeds(int nLeds) { showColor(CRGB::Black, nLeds, CRGB::Black); }

                                                                                  ^

Please submit a full bug report,

with preprocessed source if appropriate.

See <http://gcc.gnu.org/bugs.html> for instructions.

lto-wrapper.exe: fatal error: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc returned 1 exit status

compilation terminated.

c:/program files/windowsapps/arduinollc.arduinoide_1.8.21.0_x86__mdqgnx93n4wtt/hardware/tools/avr/bin/../lib/gcc/avr/5.4.0/../../../../avr/bin/ld.exe: error: lto-wrapper failed

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


Title: Re: NeoPixel + LED running off of Two switches
Post by: PaulRB on Jun 01, 2019, 01:30 pm
That's an unrelated error. Undo your changes or go back to the code in post #6 and see if the error goes away.

Did the IDE auto-upgrade itself or some libraries? There was a recent post about this error on the forum.
Title: Re: NeoPixel + LED running off of Two switches
Post by: PaulRB on Jun 01, 2019, 01:37 pm
Try this:
This is a bug with the version of the compiler used in the most recent AVR board package releases - it happens to some people, but not most (don't get me started about what it says about Arduino that they have not addressed this by either downgrading or upgrading the compiler version in the released core) .

Tools -> Boards -> Board Manger, for the official AVR board package, select 1.6.21 (that's the latest good version), install it, and restart the IDE.
Title: Re: NeoPixel + LED running off of Two switches
Post by: JetPackCat on Jun 02, 2019, 10:08 pm
I just swapped out the board to build for and was able to upload. But still nothing. Have experiemented with while loops but that isn't working for me either.