Hoi Folks,
is it possible to call a Sub which contains PWM-(LED-Dimming, runs normally in a loop) from a running sketch ?
Thx
Do you mean to call a function? Yes, there are no restrictions on what you can or cannot put into a function.
Here is the code i am using in the sub - but the LED always turns on to full brightness without dimming.
void gleislicht1an()
{
// set the brightness of pin 9:
analogWrite(Gleis1Licht, 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(150);
}
I derived this from the sample code which works fine:
int led = 11; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 10; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// 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(150);
}
Can you show your whole code. There's not much I can tell from one function without knowing how you are calling it.
Remember that the loop function gets called over and over and over again by main(), so it may work a little differently in your function if it is only being called once or not very often.
Put a Serial.println (brightness);
in and see what values you are getting. Perhaps 0 and 255 are never seen.
it shows 255 at once
Can you please post the code, the whole code. Without it the best we can do is guess why it doesn't work.
Its too big and has several tabs.
Thx so far, will try another way.
Maybe its possible to do some do loop in the sub ?
Its too big and has several tabs.
How about posting a small program that demonstrates the problem ? Call the gleislicht1an() function in the same way as you do in your full program. Does it work as expected ?
A common problem is having a global variable and local variable inside the function with the same name. Then the function will never see the global variable.
...R
How about posting a small program that demonstrates the problem ? Call the gleislicht1an() function in the same way as you do in your full program. Does it work as expected ?
i did already posted it - the declarations of course not, but its as simple:
i used the "original Code" and put that in a own sub (i posted this too).
I call it simply from the main code like this:
gleislicht1an();
That code looks okay, the problem is elsewhere in your program. We don't know how brightness is declared, or what fadeamount is, or what else is happening in the program between calls to that function, we'd need a crystal ball to tell you what the problem is. You haven't even told us what you get out when you add serial logging to record brightness (only that you got 255 first - presumably because that's what you defined brightness as - but it would be printing a number every time that code runs, so what do you get after that!?)
When you post asking for help with code, you need to be willing and able to provide us enough information for us to be able to help.
A.R.Ty:
i did already posted it - the declarations of course not, but its as simple:
i used the "original Code" and put that in a own sub (i posted this too).I call it simply from the main code like this:
gleislicht1an();
No, the code you posted is just one function. There's no setup or loop and we can't see the variable definitions.
And this is C++. It is NOT called a subroutine. It is called a function. In no language is it called just a sub. A sub is a boat that goes underwater.
And this is C++. It is NOT called a subroutine. It is called a function. In no language is it called just a sub. A sub is a boat that goes underwater.
Aaah, stupid me - NOW i know why it don't work - THANK YOU
I think the whole time, a sub was a subroutine.
NOW i know why it don't work
and the reason was ........ ?
as written, i looked for the
do {
...
...
} while();
command and that solves the problem.
THIS hint i asked for
I would be interested to see your working code as I don't understand your answer concerning solving the problem with a do/while loop.