How can I make working Relay?
All working fine
Except the relay
I am trying to code the relay will turn on after 10sec if pin 13 get high and It will turn off immediately if pin 13 is off
how can achieve this?
my code
#include <FastLED.h>
#define LED_PIN1 10
#define LED_PIN 11
#define HORN_PIN 9
#define FASTLED_INTERNAL
#define COLOR_ORDER RGB
#define LED_TYPE WS2812
#define NUM_LEDS3 9
#define NUM_LEDS 9
// buttons for commands
#define StopButton 3
#define ClignoButton 5
#define ClignoButtonR 4
#define PosButton 2
#define EmButton 6
#define PoliceButton 7
#define HornButton 13
//CRGB leds1[NUM_LEDS];
//CRGB leds[NUM_LEDS];
CRGBArray<NUM_LEDS> leds1;
CRGBArray<NUM_LEDS> leds;
CRGBArray<NUM_LEDS3> leds3;
//bottom row led strip arrays are leds1
//top row led strip arrays are leds
CRGBSet group1(leds1(0, 1));
CRGBSet group2(leds1(2, 3));
CRGBSet group3(leds1(4, 5));
CRGBSet group4(leds1(6, 7));
CRGBSet group5(leds1(8, 9));
CRGBSet group6(leds1(10, 10));
CRGBSet group15(leds1(10, 11));
CRGBSet group16(leds(8, 9));
CRGBSet group17(leds(6, 7));
CRGBSet group18(leds(4, 5));
CRGBSet group19(leds(2, 3));
CRGBSet group20(leds(0, 1));
CHSV color1(0, 255, 255); //red
CHSV color2(160, 255, 255); //blue
CHSV color3(0, 0, 0); //black
byte NCligno = 0;
byte NClignoR = 0;
bool Cligno = false;
bool ClignoR = false;
bool Stop = false;
bool Em = false;
bool Police = false;
bool Position = true;
bool Horn = false;
const byte BrightnessPos = 10; // %
const byte BrightnessCligno = 50;
const byte BrightnessStop = 100;
const byte BrightnessPolice = 100;
unsigned long ClignoDur = 20ul;
unsigned long ClignoDurR = 20ul;
unsigned long Chrono = 0;
unsigned long ChronoR = 0;
void ReadButtons () {
Cligno = !digitalRead(ClignoButton);
ClignoR = !digitalRead(ClignoButtonR);
Stop = !digitalRead(StopButton);
Position = !digitalRead(PosButton);
Em = !digitalRead(EmButton);
Police = !digitalRead(PoliceButton);
Horn = !digitalRead(HornButton);
delay(25);
}
void LightCligno (byte n) {
int orange = 255 * BrightnessCligno / 100;
for (int i = 0; i < n; i++) leds[i] = CRGB(orange, orange / 2.3, 0);
//for (int i = 144; n < 1; i--) leds[i] = CRGB(0, 0, 0);
}
void LightClignoR (byte n) {
int orange = 255 * BrightnessCligno / 100;
for (int i = 0; i < n; i++) leds1[i] = CRGB(orange, orange / 2.3, 0);
}
void LightAll (byte b) {
int red = 255 * b / 100;
for (int i = 0; i < NUM_LEDS; i++) leds1[i] = CRGB(red, 0, 0);
for (int i = 0; i < NUM_LEDS; i++) leds[i] = CRGB(red, 0, 0);
}
void LightEm (byte n) {
int orange = 255 * BrightnessCligno / 100;
for (int i = 0; i < n; i++) leds[i] = CRGB(orange, orange, 0);
for (int i = 0; i < n; i++) leds1[i] = CRGB(orange, orange, 0);
}
void LightPolice (byte b) {
int red = 255 * b / 100;
for (int i = 0; i < b; i++) leds1[i] = CRGB(0, 0, red);
delay(100);
for (int i = 0; i < b; i++) leds[i] = CRGB(red, 0, 0);
}
void setup() {
FastLED.addLeds<WS2812, LED_PIN1, RGB>(leds1, NUM_LEDS);
FastLED.addLeds<WS2812, LED_PIN, RGB>(leds, NUM_LEDS);
FastLED.addLeds<WS2812, 5, RGB>(leds1, NUM_LEDS); //strip1 or bottom row
FastLED.addLeds<WS2812, 6, RGB>(leds, NUM_LEDS); //strip2 or top row
//FastLED.addLeds<WS2812, 7, RGB>(leds3, NUM_LEDS3); //strip3 or computer lights
FastLED.setBrightness(255); //full brightness
pinMode (StopButton, INPUT_PULLUP);
pinMode (ClignoButton, INPUT_PULLUP);
pinMode (ClignoButtonR, INPUT_PULLUP);
pinMode (PosButton, INPUT_PULLUP);
pinMode (EmButton, INPUT_PULLUP);
pinMode (PoliceButton, INPUT_PULLUP);
pinMode (HornButton, OUTPUT);
Serial.begin(115200);
Chrono = millis();
ChronoR = millis();
}
void loop() {
ReadButtons ();
if (!Position & !ClignoR ) {
FastLED.clear();
} else LightAll (BrightnessPos);
if (Stop) LightAll (BrightnessStop);
if (Horn) {digitalWrite(HORN_PIN, HIGH);}
if (Cligno) {
FastLED.clear();
if (millis() - Chrono > ClignoDur) {
Chrono = millis();
NCligno = (NCligno + 1) % NUM_LEDS;
LightCligno (NCligno);
}
}
if (Cligno & Position or (Cligno & Position & Stop)) {
FastLED.clear();
if (millis() - Chrono > ClignoDur) {
Chrono = millis();
NCligno = (NCligno + 1) % NUM_LEDS;
LightCligno (NCligno);
}
}
if (ClignoR) {
FastLED.clear();
if (millis() - ChronoR > ClignoDurR) {
ChronoR = millis();
NClignoR = (NClignoR + 1) % NUM_LEDS;
LightClignoR (NClignoR);
}
}
if (Em) {
FastLED.clear();
if (millis() - Chrono > ClignoDur) {
Chrono = millis();
NCligno = (NCligno + 1) % NUM_LEDS;
LightCligno (NCligno);
}
if (millis() - ChronoR > ClignoDurR) {
ChronoR = millis();
NClignoR = (NClignoR + 1) % NUM_LEDS;
LightClignoR (NClignoR);
}
}
// if (Police) LightPolice (BrightnessStop);
if (Police) {
FastLED.clear();
for (int x = 0; x < 2; x++) { //repeat the flash 3 times
group1 = color1;
group20 = color1;
FastLED.show(100);
delay(50);
group1 = color3;
group20 = color3;
FastLED.show(100);
delay(50);
}
for (int x = 0; x < 3; x++) {
group2 = color2;
group19 = color2;
FastLED.show(100);
delay(40);
group2 = color3;
group19 = color3;
FastLED.show(100);
delay(40);
}
for (int x = 0; x < 3; x++) {
group3 = color1;
group18 = color2;
FastLED.show(100);
delay(30);
group3 = color3;
group18 = color3;
FastLED.show(100);
delay(30);
}
for (int x = 0; x < 3; x++) {
group4 = color2;
group17 = color1;
FastLED.show(100);
delay(20);
group4 = color3;
group17 = color3;
FastLED.show(100);
delay(50);
}
for (int x = 0; x < 3; x++) {
group5 = color2;
group16 = color1;
FastLED.show(100);
delay(10);
group5 = color3;
group16 = color3;
FastLED.show(100);
delay(10);
}
// if (millis() - Chrono > ClignoDur) {
// Chrono = millis();
// NCligno = (NCligno + 1) % NUM_LEDS;
// LightPolice (NCligno);
// }
// if (millis() - ChronoR > ClignoDurR) {
// ChronoR = millis();
// NClignoR = (NClignoR + 1) % NUM_LEDS;
// LightPolice (NClignoR);
// }
}
FastLED.show();
}