little question, big problem

Hi everybody.
I recently started with arduino and programming. And several of the starter programs has been ok, but now there is one of them that is very simple but nobody knows how to solve.
As you can see, it is a very simple code with very very little lines of code.
It starts with the "fading" arduino's example but i have added a blinking before the fade.
It should be easy but it has been imposible.
Each part separately works, but not together.
I have done many test and i realized that the problem is the "delay".If i remove them it runs ok, but you can't see the blinking effect. But with the delays it is always blinking and never goes to fade effect. But that's all i can say because i have no experience.
If you can help me i would thank you.

int led = 11;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by


void setup() 
{
 pinMode(led, OUTPUT);

 }

void loop() 
{ 
    digitalWrite(led, HIGH);
    delay(500);
    digitalWrite(led,LOW);
    delay(500); 
     
  analogWrite(led, brightness);   
  brightness = brightness + fadeAmount;
  if (brightness == 0 || brightness == 125)
  {
    fadeAmount = -fadeAmount ;
  }     
  delay(30);
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Read your code out loud.

Turn the led on.
Wait
Turn the led off.
Wait.
Flash the led with PWM.
Go back to the beginning... Which means, immediately turn the led on again....

From what you describe, the code is behaving exactly as you wrote it.

Thak you for your answer JamesC4S.
But i never says that ends with the pwm. The code says: led on, wait, led off, wait, goes to pwm and stay there because i say keep in there between 0-125 for the led.
And also, if it is as you say, at least we should watch how the led increases but we don't see.

This is what i think it says but i'm very new with this. But if you don't mind, could you please tell me how the correct code would be ?.
I have no more ideas....

Thanks again.

krilos:
The code says: led on, wait, led off, wait, goes to pwm and stay there because i say keep in there between 0-125 for the led.

No, it doesn't. There is nothing that makes the code "stay there." Each iteration of the entire loop increments brightness by 5. So it will take many seconds before you notice a change in the brightness.

However...

Without a significant delay between the analogWrite and the first digitalWrite at the top of the loop, the code won't sit around long enough for you to see the brightness of the LED anyway.

Hi. I am testing right now and something happens....
I have changed: fadeAmount=125, brightness=255, the last delay=1000.
It seems to be as you say but i need to test more time because i don't understand well what is happening but i wanted to tell you about you are right.
But what i don´t know is how to say the code never go out of the pwm. I you could tell me :roll_eyes:

Thanks !!

Hint, I think you a missing a for() loop or do...while() loop around the part of the code where you increment brightness and call analogWrite.

I could be wrong, but I think you're trying to blink the led once and then fade it up. That's not what your code says right now.

Just add this statement in loop() and there is no big problem :wink:

  digitalWrite(led, HIGH);
  delay(500);
  digitalWrite(led,LOW);
  delay(1000);
  
  //statement you need do and wihle
  do{ 
  // set the brightness of pin 9:
  analogWrite(led, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(30);
  }while(0 !=brightness);//statement you need
}

cheers

James C4S says

Hint, I think you a missing a for() loop or do...while() loop around the part of the code where you increment brightness and call analogWrite.

I could be wrong, but I think you're trying to blink the led once and then fade it up. That's not what your code says right now.

James C4S right
Cybernetician late to answer.

Yes, as you say what i want is blink the led and after that fade it up and down all the time.
But don't worry, with the clue you have given me i want try it and i will tell you if i can get something or if i need your help again, ok?.

Thanks again for your help !!

Yes, as you say what i want is blink the led and after that fade it up and down all the time.
But don't worry, with the clue you have given me i want try it and i will tell you if i can get something or if i need your help again, ok?.

Thanks again for your help !!

Above code showing exact output what you need

Hello Cybernetician. Thank you for your answer. It has been also very important because i was trying to do with an "if" sentence because i still don't know all the sentences for arduino.
It's been great you have put it finished and your smiley!.
I have changed one thing because a i want the program keep inside the pwm. So i have changed brightness=1, fadeAmount=1, brightness==1, last delay=10.

This way it never goes to zero and never goes to the begining.

I have a new question now. So, in my original code, we start reading the code and it goes through all the lines once, and after reading the last delay(30), why does it go to the begining and doesn't go the "if" it belongs as i thought??.

Thanks again for you and JamesC4S !! This was driving me crazy :wink:

Post your modify code and to check values use serial port

krilos:
why does it go to the begining and doesn't go the "if" it belongs as i thought??.

How would the code know what belongs with the if-statement. If-statements are a on time instruction, like most statements. You must use a loop to repeat it.

I see, i see. I thought that when we use an "if", it returns to the "if" always until it has completed done the condition.
Now i know that i must use and extra condition to keep it inside the loop.
I'll try to use the serial port as you say Cybernetician.
Only one question please:

  • Is possible use pwm in 6 outputs at the same time ?.
    I have thought that correct way would be going pin by pin increasing step by step util it goes to the maximum(255) and the same for decreasing until zero. But i can't use pw1 at pin 3 and pwm2 at pin5, and pwm3 at pin, ..... because the code is read up to down and if it is inside a loop never will go to the next, isn't it?

Thanks again for all your help, i's been very very good.

Finally it is this way and it works just i want:

int led = 11;           // the pin that the LED is attached to
int brightness = 1;    // how bright the LED is
int fadeAmount = 1;    // how many points to fade the LED by


void setup()
{
 pinMode(led, OUTPUT);

 }

void loop()
{
  digitalWrite(led, HIGH);
  delay(500);
  digitalWrite(led,LOW);
  delay(1000);
  
  //statement you need do and wihle
  do{ 
  // set the brightness of pin 9:
  analogWrite(led, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 1 || brightness == 200) {
    fadeAmount = -fadeAmount ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(10);
  }while(0 !=brightness);//statement you need
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Only one question please:

  • Is possible use pwm in 6 outputs at the same time ?.
    I have thought that correct way would be going pin by pin increasing step by step util it goes to the maximum(255) and the same for decreasing until zero. But i can't use pw1 at pin 3 and pwm2 at pin5, and pwm3 at pin, ..... because the code is read up to down and if it is inside a loop never will go to the next, isn't it?

May be "Yes" in arduino
by using interrupts
because i use something like that long ago in "PIC Controller" programming not in arduino platform. And Accuracy of result also decreases by adding more tasks.

by using Real time operating System
Again i use rtos in "PIC Controller" to do multiple tasks simultaneously.

I prefer Second option.

Ok, it was only a question at the momment..... , but i will try XD
Thaks again for all your help. It has been very important for me now that i am begining with this.

Ok, it was only a question at the momment..... , but i will try
Thaks again for all your help. It has been very important for me now that i am begining with this.

It's my pleasure. Enjoy "Art of Logic" 8)

krilos:

  • Is possible use pwm in 6 outputs at the same time ?

Yes. You can use all 6 PWM at the same time with analogWrite()s.

Cybernetician:

  • Is possible use pwm in 6 outputs at the same time ?

by using interrupts
by using Real time operating System
I prefer Second option.

I prefer the third option: Using the six PWM pins to perform PWM and blink-without-delay to keep the whole thing running smoothly.