21-05-2024, 11:23
Witam,
chciałbym uzyskać taki efekt animacji diód. Video -> Gdrive
A kod mam taki:
chciałbym uzyskać taki efekt animacji diód. Video -> Gdrive
A kod mam taki:
Kod:
#include "Arduino.h"
#include <FastLED.h>
#define LED_PIN 11 // Połączenie sygnału taśmy LED
#define LED_PIN2 12
#define DRLSignal 3 // Połączenie sygnału przełącznika DRL
#define LeftSignal 4 // Połączenie sygnału lewego migacza
#define RightSignal 5 // Połączenie sygnału prawego migacza
#define NUM_LEDS 15
#define NUM_LEDS2 15 // Całkowita liczba diod LED w dwóch taśmach LED (np. Użyj tutaj 40 dla dwóch 20 taśm LED)
#define BlinkerLEDs 15
#define NUM_LEDS3 30
#define NUM_LEDS4 30 // Całkowita liczba diod LED w dwóch taśmach LED (np. Użyj tutaj 40 dla dwóch 20 taśm LED)
// Liczba diod LED dla lewego/prawego migacza
//#define BlinkerLEDs NUM_LEDS/2 // Liczba diod LED dla lewego/prawego migacza
int BlinkerSpeed = 50; // Kierunkowskaz działa prędkość LED. Dostosuj to do prędkości kierunkowskazów pojazdu.
int BlinkerOffDelay = 500; // Włącz czas wyłączenia sygnału. Dostosuj to do prędkości kierunkowskazów pojazdu.
int StartupSpeed = 20;
int DRLDetect = 0;
int DRLColour = 3; // Zmień kolor diody LED tutaj, 1-lodowy błękit, 2-niebieski, 3-biały, 4-pomarańczowy
CRGB leds[NUM_LEDS];
CRGB leds2[NUM_LEDS2];
CRGB leds3[NUM_LEDS];
CRGB leds4[NUM_LEDS2];
int r, r_dim, g, g_dim, b, b_dim;
void setup()
{
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.addLeds<WS2812, LED_PIN2, GRB>(leds2, NUM_LEDS2);
pinMode(DRLSignal, INPUT_PULLUP);
pinMode(LeftSignal, INPUT_PULLUP);
pinMode(RightSignal, INPUT_PULLUP);
leds[NUM_LEDS] = CRGB(0, 0, 0);
leds2[NUM_LEDS2] = CRGB(0, 0, 0);
leds3[NUM_LEDS3] = CRGB(0, 0, 0);
leds4[NUM_LEDS4] = CRGB(0, 0, 0);
FastLED.show();
}
void loop()
{
if (DRLDetect == 0)
{
if (digitalRead(DRLSignal) == 1)
{
DRLDetect = 1;
}
}
if (DRLDetect == 2)
{
if (digitalRead(DRLSignal) == 0)
{
DRLDetect = 3;
}
}
if (DRLDetect == 1)
{
DRL_ON();
DRLDetect = 2;
}
if (DRLDetect == 3)
{
DRL_OFF();
DRLDetect = 0;
}
if (digitalRead(DRLSignal) == 0)
{
if ((digitalRead(LeftSignal) == 1) && (digitalRead(RightSignal) == 0)) // Lewy migacz
{
RightOff();
LeftBlinker();
LeftDim();
delay (BlinkerOffDelay);
}
if ((digitalRead(RightSignal) == 1) && (digitalRead(LeftSignal) == 0)) // Prawy migacz
{
LeftOff();
RightBlinker();
RightDim();
delay (BlinkerOffDelay);
}
if ((digitalRead(LeftSignal) == 1) && (digitalRead(RightSignal) == 1)) // Podwójny migacz/zagrożenie
{
MiddleOff();
DualBlinker();
LeftDim();
RightDim();
delay (BlinkerOffDelay);
}
}
if (DRLDetect == 2)
{
if ((digitalRead(LeftSignal) == 0) && (digitalRead(RightSignal) == 0)) // Światło parkowe
{
ParkFull();
}
if ((digitalRead(LeftSignal) == 1) && (digitalRead(RightSignal) == 0)) // Lewy migacz
{
LeftDim();
RightLit();
LeftBlinker();
LeftDim();
delay (BlinkerOffDelay);
}
if ((digitalRead(RightSignal) == 1) && (digitalRead(LeftSignal) == 0)) // Prawy migacz
{
RightDim();
LeftLit();
RightBlinker();
RightDim();
delay (BlinkerOffDelay);
}
if ((digitalRead(LeftSignal) == 1) && (digitalRead(RightSignal) == 1)) // Podwójny migacz/zagrożenie
{
// LeftDim();
// RightDim();
// ParkMiddle();
DualBlinker();
LeftDim();
RightDim();
delay (BlinkerOffDelay);
}
}
switch (DRLColour)
{
case 1:
g = 255;
g_dim = 30;
b = 255;
b_dim = 30;
break;
case 2:
b = 255;
b_dim = 30;
break;
case 3:
r = 255;
r_dim = 30;
g = 255;
g_dim = 30;
b = 255;
b_dim = 30;
break;
case 4:
r = 255;
r_dim = 30;
g = 165;
g_dim = 20;
b = 0;
b_dim = 0;
break;
}
}
void DRL_ON()
{
for (int j = ((NUM_LEDS3 / 2) - 1); j >= 0; j--)
{
leds3[j] = CRGB(r_dim, g_dim, b_dim);
leds3[j + 1] = CRGB(0, 0, 0);
leds3[(NUM_LEDS3 / 2 - 1) + ((NUM_LEDS3 / 2) - j)] = CRGB(r_dim, g_dim, b_dim);
leds3[(NUM_LEDS3 / 2 - 1) + ((NUM_LEDS3 / 2) - j) - 1] = CRGB(0, 0, 0);
leds4[j] = CRGB(r_dim, g_dim, b_dim);
leds4[j + 1] = CRGB(0, 0, 0);
leds4[(NUM_LEDS4 / 2 - 1) + ((NUM_LEDS4 / 2) - j)] = CRGB(r_dim, g_dim, b_dim);
leds4[(NUM_LEDS4 / 2 - 1) + ((NUM_LEDS4 / 2) - j) - 1] = CRGB(0, 0, 0);
FastLED.show();
delay (StartupSpeed);
}
for (int i = 0; i < (NUM_LEDS3 / 2); i++)
{
leds3[i] = CRGB(r_dim, g_dim, b_dim);
leds3[(NUM_LEDS3 - 1) - i] = CRGB(r_dim, g_dim, b_dim);
leds4[i] = CRGB(r_dim, g_dim, b_dim);
leds4[(NUM_LEDS4 - 1) - i] = CRGB(r_dim, g_dim, b_dim);
FastLED.show();
delay (StartupSpeed);
}
for (int j = ((NUM_LEDS3 / 2) - 1); j >= 0; j--)
{
leds3[j] = CRGB(r, g, b);
leds3[(NUM_LEDS / 2 - 1) + ((NUM_LEDS3 / 2) - j)] = CRGB(r, g, b);
leds4[j] = CRGB(r, g, b);
leds4[(NUM_LEDS / 2 - 1) + ((NUM_LEDS4 / 2) - j)] = CRGB(r, g, b);
FastLED.show();
delay (StartupSpeed);
}
}
void DRL_OFF()
{
for (int j = 0; j <= ((NUM_LEDS3 / 2) - 1); j++)
{
leds3[j] = CRGB(r, g, b);
leds3[j - 1] = CRGB(r_dim, g_dim, b_dim);
leds3[(NUM_LEDS3 - 1) - j] = CRGB(r, g, b);
leds3[(NUM_LEDS3) - j] = CRGB(r_dim, g_dim, b_dim);
leds4[j] = CRGB(r, g, b);
leds4[j - 1] = CRGB(r_dim, g_dim, b_dim);
leds4[(NUM_LEDS4 - 1) - j] = CRGB(r, g, b);
leds4[(NUM_LEDS4) - j] = CRGB(r_dim, g_dim, b_dim);
FastLED.show();
delay (StartupSpeed);
}
leds3[((NUM_LEDS3 / 2) - 1)] = CRGB(r_dim, g_dim, b_dim);
leds3[((NUM_LEDS3 / 2) - 1) + 1] = CRGB(r_dim, g_dim, b_dim);
leds4[((NUM_LEDS4 / 2) - 1)] = CRGB(r_dim, g_dim, b_dim);
leds4[((NUM_LEDS4 / 2) - 1) + 1] = CRGB(r_dim, g_dim, b_dim);
FastLED.show();
for (int j = ((NUM_LEDS3 / 2) - 1); j >= 0; j--)
{
leds3[j] = CRGB(r_dim, g_dim, b_dim);
leds3[j + 1] = CRGB(0, 0, 0);
leds3[(NUM_LEDS3 / 2 - 1) + ((NUM_LEDS3 / 2) - j)] = CRGB(r_dim, g_dim, b_dim);
leds3[(NUM_LEDS3 / 2 - 1) + ((NUM_LEDS3 / 2) - j) - 1] = CRGB(0, 0, 0);
leds4[j] = CRGB(r_dim, g_dim, b_dim);
leds4[j + 1] = CRGB(0, 0, 0);
leds4[(NUM_LEDS4 / 2 - 1) + ((NUM_LEDS4 / 2) - j)] = CRGB(r_dim, g_dim, b_dim);
leds4[(NUM_LEDS4 / 2 - 1) + ((NUM_LEDS4 / 2) - j) - 1] = CRGB(0, 0, 0);
FastLED.show();
delay (StartupSpeed);
}
for (int j = 0; j <= ((NUM_LEDS3 / 2) - 1); j++)
{
leds3[j] = CRGB(r_dim, g_dim, b_dim);
leds3[j - 1] = CRGB(0, 0, 0);
leds3[(NUM_LEDS3 - 1) - j] = CRGB(r_dim, g_dim, b_dim);
leds3[(NUM_LEDS3) - j] = CRGB(0, 0, 0);
leds4[j] = CRGB(r_dim, g_dim, b_dim);
leds4[j - 1] = CRGB(0, 0, 0);
leds4[(NUM_LEDS4 - 1) - j] = CRGB(r_dim, g_dim, b_dim);
leds4[(NUM_LEDS4) - j] = CRGB(0, 0, 0);
FastLED.show();
delay (StartupSpeed);
}
leds3[(NUM_LEDS3 / 2) - 1] = CRGB(0, 0, 0);
leds3[NUM_LEDS3 / 2] = CRGB(0, 0, 0);
leds4[(NUM_LEDS4 / 2) - 1] = CRGB(0, 0, 0);
leds4[NUM_LEDS4 / 2] = CRGB(0, 0, 0);
FastLED.show();
}
void AllOff()
{
for (int i = 0; i < NUM_LEDS; i++)
{
leds[i] = CRGB(0, 0, 0);
}
FastLED.show();
}
void LeftOff()
{
for (int i = 0; i < (NUM_LEDS - BlinkerLEDs); i++)
{
leds[i] = CRGB(0, 0, 0);
}
FastLED.show();
}
void RightOff()
{
for (int i = BlinkerLEDs; i < NUM_LEDS; i++)
{
leds[i] = CRGB(0, 0, 0);
}
FastLED.show();
}
void MiddleOff()
{
for (int i = BlinkerLEDs; i < (NUM_LEDS - BlinkerLEDs); i++)
{
leds[i] = CRGB(0, 0, 0);
}
FastLED.show();
}
void ParkFull()
{
for (int i = 0; i < NUM_LEDS; i++)
{
leds[i] = CRGB(r, g, b);
}
FastLED.show();
}
void ParkMiddle()
{
for (int i = BlinkerLEDs; i < (NUM_LEDS - BlinkerLEDs); i++)
{
leds[i] = CRGB(60, 0, 0);
}
FastLED.show();
}
void LeftBlinker()
{
for (int i = (NUM_LEDS2 - BlinkerLEDs); i < NUM_LEDS2; i++)
{
leds2[i] = CRGB(255, 90, 0); //RED, GREEN, BLUE
FastLED.show();
delay (BlinkerSpeed);
}
}
void LeftDim()
{
for (int i = (NUM_LEDS2 - BlinkerLEDs); i < NUM_LEDS2; i++)
{
leds2[i] = CRGB(0, 0, 0);
}
FastLED.show();
}
void LeftLit()
{
for (int i = BlinkerLEDs; i < NUM_LEDS2; i++)
{
leds2[i] = CRGB(r, g, b);
}
FastLED.show();
}
void LeftFull()
{
for (int i = BlinkerLEDs; i < NUM_LEDS2; i++)
{
leds2[i] = CRGB(r, g, b);
}
FastLED.show();
}
void RightBlinker()
{
for (int i = (NUM_LEDS - BlinkerLEDs); i < NUM_LEDS; i++)
{
leds[i] = CRGB(255, 90, 0);
FastLED.show();
delay (BlinkerSpeed);
}
}
void RightDim()
{
for (int i = (NUM_LEDS - BlinkerLEDs); i < NUM_LEDS; i++)
{
leds[i] = CRGB(0, 0, 0);
}
FastLED.show();
}
void RightLit()
{
for (int i = BlinkerLEDs; i < NUM_LEDS; i++)
{
leds[i] = CRGB(r, g, b);
}
FastLED.show();
}
void RightFull()
{
for (int i = BlinkerLEDs; i < NUM_LEDS; i++)
{
leds[i] = CRGB(r, g, b);
}
FastLED.show();
}
void DualBlinker()
{
for (int i = (BlinkerLEDs - 1); i >= 0; i--)
{
leds[i] = CRGB(255, 90, 0);
leds[NUM_LEDS - 1 - i] = CRGB(255, 90, 0);
FastLED.show();
delay (BlinkerSpeed);
}
}