Hi Guys,
I am using a nano to control a bunch of LED lights inside a model car and I would like some help to control them with an element of randomness but still behaving like a car. I can control the LEDs without issue and have the "headlights connected to PWM so can control the brightness.
I have this working so far:
- a ring of LEDs spinning to simulate the engine
- switches in the doors to control the interior lights which are WS2812 leds so I can configure the color of the interior when a door is open.
- headlights are PWM controlled led rings so have low and high beam. The state is randomly selected every few seconds.
The part I have trouble getting my head around is how to make the indicators occasionally turn on and flash a random number of times whilst still having a normal interval between flashes.
The indicators should have 4 states
- Off
- Left flash
- Right flash
- Both flash
I want it to look realistic so I don't just want the indicator to flash once and I don't want the indicator flash to affect any of the other lights so using delay is out...
I would really love some ideas how to do it please.
thanks for any help
Here is the code I currently have that works but no indicators LEDs yet:
#include <FastLED.h>
#define LED_PIN 2
#define NUM_LEDS 2
#define BRIGHTNESS 200
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 100
// Engine LEDs
#define REDLED_1 8
#define REDLED_2 7
#define REDLED_3 6
#define REDLED_4 5
#define REDLED_5 4
#define REDLED_6 9
#define Headlight_Left 10 //PWM
#define Headlight_Right 11 //PWM
// Indicator LEDs
#define Front_Left_Indicator A0
#define Rear_Left_Indicator1 A1
#define Rear_Left_Indicator2 A2
#define Front_Right_Indicator A3
#define Rear_Right_Indicator1 A4
#define Rear_Right_Indicator2 A5
// Spare LEDs
#define Spare1 13
#define Spare2 A6
//Input Switches
#define Door_switch 3
//WS2812B LEDs x2
#define Interior 2
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long Headlight_previousMillis = 0; // will store last time LED was updated
const long engine_RPM_interval = 20; // interval at which to spin red engine LEDs
const long Headlight_interval = 200; // interval at which to change Headlight state
byte pins[6] = {REDLED_6,REDLED_5,REDLED_4,REDLED_3,REDLED_2,REDLED_1};
int Door_State = 0;
int HeadLight_State = 0;
int Indicator = 0;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness(BRIGHTNESS);
pinMode(Door_switch, INPUT);
pinMode(REDLED_1, OUTPUT);
pinMode(REDLED_2, OUTPUT);
pinMode(REDLED_3, OUTPUT);
pinMode(REDLED_4, OUTPUT);
pinMode(REDLED_5, OUTPUT);
pinMode(REDLED_6, OUTPUT);
pinMode(Headlight_Left, OUTPUT);
pinMode(Headlight_Right, OUTPUT);
pinMode(Front_Left_Indicator, OUTPUT);
pinMode(Rear_Left_Indicator1, OUTPUT);
pinMode(Rear_Left_Indicator2, OUTPUT);
pinMode(Front_Right_Indicator, OUTPUT);
pinMode(Rear_Right_Indicator1, OUTPUT);
pinMode(Rear_Right_Indicator2, OUTPUT);
//pinMode(Spare1, OUTPUT);
//pinMode(Spare2, OUTPUT);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
unsigned long currentMillis = millis();
// Keep engine LEDs spinning
if (currentMillis - previousMillis >= engine_RPM_interval){
previousMillis = currentMillis;
Cycle_Engine_Leds();
}
// Choose Headlight State
if (currentMillis - Headlight_previousMillis >= Headlight_interval){
Headlight_previousMillis = currentMillis;
HeadLight_State = random(1,4);
Serial.println(HeadLight_State);
}
HeadLights();
// Read input switch to see if a car door is open
Door_State = digitalRead(Door_switch);
InteriorLights();
// every random interval 1-10minutes
// random choose left, right or both indicator to blink
// random choose number of times to blink
// initiate blink
// every random interval 1-10minutes
// random choose
// random choose number of times to blink
// initiate blink
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Indicators(){
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Cycle_Engine_Leds(){
static byte led = 0;
digitalWrite(pins[led], LOW);
if (++led >= sizeof(pins)/sizeof(pins[0])) led = 0;
digitalWrite(pins[led], HIGH);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void InteriorLights(){
if (Door_State == LOW){
for( int i = 0; i < NUM_LEDS; ++i){
leds[i] = CRGB::Blue;
FastLED.show();
}
}
if (Door_State == HIGH){
for( int i = 0; i < NUM_LEDS; ++i){
leds[i] = CRGB::Black;
FastLED.show();
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void HeadLights(){
if (HeadLight_State == 3){
analogWrite(Headlight_Left, 255); // turn the LED on (HIGH is the voltage level)
analogWrite(Headlight_Right, 255); // turn the LED on (HIGH is the voltage level)
}
if (HeadLight_State == 2){
analogWrite(Headlight_Left, 50); // turn the LED on (HIGH is the voltage level)
analogWrite(Headlight_Right,50); // turn the LED on (HIGH is the voltage level)
}
if (HeadLight_State == 1){
analogWrite(Headlight_Left, 0); // turn the LED on (HIGH is the voltage level)
analogWrite(Headlight_Right,0); // turn the LED on (HIGH is the voltage level)
}
}