Tripwire - dim LED from on to off

Hello,

I have a simple project that uses a laser and a photocell to act as a trip wire. One LED is on all the time (RING variable) except for when the laser is tripped. Right now I have it so when the laser is tripped the RING LED turns off (as desired) and the STRIP variable LED turns on but stays on only when the laser is tripped.

My ultimate goal would be to have the STRIP LED turn on and then dim to OFF when the laser is tripped and have RING off during this dimming.

My code is below. I have searched and searched and have successfully gotten it to dim while the laser stays tripped, but I can't quite wrap my head around how to accomplish the end goal.

Many thanks in advance!

/*
Laser trip light changer
*/

int LASER = 13; //set up variable LASER as the pin to which the laser connects
int RING = 12; //set up the variable RING as the pin to which the led ring connects
int STRIP = 11; //set up the variable STRIP as the pin to which the led strip connects

int BRIGHTNESS = 255; //set the initial brightness of 255 (full)

void setup() { //function that only runs once initially

  pinMode(LASER, OUTPUT); //set up LASER pin as OUTPUT
  pinMode(RING, OUTPUT); //set up RING pin as OUTPUT
  pinMode(STRIP, OUTPUT); //set up STRIP pin as OUTPUT
}


void loop() { //function that continues to loop
  
    digitalWrite(LASER, HIGH); //turn the laser on
    
  
    if(analogRead(0) > 700){ //if analog 0 has value over 700 from photocell
  
    digitalWrite(RING, HIGH); //turn RING on 
    digitalWrite(STRIP, LOW); //turn STRIP off	
    }
	
    else{ //if analog 0 has value under 700 from photocell
      digitalWrite(RING, LOW); //turn RING off
      digitalWrite(STRIP,HIGH); //turn STRIP on      
    }
}

Not exactly sure what you want here, but if you are after the LED fading from ON to OFF, have a look at this Fade tutorial

Yours,
TonyWilk

? where is the code to setup the analog pin and the variable brightness is not used? is there more code

Where you set STRIP to high, replace that line with a loop executing the fade TonyWilk pointed you to. (and yeah ... what rogertee said).

JB

Thanks everyone for taking a look at this! Your suggestions make sense and I have implemented them below. They are definitely a step in the right direction. I think i had an issue with clearly stating what I was hoping to achieve here. My fault! Below is my new code:

/*
Laser trip light changer
*/

int LASER = 13; //set up variable LASER as the pin to which the laser connects
int RING = 12; //set up the variable RING as the pin to which the led ring connects
int STRIP = 11; //set up the variable STRIP as the pin to which the led strip connects
int PHOTO = 0; //set up the variable PHOTO as the pint to which the photocell connects

int BRIGHTNESS = 255; //set the initial brightness of 255 (full)
int FADE = 5; //set the fade value

void setup() { //function that only runs once initially
  
  pinMode(LASER,OUTPUT); //set up LASER pin as OUTPUT
  pinMode(RING,OUTPUT); //set up RING pin as OUTPUT
  pinMode(STRIP,OUTPUT); //set up STRIP pin as OUTPUT
}

void loop() { //function that continues to loop
    digitalWrite(LASER,HIGH); //turn the laser on
    
    if(analogRead(PHOTO) > 700){ //if analog 0 has value over 700 from photocell

    digitalWrite(RING,HIGH); //turn RING on 
    digitalWrite(STRIP,LOW); //turn STRIP off 
    }
    else{
      digitalWrite(RING, LOW); //turn RING off
      analogWrite(STRIP, BRIGHTNESS); //turn STRIP on to level 255

      BRIGHTNESS = BRIGHTNESS - FADE; //lower the brightness determined by FADE
      delay(30); //delay 30 ms before looping again
      }
}

Here is a video of how it works:

My end goal is to have the full fade run if the laser is not tripped for that duration. So if the loop statement goes into else how can I have it run the full fade cycle?

I also tried a For Loop (https://www.arduino.cc/en/Tutorial/Loop) but ended up with the same results as shown in the video.

Good so far !

relic217:
My end goal is to have the full fade run if the laser is not tripped for that duration. So if the loop statement goes into else how can I have it run the full fade cycle?

I think this may be what you are after, but it's the way of approaching the problem which might be of more general interest....

So what we want it to do is:

If the beam is not activated, the RING is ON and the STRIP is OFF
If the beam goes from NOT activated to activated, we want to immediately:

  • Turn the RING off*
  • Turn the STRIP ON*
    When we've been activated and the STRIP is ON
  • fade the strip down to OFF over a second or two.*
  • While it is doing this we don't want to be messed about by the beam tripping or not.*
    When the fade has faded out, we want to go and look at the beam again

So, turning this into code: we need something to remember if we're activated or not.

  if( beamIsActivated == 0 ){
    // beam is currently not activated... 
      digitalWrite(RING,HIGH); //turn RING on 
      BRIGHTNESS= 0;          //turn STRIP off 

    // check state of the beam
    if(analogRead(PHOTO) < 700){ //if analog 0 has value under 700 from photocell
      // beam has just been activated !!!
      beamIsActivated= 1; 
      digitalWrite(RING, LOW); //turn RING off
      BRIGHTNESS= 255;    // turn the STRIP ON
    }
  }else{
    // beam has been activated
    // - we need to fade the STRIP

    BRIGHTNESS = BRIGHTNESS - FADE; //lower the brightness determined by FADE
    if( BRIGHTNESS <= 0 ){    // have we faded out yet ?
      BRIGHTNESS= 0;
      beamIsActivated= 0;   // we aren't activated any more
    }

  }

  analogWrite(STRIP, BRIGHTNESS); // write out whatever brightness we've set
  delay(30); //delay 30 ms before looping again

I just typed that in, didn't compile it... but something like that should work :slight_smile:

Yours,
TonyWilk

That worked out perfectly, TonyWilk! Creating the beamIsActivated variable was the key that I was missing. Using that to trigger the fade for the STRIP and then using it again to break out of the else statement gave just the effect I was looking for. Thank you very much for your time!

Video of the final effect: - YouTube