Go Down

Topic: Flickering LED (Read 1 time) previous topic - next topic

JimboZA

Quote
You are at -1 when you let go of the button.


..... which you would have seen if you were sending "y" to the monitor  :P
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

LarryD

Quote
while(y<=255 && button1State == LOW) //while LED is not fully lit and button 1 is pushed
    {
      analogWrite(ledPin, y);  // turn the LED on gradually
      delay(10);
      button1State = digitalRead(button1Pin); //check state of button
      y++; //step up y to increase brightness
      Serial.println(y);                                         //<<<<<<<< use Serial.print for debugging
    }
  }
  else
  {
    while(y>=0 && button1State==HIGH) //while LED is not completely off and button 1 is not pushed
    {
      analogWrite(ledPin, y);  // turn the LED on
      delay(10);
      button1State = digitalRead(button1Pin); //check state of button
      y--; //step down y to decrease brightness
      if (y<0) y=0;                                                // Comment this line to see the problem on the serial monitor
      Serial.println(y);                                         //<<<<<<<< use Serial.print for debugging


Use  Serial.println() to aid in troubleshooting code.           
The way you have it in your schematic isn't the same as how you have it wired up!

HazardsMind

#7
Jan 12, 2013, 05:10 am Last Edit: Jan 12, 2013, 05:14 am by HazardsMind Reason: 1
Your over complicating this. And make your button default low and make IF statement look for HIGH.
If button is high, increment counter (global var), send to LED.

Else button is low, decrement counter(same counter as before, starting point for decrement), send to LED.

Set limits, if counter < 0, set to zero. If counter > 255, set to 255.

Easy.

Edit: add debounce if needed.
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

dent424

Thanks for your help guys!

JimboZA, I do have a resistor connected to the button. Your advice about using Serial is definitely something I'm going to start doing! I hadn't gotten to the point in the tutorial where they introduced me to that yet, but I evidently already need it!

LarryD, thanks so much. I puzzled over this for a few minutes and I think I sort of understand why it's doing this.

Am I understanding things correctly if I say that with the analogWrite function, if I do analogWrite(LEDPin, 256), that is the same thing as analogWrite(LEDPin, 0)?

I still do not quite understand why the LED blinks. In particular, I understand that when I run through my while loops, the one with y++ leaves me with a final value of 256 (one more than 255) if I keep holding it and the while loop with y-- has a final value of -1 if I leave the button unpressed for long enough. The thing I'm puzzled about is why does this flickering happen the very first time that  I press the button? I would have thought that since I set y=0 at the very start, that at least the first time, the loop would start with a value of y=0 and not a value of y=-1

HazardsMind, I think I understand what you're saying, and the thought had briefly flickered through my mind when I was thinking of another mini-project I wanted to work on. If I understand you correctly, you are saying that I can ditch my two while loops. Does that sound right?

Thanks for the help everyone!

JimboZA

Quote
I would have thought that since I set y=0 at the very start, that at least the first time, the loop would start with a value of y=0 and not a value of y=-1


..... All the more reason to implement Serial asap, then you can see if what you think is the same as the Arduino thinks  8)
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Go Up