Go Down

Topic: HELP! Fading LED troubles! (Read 511 times) previous topic - next topic

AWilliams

I've been having trouble with a project and I need help! I need to have an LED fade in and out when I press a button until I press the button again. The LED fades in and out but I cant seem to get it to stop. Could anyone help? Here is the code:

const int ledPin = 9;
int waitTimeInMs = 1000;
unsigned int buttonPin = 8;
int lastKnownButtonState = LOW;
unsigned long lastTimeButtonChanged = 0;
unsigned int debounceDelay = 50;
int buttonState = LOW;

int buttonPushCounter = 0;


void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode( buttonPin, INPUT);
  Serial.begin(9600);
}


void loop() {

 int buttonValue = digitalRead(buttonPin);
           
if (buttonValue != lastKnownButtonState) {
           
  lastTimeButtonChanged = millis();
}
if ((millis() - lastTimeButtonChanged) > debounceDelay) {
         
  if (buttonValue != buttonState) {
             
    buttonState = buttonValue;

         
    if (buttonState == HIGH) {
      buttonPushCounter++;
     
    }
  }           
}           

              lastKnownButtonState = buttonValue;


  if (buttonPushCounter == 1) {
   
     for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 1) {
 
    analogWrite(ledPin, fadeValue);
   
    delay(5);
  }


  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 1) {

    analogWrite(ledPin, fadeValue);
   
    delay(5);
  }
   
  }
 
 
  if(buttonPushCounter == 2){
    analogWrite(ledPin, 0);
    buttonPushCounter = 0;
   
  }
}


Any help is appreciated!

Tissi_2

Same probleme here: https://forum.arduino.cc/index.php?topic=462909.0

I am not a pro but I think you have he same issue like me. We are both stuck in a loop with "delay" so that the board does not recognize the push button inputs anymore. I am trying to get out of this problem by using millis().

PaulRB

@Tissi_2, how is that going?

@AWilliams, code tags please! Read the forum rules for that & other tips.

Tissi_2

Well, you told me, that this is the problem. I tried a lot of different code, but I do not get it working. Is there anybody out there who can help us with a code example?

PaulRB

#4
Mar 18, 2017, 07:54 pm Last Edit: Mar 18, 2017, 07:55 pm by PaulRB
Well, you told me, that this is the problem. I tried a lot of different code, but I do not get it working. Is there anybody out there who can help us with a code example?
Did you? There has not been an update to your thread since I posted. I can't find where you posted the code you have tried and asked why it does not work. Why is that? Hoping someone will give you the code on a silver plate?

Tissi_2

Dear Paul,

thank you for your honesty! Due to your post I can see that you have the strong believe that most of the peolpe who ask for help did not try out code for a long time to get it working. Well, I really feel sorry for you that you are thinking in that negative way about other people. Do you really think it makes sense to bother others with tons of code that is not working the way it should be? You can handle it this way to spam the forum, waste time and energy and wrongly train people reading that code.

In my opinion a forum is a place where somebody should receive help when he or she asks for help. I have tried out code for many hours but unfortunately I am not that well trained to get it working because I am not an expert. So I ended up in this forum in order to ask for a help. Question - Answer. That´s it  - no more, no less.

If you are able to help because you are an expert in multithreading, then feel free to help me/us. If not you can bully somebody else. Thank you for your understanding!

Regards
Tissi

boolrules

Your reply to PaulRB got me interested enough to take a look at the code you referenced.
Just thought that you would be interested to know that I got it working.

Tissi_2

Hi boolrules! Could you provide the code to let us know where we made the mistake(s)? Then I can post your code im my thread and label it as solved. That would be highly appreciated!

PaulRB

Due to your post I can see that you have the strong believe that most of the peolpe who ask for help did not try out code for a long time to get it working.
Correct.

Well, I really feel sorry for you that you are thinking in that negative way about other people. 
Ah, to be young!

Do you really think it makes sense to bother others with tons of code that is not working the way it should be? You can handle it this way to spam the forum, waste time and energy and wrongly train people reading that code.
Yes. We learn most by our mistakes. And the mistakes of others.

In my opinion a forum is a place where somebody should receive help when he or she asks for help. I have tried out code for many hours but unfortunately I am not that well trained to get it working because I am not an expert. So I ended up in this forum in order to ask for a help. Question - Answer. That´s it  - no more, no less.
"Give a man a fish and he will eat for a day (and return tomorrow expecting to be given another fish). Teach a man to fish..."

If you are able to help because you are an expert in multithreading, then feel free to help me/us.
Check the other thread. I have gone soft on you and given you the answer.

If not you can bully somebody else.
But I very nearly didn't because of that comment.

Go Up