Go Down

Topic: Progressive PWM Increase (Read 651 times) previous topic - next topic

andrewf

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!

AlphaBeta

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: [Select]
//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..

tdc218


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: [Select]

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

robtillaart


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

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

Go Up