Go Down

Topic: Problem controlling LED fades without delays (Read 9034 times) previous topic - next topic


Hi everyone, I'm quite new to Arduino and brand new to the forum. Any assistance would be greatly appreciated.

I am developing code for an installation. If a viewer sits in a seat for longer than 3000ms a button in the seat triggers a fluorescent light to switch off (via a powertail). If the viewer stands up and releases the button, the Fluoro turns back on.
I have this aspect covered, coded and working.
Where I am having trouble is in fading in and out various LEDs once the Fluoro has switched off. I do not want to use delay because then the reset (Fluoro back on once button released and all LEDs off) will be delayed. But I want to be able to control the LED fades.
I am hoping that there might be something in 'edge detection'  but I want the increments and decrements to step faster than every 3 seconds.

Here is my code thus far:
Code: [Select]

based on

Click and Press+Hold Test Sketch
By Jeff Saltzman

int FLUORO = 13; // output pin for fluoro (via powerswitch tail)
int BUTTON = 7; // digital input pin for the seat button
int led1 = 3; // output pin for Led1
int led2 = 5; // output pin for Led2
int led3 = 6; // output pin for Led3
int led4 = 9; // output pin for Led4
int led5 = 10; // output pin for Led5

int buttonVal = 0; // value read from the button
int buttonLast = 0; // buffered value of the button's previous state
long btnDnTime; // time the button was pressed down
long btnUpTime; // time the button was released

const int debounce = 20; //ms debounce period to prevent flickering when pressing or releasing the button
const int holdTime = 3000; // how long to wait for a press and hold event

//FLOURO variables

boolean fluoroVal = true; // state of FLUORO

//LED fade variables


void setup(){

  // Set button input pin

  // Set Fluoro ouput pin
  pinMode(FLUORO, OUTPUT);
  digitalWrite (FLUORO, fluoroVal);

  // Set Led output pins
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);

void loop() {

  // Read the state of the button
  buttonVal = digitalRead(BUTTON);

  // Test for button pressed and store the down time
  if (buttonVal == HIGH && buttonLast == LOW && (millis() - btnUpTime) > long(debounce))
    btnDnTime = millis();

  // Test for button release and store the up time
  if (buttonVal == LOW && buttonLast == HIGH)


  // Test for button held down for longer than the hold time
  if (buttonVal == HIGH && (millis() - btnDnTime) > long(holdTime))

    btnDnTime = millis();

  buttonLast = buttonVal;


// Events to trigger by click and press+hold

void event1()

  digitalWrite(FLUORO, HIGH);
  analogWrite(led1, 0);
  analogWrite(led2, 0);
  analogWrite(led3, 0);
  analogWrite(led4, 0);
  analogWrite(led5, 0);

void event2()

  digitalWrite(FLUORO, LOW); //turn off fluoro light

  // LED sequence


Thanks for reading!


have you ever considered using interrupts?

If you use interrupts attatched to the light on and light off functions (event1() and event2(), I believe), then you will be able to use delay() to control the fades.

Go Up