0% found this document useful (0 votes)
30 views

ES32 Arduino

This code controls the lighting of LED strips to indicate movement up and down a staircase. It uses two sensors to detect movement and triggers the appropriate lighting effect. The LEDs are dimmed on and off sequentially to simulate movement in the indicated direction. Timers are used to add delays and pauses between the up and down lighting states.

Uploaded by

steevens Garrido
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

ES32 Arduino

This code controls the lighting of LED strips to indicate movement up and down a staircase. It uses two sensors to detect movement and triggers the appropriate lighting effect. The LEDs are dimmed on and off sequentially to simulate movement in the indicated direction. Timers are used to add delays and pauses between the up and down lighting states.

Uploaded by

steevens Garrido
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

#include <FastLED.

h>

#define SensorUP A2

#define SensorDOWN A3

#define NUM_LEDS 61

#define DATA_PIN 8

#define BAUDRATE 4800

CRGB leds[NUM_LEDS];

int S2 = 0, S3 = 0;

unsigned long startMillisA, startMillisB, startMillisC, startMillisD;

unsigned long currentMillis;

int Flag = 0;

int delay_start_up = 500;

int padq = 50;

int delay_ms = 60;

int pausa = 4000;

int i = 0, j = 0, k = 0;

int x = 0, y = NUM_LEDS - 1;

enum state {WAIT, DIMM_UP_ON, DIMM_DOWN_ON, DELAY, DIMM_UP_OFF, DIMM_DOWN_OFF} state_up, state_down;

bool LED_STATE = true;

bool enable_up = false;

bool enable_down = false;

void sensorInicioEscalera()

S2 = digitalRead(SensorUP);

S3 = digitalRead(SensorDOWN);

if (S2 == 1 && state_up == WAIT)

state_up = DIMM_UP_ON;

Serial.println("state_up=DIMM_UP_ON");

S2 = 1;

if (S3 == 1 && state_down == WAIT)

state_down = DIMM_DOWN_ON;

Serial.println("state_down=DIMM_DOWN_ON");
S3 = 1;

Serial.print("A2:");

Serial.print(S2);

Serial.print(" --- A3:");

Serial.println(S3);

void Configuracion_Timer()

cli(); //stop interrupts for till we make the settings

TCCR1A = 0; // Reset entire TCCR1A to 0

TCCR1B = 0; // Reset entire TCCR1B to 0

TCCR1B |= B00000100; //Set CS12 to 1 so we get prescalar 256

TIMSK1 |= B00000010; //Set OCIE1A to 1 so we enable compare match A

OCR1A = 7812;//15625;//31250; //Finally we set compare register A to this value

sei(); //Enable back the interrupts

void setup()

Configuracion_Timer();

pinMode(LED_BUILTIN, OUTPUT);

pinMode(SensorUP, INPUT);

pinMode(SensorDOWN, INPUT);

Serial.begin(BAUDRATE);

FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);

FastLED.setBrightness(64);

startMillisD = currentMillis;

Serial.println("ES32 - Inicio");

startMillisA = millis();

void loop()

if (Flag == 0)

{
if (startMillisB - startMillisA > delay_start_up)

FastLED.clear();

FastLED.show();

S2=0;S3=0;

Flag = 1;

startMillisB = millis();

else

currentMillis = millis();

if (currentMillis - startMillisD >= padq)

startMillisD = currentMillis;

sensorInicioEscalera();

if (state_up == DIMM_UP_ON)

escalera_inicio_UP();

else if (state_down == DIMM_DOWN_ON)

escalera_inicio_DOWN();

ISR(TIMER1_COMPA_vect)

TCNT1 = 0; //First, set the timer back to 0 so it resets for next interrupt

LED_STATE = !LED_STATE; //Invert LED state

digitalWrite(LED_BUILTIN, LED_STATE); //Write new state to the LED on pin D5

void escalera_inicio_DOWN()

//WAIT,DIMM_UP_ON,DIMM_DOWN_ON, DELAY, DIMM_UP_OFF,DIMM_DOWN_OFF


if (state_down == DIMM_DOWN_ON)

pausa = 4000;

Serial.println("------------- Inicio DOWN -----------------");

for (i = 0; i < NUM_LEDS; i++)//S3

leds[(NUM_LEDS - 1) - i] = CHSV(255, 255, 255);

if (state_up == DIMM_UP_ON)

leds[i - j] = CHSV(255, 255, 255);

delay(delay_ms);

FastLED.show();

S2 = digitalRead(SensorUP);

if (S2 == 1 && state_up == WAIT)

state_up = DIMM_UP_ON;

j = i;

Serial.println("------------- Inicio UP -----------------");

pausa = pausa * 2;

state_down = DELAY;

if (state_up == DIMM_UP_ON)

state_up = DELAY;

Serial.println("------------- Pausa -----------------");

//delay(pausa);

while (state_down == DELAY)

EVERY_N_MILLISECONDS(1)

pausa--;

S2 = digitalRead(SensorUP);

S3 = digitalRead(SensorDOWN);

if (S2 == 1 || S3 == 1) {

pausa = 8000;

S2 = 0;

S3 = 0;
Serial.println("------------- Inicio Nuevo Pausa -----------------");

if (pausa == 0)

Serial.println("-------------Fin de Pausa -----------------");

state_down = DIMM_DOWN_OFF;

if (state_up == DELAY)

state_up = DIMM_UP_OFF;

Serial.println("------------- Apagado -----------------");

for (i = 0; i < NUM_LEDS; i++)//S2

leds[(NUM_LEDS - 1) - i] = CHSV(0, 0, 0);

if (state_up == DIMM_UP_OFF)

leds[i] = CHSV(0, 0, 0);

delay(delay_ms);

FastLED.show();

if (enable_up == false)

S2 = digitalRead(SensorUP);

if (enable_down == false)

S3 = digitalRead(SensorDOWN);

if (S2 == 1) {

S2 = 0;

enable_up = true;

if (S3 == 1) {

S3 = 0;

enable_down = true;

Serial.println("------------- Fin --------------------");

if (enable_up == true) {

state_up = DIMM_UP_ON;

enable_up = false;

else state_up = WAIT;


if (enable_down == true) {

state_down = DIMM_DOWN_ON;

enable_down = false;

else state_down = WAIT;

void escalera_inicio_UP()

if (state_up == DIMM_UP_ON)

pausa = 4000;

Serial.println("------------- Inicio UP -----------------");

for (i = 0; i < NUM_LEDS; i++)//S2

leds[i] = CHSV(255, 255, 255);

if (state_down == DIMM_DOWN_ON)

leds[(NUM_LEDS - 1) - i + j] = CHSV(255, 255, 255);

delay(delay_ms);

FastLED.show();

S3 = digitalRead(SensorDOWN);

if (S3 == 1 && state_down == WAIT)

state_down = DIMM_DOWN_ON;

j = i;

Serial.println("------------- Inicio DOWN -----------------");

pausa = pausa * 2;

state_up = DELAY;

if (state_down == DIMM_DOWN_ON)

state_down = DELAY;

Serial.println("------------- Pausa -----------------");

//delay(pausa);

while (state_up == DELAY)


{

EVERY_N_MILLISECONDS(1)

pausa--;

S2 = digitalRead(SensorUP);

S3 = digitalRead(SensorDOWN);

if (S2 == 1 || S3 == 1) {

pausa = 8000;

S2 = 0;

S3 = 0;

Serial.println("------------- Inicio Nuevo Pausa -----------------");

if (pausa == 0)

Serial.println("-------------Fin de Pausa -----------------");

state_up = DIMM_UP_OFF;

if (state_down == DELAY)

state_down = DIMM_DOWN_OFF;

Serial.println("------------- Apagado -----------------");

for (i = 0; i < NUM_LEDS; i++)//S2

leds[i] = CHSV(0, 0, 0);

if (state_down == DIMM_DOWN_OFF)

leds[(NUM_LEDS - 1) - i] = CHSV(0, 0, 0);

delay(delay_ms);

FastLED.show();

if (enable_up == false)

S2 = digitalRead(SensorUP);

if (enable_down == false)

S3 = digitalRead(SensorDOWN);

if (S2 == 1) {

S2 = 0;

enable_up = true;

if (S3 == 1) {
S3 = 0;

enable_down = true;

Serial.println("------------- Fin --------------------");

if (enable_up == true) {

state_up = DIMM_UP_ON;

enable_up = false;

else state_up = WAIT;

if (enable_down == true) {

state_down = DIMM_DOWN_ON;

enable_down = false;

else state_down = WAIT;

You might also like