"If" statement and LEDs not working correctly

Thanks for the add...My 1st post here

I am using an Ardunio Uno and FastLED library for my 1st project.

I have the LED strips (WS2812B) working like I like but I then wanted to use my UNO with two inputs to drive two outputs.

I cut'n pasted my working code into the IF/ELSE statement and it doesn't work as it did outside the IF statements.

It basically is acting like may INPUT value is HIGH even when I physically remove the INPUT wire from the UNO

Any thoughts... Here is my code. The LED strips are just blinking and scrolling in some pattern. My goal is to have a structure where I can/add the code and have the IF/ELSE function.

#include <FastLED.h>

#define LED_PIN 5
//#define CLOCK_PIN 4
#define NUM_LEDS 7 //Edit this to equal the number of LEDs connected to each code for the light system
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define BRIGHTNESS 64 //Default unless overridden in the line of code. This isn't used in my current version
int LeftSWITCH = 8; //Pin number wire is connected to
int LeftLED = 5; //Pin number wire is connected to
//int RightSWITCH = 13; //Pin number wire is connected to
//int RightLED = 12; //Pin number wire is connected to

CRGB leds[NUM_LEDS];

void setup() {
// put your setup code here, to run once:
delay(1000);
LEDS.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
// LEDS.addLeds<LED_TYPE, LED_PIN, CLOCK_PIN COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);

pinMode(LeftSWITCH, INPUT); // 5v + from 12v/5v transformer
//pinMode(RightSWITCH, INPUT); // 5v + from 12v/5v transformer
pinMode(LeftLED, OUTPUT); // Data out to strip
//pinMode(RightLED, OUTPUT); // Data out to strip
}

void loop() {
// put your main code here, to run repeatedly:

if(digitalRead(LeftSWITCH)== HIGH) {
//digitalWrite(LeftLED, HIGH);
for(int i=0;i<NUM_LEDS;i++){
leds*.setRGB(255,0,0);*

  • FastLED.setBrightness(150);*
  • FastLED.show();*
  • delay(90); //Lower number is faster blinking*
  • FastLED.clear();*
    }
  • }*
    else{
    // digitalWrite(LeftLED, LOW);
  • for(int i=0;i<NUM_LEDS;i++){*
    _ leds*.setRGB(0,0,0);_
    _
    FastLED.show();_
    _
    }_
    _
    }_
    _
    }*_

(deleted)

I am not using one.

I am adding or removing 5v at pin #8

My thought was, if 5v is at pin#8 to the true code

If 5V is on pin8 it will be true. If nothing is on pin8 it will be floating about and may register anything at all.

Get a pull-down resistor.

Steve

Thanks Steve.. I'll google that and add one

I just noticed my earlier reply was cut off.. Not sure if it changes things

I am not using one.

I am adding or removing 5v at pin #8 (physically removing the 5v wire)

My thought was, if 5v is at pin#8 to the true code, if the 5v is removed, to the else code.

I will have a manual switch which will add or remove the 5v to pin#8.

My understanding is HIGH is 5v, LOW is 0v

Could I add an "else if" after the above "if" to do something else if pin #8 is LOW, then do "this"

FYI

.

Awesome guys.. I'll add the pull-down "R4" to my circuit and report back..

Easier than adding an external resistor is to use the INPUT_PULLUP mode of pinMode() to activate the built in pullup resistor and wiring the switch to take the pin to GND when it is closed. The downside is that you have to change the program logic to use match the hardware setup but it can make for a neater circuit with no extra components involved.

Thanks.. The "switch" is coming from an external component and will be delivering 5v to my Arduino circuit to act as a switch.

Does the external component have a common GND with the Arduino ?

The external component is only connected to the Arduino by the 5v wire.

The arduino is connected to the laptop USB (ground from laptop)
The "switch" is powered through the house voltage dropped down to 5v DC (ground from house)

Turns out I needed to ground them with the same ground. I tapped of the house ground and connected it to the GRD pin near the 5v out pin.

Looks like everything is working now..

Time to add more code and see if it I can keep it working

Cheers guys

I tapped of the house ground and connected it to the GRD pin near the 5v out pin.

I don't like the sound of that so be careful.