Pages: [1] 2 3   Go Down
Author Topic: Fading three LEDs consecutively over three days.  (Read 2258 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Help! Finishing a valentines day project for my girl.

I am working with the following code to fade three LEDs over the course of three consecutive days. To clarify, LED1, LED2 and LED3 will be lit at full on startup.  LED1 will begin to fade on startup from full to 0 over 24 hours, while LED2 and LED3 remain on. Once LED1 hits 0, LED2 will begin this process in a similar way, followed by LED3. Once LED1, LED2 and LED3 are all faded out, they will remain black until a system reset (something I hope to do with a flick of an RFID tag).

I am using pins 5, 7 and 9 for LED1, LED2 and LED3 respectively.

I have this code:
Code:
for (int brightnessa=255; brightnessa <= 255; brightnessa--){
      analogWrite(5, brightnessa);
      delay(200);
  }

Which is dimming my first LED to black, but even though it's contained within void setup(), it goes back to the full brightness a moment later and repeats this process.

Can anyone help guide this newbie to a functioning piece of code that accomplishes that I wrote out above? Appreciated!  smiley-lol
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Show the whole code please.  Just that little snippet tells us nothing.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It's that "little snippet" that handles the fade alone. I hesitate to post all the code, because 99% of it involves the RFID. But since you asked:

Code:
int RFIDResetPin = 13;

void(* resetFunc) (void) = 0; //declare reset function @ address 0

//Register your RFID tags here
char tag1[14] = "046F3AB283603";

int brightnessa;    // how bright the LED is
int brightnessb = 1440;
int brightnessc = 1440;
int brightness = 1440;
int fadeAmount = 5;    // how many points to fade the LED by

void setup(){
  Serial.begin(9600);

  pinMode(RFIDResetPin, OUTPUT);
  digitalWrite(RFIDResetPin, HIGH);

  //ONLY NEEDED IF CONTROLING THESE PINS - EG. LEDs
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(9, OUTPUT);
 
    for (int brightnessa=255; brightnessa <= 255; brightnessa--){
      analogWrite(5, brightnessa);
      delay(200);
  }
}


void loop(){

  char tagString[13];
  int index = 0;
  boolean reading = false;

  while(Serial.available()){

    int readByte = Serial.read(); //read next available byte

    if(readByte == 2) reading = true; //begining of tag
    if(readByte == 3) reading = false; //end of tag

    if(reading && readByte != 2 && readByte != 10 && readByte != 13){
      //store the tag
      tagString[index] = readByte;
      index ++;
    }
  }

  checkTag(tagString); //Check if it is a match
  clearTag(tagString); //Clear the char of all value
  resetReader(); //eset the RFID reader
}

void checkTag(char tag[]){
///////////////////////////////////
//Check the read tag against known tags
///////////////////////////////////

  if(strlen(tag) == 0) return; //empty, no need to contunue

  if(compareTag(tag, tag1)){ // if matched tag1, do this
    lightLED(2);
    delay(100);
    resetFunc();

  }

  else{
    Serial.println(tag); //read out any unknown tag
  }

}

void lightLED(int pin){
///////////////////////////////////
//Turn on LED on pin "pin" for 250ms
///////////////////////////////////
  Serial.println(pin);

  digitalWrite(pin, HIGH);
  delay(100);
  digitalWrite(pin, LOW);
}

void resetReader(){
///////////////////////////////////
//Reset the RFID reader to read again.
///////////////////////////////////
  digitalWrite(RFIDResetPin, LOW);
  digitalWrite(RFIDResetPin, HIGH);
  delay(150);
}

void clearTag(char one[]){
///////////////////////////////////
//clear the char array by filling with null - ASCII 0
//Will think same tag has been read otherwise
///////////////////////////////////
  for(int i = 0; i < strlen(one); i++){
    one[i] = 0;
  }
}

boolean compareTag(char one[], char two[]){
///////////////////////////////////
//compare two value to see if same,
//strcmp not working 100% so we do this
///////////////////////////////////

  if(strlen(one) == 0) return false; //empty

  for(int i = 0; i < 12; i++){
    if(one[i] != two[i]) return false;
  }

  return true; //no mismatches
}
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
for (int brightnessa=255; brightnessa <= 255; brightnessa--){
      analogWrite(5, brightnessa);
      delay(200);
  }

Translated you're saying:
if brightnessa is less or equal to 255, decrease it by 1.  Do you see a problem with that?

What you want is:
if brightnessa is greater than 0, decrease by 1
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
for (int brightnessa=255; brightnessa <= 255; brightnessa--){
      analogWrite(5, brightnessa);
      delay(200);
  }

Translated you're saying:
if brightnessa is less or equal to 255, decrease it by 1.  Do you see a problem with that?

What you want is:
if brightnessa is greater than 0, decrease by 1

I agree your suggested code is better, but, as a learner, I'll ask- why would his code not work? Eventually it'll bottom out and the light should stay out, right?
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Because it will always be less than 255 from the first loop through, and it will never bottom out.  When it reaches 0, you continue to subtract from it, causing it to roll over back to 255.  If you're on a Windows machine, try it.  Open the Calculator, set it to Programmer (View -> Programmer).  Now type the letters 'FF' in.  Subtract 1 from it.  Now hold the Enter/Return key and watch it count down.  When it reaches 0, you'll notice it rolling over to FFFF FFFF FFFF FFFF.  You just rolled it over and you're back at FF (since you're only using the last two positions).

Whereas in my code, I say, while it's larger than 0, subtract 1.  When it reaches 0, it stops because 0 is not larger 0, it can't continue.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Translated you're saying:
if brightnessa is less or equal to 255, decrease it by 1.  Do you see a problem with that?

What you want is:
if brightnessa is greater than 0, decrease by 1

Got it!

This works for the first LED. Now I'm trying to right the simple instructions that will have this happen again on the second and third LED. I've written this, but it is not working. On boot, the second LED doesn't even light up.

Code:
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(9, OUTPUT);
 
    for (int brightnessa=100; brightnessa > 0; brightnessa--){
      analogWrite(5, brightnessa);
      delay(50);
    }
     
   if (brightnessa == 0)
{
for (int brightnessb=100; brightnessb > 0; brightnessb--){
      analogWrite(7, brightnessb);
      delay(50);
}
}

Furthermore, when the LED "bottoms out" at what should be zero, it's still faintly lit. I guess not the end of the world, but for the purposes of learning, is there a way to have it shut completely off?

And good news! The RFID "reset swipe" seems to be working perfectly right off the bat! One flick of my specified tag and LED1 lights back up and starts its dimming process over again. Sweet.
« Last Edit: February 07, 2012, 06:21:09 am by kkachurak » Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 48
Posts: 3407
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Though, when the LED "bottoms out" at what should be zero, it's still faintly lit. I guess not the end of the world, but for the purposes of learning, is there a way to have it shut completely off?

Code:
digitalwrite(5,LOW);
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



Got it!

This works for the first LED. Now I'm trying to right the simple instructions that will have this happen again on the second and third LED. I've written this, but it is not working. On boot, the second LED doesn't even light up.


How about during setup doing:

Code:
digitalwrite(5,HIGH);
digitalwrite(7,HIGH);
digitalwrite(9,HIGH);
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Pin 7 is not PWM.  Pin 6 is.

Try this:
Code:
int dayOne = 5;  // pin 5
int dayTwo = 6;  // pin 6
int dayThree = 9;   // pin 9
int completed = 0;

void setup() {
  pinMode(dayOne, OUTPUT);
  pinMode(dayTwo, OUTPUT);
  pinMode(dayThree, OUTPUT);
}

void loop() {
  if (!completed) {
    // Start by turning all three days ON
    analogWrite(dayOne, 255);
    analogWrite(dayTwo, 255);
    analogWrite(dayThree, 255);
    for (int day = 1; day <= 3; day++) {
      switch(day) {
        case 1:
          // fade day One
          for (int fade = 255; fade >= 0; fade--) {
            analogWrite(dayOne, fade);
            delay(10);
          }
          break;
        case 2:
          // fade day Two
          for (int fade = 255; fade >= 0; fade--) {
            analogWrite(dayTwo, fade);
            delay(10);
          }
          break;
        case 3:
          // fade day Three
          for (int fade = 255; fade >= 0; fade--) {
            analogWrite(dayThree, fade);
            delay(10);
          }
          break;
      }
    }
    completed = 1;
  }
}
« Last Edit: February 07, 2012, 10:49:18 am by KirAsh4 » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46030
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    for (int brightnessa=100; brightnessa > 0; brightnessa--){
      analogWrite(5, brightnessa);
      delay(50);
    }
The middle portion of a for loop is the continue clause. You are saying to execute the body of the loop as long as brightnessa is greater than 0. You want to execute the body of the loop as long as brightnessa is greater than OR EQUAL to zero.

Code:
   if (brightnessa == 0)
This would then be tested for less than or equal 0. But this test is unnecessary. You won't get to this statement until brightness is less than (or equal) 0, so remove this test (and the { and } that go with it.

If warnings were enabled, you'd see that brightnessa is local to the for loop, so referencing it like this is not a good idea, anyway.
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

By the way, the code I posted above was written as I was running out the door this morning, I literally had about a minute to do that.  I did plug my Uno in and ran it and it seemed to do what you want it to do, fade the three LEDs down and remain off at the end (it jumps out of the loop once all three LEDs are off and the 'completed' variable is set.)  However, a couple of things:

a) the delays in my code are short just so I could see the effect (as opposed to waiting three days of course), so you'll have to adjust them to your programming.  Personally I would get away from using delay() altogether, but that's an exercise you have to do yourself.

b) there is no mechanism to figure out if the code got reset within the 3 days, in which case you don't really want to start at the beginning again, I don't think.  For example, if the system resets a day into it, you really want it to pick up where it left off, with the first LED already turned off and the second one faded down to whatever level is appropriate.  However, this might also mean you having to put an RTC in the circuit.  Not sure if that's worth it for you or not.  You could also use the EEPROM to set the values each time, and have a reset start off with the last value written, but again, if the thing was turned off for a period of time, then your counter would be off.  An RTC would fix that.

c) as with the phrase, 'there are many ways to skin the cat', there are also many ways of doing this piece of code.  In a different iteration, I might put the assigned pins in an array and make the loops different, and the whole code shorter.

But, it's a start.  You need to integrate it with your current RFID code of course, and then go from there, adjust it to your liking.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I sincerely have never found an Internet forum community more helpful and quick than this one. Wow, thank you to everyone and especially KirAsh4. Not only does this all work just the way I envisioned it, but I learned a little about the programming language as well.

I can't thank you all enough.

KirAsh4 - I did the math for the delay, basically breaking down 255 steps of the fade into 5.something minute intervals so that it goes from full to off over 24 hours. I connected the Arduino to wall power and have started the "test & adjust" phase by timing it with my phone. So, we'll see how it goes.

In terms of functionality, the reset works exactly how I imagined it. At any point during the fade, I can reset it by swiping my RFID tag. Whether all the LEDs are off, or say, maybe only the first LED is halfway dimmed. So this is just peachy.
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I understood the card as being the reset, so it essentially starts from the beginning, correct?  What happens if you start the pseudo clock on the 11th (runs for 3 days to the 14th), and you swipe the card on the 13th?  The system resets back to the beginning, yes?  or did you write the code to restart where it left off, with only a day left to run, therefore only one LED remains lit?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No, the swipe initiates a full reset of the system, whether it's after a few minutes or a few months. The goal being that after three days, all the lights run out.

See the test image: http://goo.gl/zLBuK
Logged

Pages: [1] 2 3   Go Up
Jump to: