Pages: [1]   Go Down
Author Topic: Problem controlling LED fades without delays  (Read 7470 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 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!

In Front Of Computer
Offline Offline
Karma: 0
Posts: 47
Resistance is futile, but only for less than ten ohms.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

The Three Laws of Thermodynamics:
1. You can never get ahead, you can only break even.
2. You will only break even at absolute zero.
3. You

Offline Offline
Sr. Member
Karma: 5
Posts: 456
With every answer comes more questions.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

More interrupt info -

Pages: [1]   Go Up
Jump to: