Go Down

Topic: NeoPixel + LED running off of Two switches  (Read 912 times) previous topic - next topic

JetPackCat

May 28, 2019, 07:04 am Last Edit: May 28, 2019, 07:51 am by JetPackCat
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);
  }
}
}

  




PaulRB

#1
May 28, 2019, 07:46 am Last Edit: May 28, 2019, 07:53 am by PaulRB
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.

JetPackCat

#2
May 28, 2019, 07:51 am Last Edit: May 28, 2019, 08:00 am by JetPackCat
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.

PaulRB

#3
May 28, 2019, 08:02 am Last Edit: May 28, 2019, 08:03 am by PaulRB
+1 Karma for fixing the code tags. Auto Format?

JetPackCat

No, I just manually changed it by typing in code in place of quote

PaulRB

I meant please Auto Format your code, like I asked before.

JetPackCat

#6
May 29, 2019, 06:39 am Last Edit: May 29, 2019, 06:40 am by JetPackCat
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);
    }
  }
}



PaulRB

#7
May 29, 2019, 04:49 pm Last Edit: May 29, 2019, 04:50 pm by PaulRB
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).

JetPackCat

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.

PaulRB

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.

JetPackCat

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.

PaulRB

#11
May 30, 2019, 08:05 am Last Edit: May 30, 2019, 08:21 am by PaulRB
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??

JetPackCat

#12
May 30, 2019, 09:52 am Last Edit: May 30, 2019, 09:52 am by JetPackCat
Hope this clears things up. Basic graphic attached


PaulRB

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)


JetPackCat

#14
May 30, 2019, 09:02 pm Last Edit: May 31, 2019, 05:11 am by JetPackCat
Switches connected to Pins 2 and 3. LED on 13, and NeoPixel on 12, connected to 5v and Ground.


Go Up