How can I make working Relay?

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();
}

look at the BlinkWithoutDelay example in the IDE. It will show you how. Also check out the StateChangeDetection example since you want to detect the transition of pin 13, not its state.

Basically, when pin 13 transitions from LOW to HIGH, you capture the time that event happens. Then, every time though loop(), you determine the elapsed time and it greater than 10 seconds, turn on the relay. If you detect the transition from HIGH to LOW, you turn off the relay. You will need to keep track of which state you are in (waiting for start, timing, off)

Write some code. Give it a try. Report back if you get stuck.

Why is HornButton an output?

blh64:
look at the BlinkWithoutDelay example in the IDE. It will show you how. Also check out the StateChangeDetection example since you want to detect the transition of pin 13, not its state.

Basically, when pin 13 transitions from LOW to HIGH, you capture the time that event happens. Then, every time though loop(), you determine the elapsed time and it greater than 10 seconds, turn on the relay. If you detect the transition from HIGH to LOW, you turn off the relay. You will need to keep track of which state you are in (waiting for start, timing, off)

Write some code. Give it a try. Report back if you get stuck.

I have try many way and i am not expert in code If you can modify code for me then I will be great.

evanmars:
Why is HornButton an output?

Actually i want to convert the Self Start switch to Horn switch.

My target is when Bike is off Self Start switch will work as Self Start, If Bike is on ( mean started) then is switch will convert as Horn switch.

All others working fine. Only I can't make it work the relay as I mention

alaminxp:
I have try many way and i am not expert in code If you can modify code for me then I will be great.

That is called hiring a programmer. That is not what this forum is about.

The best thing is to write a simple sketch that performs the function you want in isolation from the rest . Test it and incorporate it when it works.

If you didn’t write the original code and don’t understand it , then sort that issue out first . Copying code is ok, but if you can’t understand how it works and can’t then modify it then you will always have a problem maintaining it or altering it .
It’s often better to take inspiration from others, but then write and comment your own code IMO .