Pages: [1]   Go Down
Author Topic: Progressive PWM Increase  (Read 614 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 80
Obsessed with Apple
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all!
I'm new to Arduino, but am learning pretty quickly.
I was wondering if there was any way to use PWM to steadily increase something.

Example: If you are using a thermistor, each degree that increases, you want to increase the speed of a motor that you have connected to a fan. So if it was 70°, the speed might be 200, 71°, the speed might be 210…

Another example: You have a photoresistor and an LED (hooked up to a PWM pin of course) and want each increase in light to increase the brightness of the LED, how would you do this?

Or maybe you want the LED to increase in brightness 10% each 15 minutes that go by.
How could this be done?

Thanks!
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, the analogWrite takes a parameter for the PWM that spans from 0 to 255.

So, you need a variable for the pwm and some logic for setting it correctly.


Linear relationships between two values can often be best solved by the map() function.

Code:
//example code for the thermistor problem
int temp = getTempFromThermistor();
int value = map(temp , minTemp, maxTemp, 0, 255); //this line maps the temperature from the range [minTemp,maxTemp] to the range [0,255]
analogWrite(ledPin,value);

When you need time to be a part of the equation, you need to use millis() in addition to the previous guidelines.



A specific problem would be easier to help..
Logged

~/earth/north_america/usa/mi
Offline Offline
Newbie
*
Karma: 3
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all!
I'm new to Arduino, but am learning pretty quickly.
I was wondering if there was any way to use PWM to steadily increase something.
...
Or maybe you want the LED to increase in brightness 10% each 15 minutes that go by.
How could this be done?

Thanks!


I'm winging this off the top of my head, so here goes:

Code:
#define INTERVAL 60000     // change interval in milliseconds
#define DELTAB       25     // change in brightness

const int LedPin = 13;

int LedBrightness = 0;
unsigned long previousMillis;

void setup(void)
{
  previousMillis = millis();
  analog.write(LedPin, LedBrightness);
}

void loop(void)
{
  if (millis() - previousMillis > INTERVAL)
  {
    previousMillis = millis();
    if (LedBrightness < 256)
    {
       LedBrightness += DELTAB;
      analog.write(LedPin, LedBrightness);
     }
  }
// do other stuff here ( or not)
}

This code will start the LED off, and then once a minute (60,000 ms) increase the pulse width by DELTAB until it's full on.  LED brightness may not be a linear function of pulse width.  I have code very similar to this controlling the brightness of 12 volt incandescent lamps.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13917
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


The code from tdc218 could check against some predefined maxBrightness value ..
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: