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.
#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);
}
}
}
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.
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.
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.
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.
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.
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.
DrAzzy:
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.
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.