Pages: [1] 2   Go Down
Author Topic: little question, big problem  (Read 1123 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Code:
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: [code] [/code] tags added.
« Last Edit: November 15, 2012, 05:53:20 pm by Coding Badly » Logged

Austin, TX
Online Online
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

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

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.
Logged

Austin, TX
Online Online
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

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

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  smiley-roll-blue

Thanks !!
Logged

Austin, TX
Online Online
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just add this statement in loop() and there is no big problem smiley-wink
Code:
  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
 
Logged

From Idea To Invention

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

James C4S says
Quote
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.
Logged

From Idea To Invention

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

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 !!
Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Logged

From Idea To Invention

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

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  smiley-wink
Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post your modify code and to check values use serial port   
Logged

From Idea To Invention

Austin, TX
Online Online
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

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

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:


Code:
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: [code] [/code] tags added.
« Last Edit: November 15, 2012, 05:52:20 pm by Coding Badly » Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

From Idea To Invention

Pages: [1] 2   Go Up
Jump to: