Go Down

Topic: [SOLVED] Is PWM in a SUB possible ? (Read 839 times) previous topic - next topic

Trixi

Oct 05, 2015, 07:30 pm Last Edit: Oct 06, 2015, 12:11 pm by A.R.Ty
Hoi Folks,
is it possible to call a Sub which contains PWM-(LED-Dimming, runs normally in a loop) from a running sketch ?
Thx
Dum spiro, spero - if you find my posting helpful, please click my Karma :)

Delta_G

Do you mean to call a function?  Yes, there are no restrictions on what you can or cannot put into a function. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Trixi

Here is the code i am using in the sub - but the LED always turns on to full brightness without dimming.
Code: [Select]
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:
Code: [Select]

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);
}


Dum spiro, spero - if you find my posting helpful, please click my Karma :)

Delta_G

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.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

CrossRoads

Put a Serial.println (brightness);
in and see what values you are getting. Perhaps 0 and 255 are never seen.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Trixi

Dum spiro, spero - if you find my posting helpful, please click my Karma :)

Delta_G

Can you please post the code, the whole code.  Without it the best we can do is guess why it doesn't work.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Trixi

#7
Oct 05, 2015, 08:34 pm Last Edit: Oct 05, 2015, 08:39 pm by A.R.Ty
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 ?
Dum spiro, spero - if you find my posting helpful, please click my Karma :)

UKHeliBob

Quote
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 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

Trixi

#10
Oct 05, 2015, 09:02 pm Last Edit: Oct 05, 2015, 09:18 pm by A.R.Ty
Quote
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:
Code: [Select]
gleislicht1an();
Dum spiro, spero - if you find my posting helpful, please click my Karma :)

DrAzzy

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.
ATTinyCore for x4/x5/x61/x7/x8/x41/1634/828/x313 megaTinyCore for the megaavr ATtinies - Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts, mosfets, awesome prototyping board in my store http://tindie.com/stores/DrAzzy

Delta_G

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:
Code: [Select]
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.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Trixi

Quote
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  :smiley-sleep:

I think the whole time, a sub was a subroutine.

Dum spiro, spero - if you find my posting helpful, please click my Karma :)

UKHeliBob

Quote
NOW i know why it don't work
and the reason was ........ ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up