# How to use an exponential function in order to control PWM output?

Essentially I have a homework assignment where we have to write a sketch for a breathing LED using essentially a piece wise function containing two exponential functions, one inhale and one exhale, and a pause in between that is constant. The only code I have to go on is a sketch where you have three light levels but they are constant. So basically the voltage would start at lets say 20, exponentially ramp up to lets say 235, pause for a second, and then ramp back down to lets say 35. I have spent about 6 hours on this problem, there are no tutors, no book to reference and no office hours until after it is do and ZERO background in programming. Any help would be much appreciated!

So the code I have as a baseline is below.

int LED_pin = 11;

void setup() {
Serial.begin(9600);
pinMode(LED_pin, OUTPUT);
}

void loop() {

int v, vin=40, vpause=250, vex=20;
unsigned long tm, t2=2000, t3=2500, t4=5000;

tm = millis();

if ( tm<=t2 ) {
v = vin;
} else if ( tm<=t3 ) {
v = vpause;
} else if ( tm<=t4 ) {
v = vex;
} else {
v = 0;
}
analogWrite(LED_pin,v);

Serial.print™; Serial.print(" ");
Serial.println(v);
}

Being that this is an assignment, the usual answer is "Do your own homework". But im nice, and I'll give you a hint to point you in the right direction.

From what I gather, your led must start off dim and increase to full brightness then back down. Well the analogWrite function needs a pin number and a value within a byte (0 - 255) range. So what you could do is have a value start at ZERO (or where ever you want to start from) and slowly increment it to full brightness(255), pause for a moment then decrement that value back down to zero. Rinse and repeat. Nothing fancy needed. Also look at the example sketch Blink Without Delay.

Oh and 2 ^ (N - 1) will come in handy.

Definitely not opposed to doing my own homework, I have already spent 10 hours on this assignment and have made no progress. I was told I would be taught how to write arduino, so far this has not been the case, there is no book, no tutors, I’m literally on my own with no idea what to do, and I was told that its not cheating if i go online to get help/reference material. I appreciate your hints but still have no clue what to do. Basically I can write the blink code and thats it, anything beyond that I haven’t learned yet. In over my head.

There are thousands of online tutorials, almost all of which are on YouTube. Jeremy Blum makes great tutorials for beginners.
You said you spent 10 hours on this, did you go through each example provided with the Arduino software?
Mainly pointing to Examples -> analog -> fading?

I'm not trying to be rude or anything but 10 hours and you didn't try the analog examples?

I did look online and through the examples, but non of them use an exponential function to control the brightness of the light.

Pause
.____.
. .
Inhale . . Exhale
. .
. .
. .

This shows the brightness of the light, I don’t know how or where to write en exponential function in the sketch to do this, all of the examples, including the fade, don’t have exponential functions in them : ( I do appreciate the help though. I just need to have a chat with my professor, they really need to be teaching the stuff were expected to know.

Your setup function is fine. Now you just need two IF statements, one to check if the Byte is greater than 8, and the other if it is less than or equal to zero. That Boolean variable will determine if the LED inhales or exhales, with this: "Boolean variable" ? "byte"++ : "byte"--; (substitute the "" with your actual variables)

Then with some delay, say 1000, and the pow function. pow(base, exponent), you can control the brightness of the LED on pin 11.
Put it all together and your LED is breathing.

Getting the LED to do this smoothly requires a little more.

MechENGRSTDNT:
I don't know how or where to write en exponential function in the sketch to do this, all of the examples, including the fade, don't have exponential functions in them : ( I do appreciate the help though. I just need to have a chat with my professor, they really need to be teaching the stuff were expected to know.

I guess if you have a professor you are at University. I imagine you came across exponential functions before you got to University.

How would you do an exponential function with pencil and paper?

...R

Possibly not how you would do it with pencil and paper, but to implement a stepwise exponential, you simply multiply the value by a constant at each step.

Since the eventual resolution here is no more than 255 levels, the most pragmatic way to do it is to multiply integers using a "star-slash" procedure from the FORTH language. Multiply a 16 bit integer (starting with 256) by a value slightly greater than 256, then "lose" the last byte of the (24-bit) result - divide by 256 by right-shifting 8 bits. Repeat this with each step and use the high-order byte as the output.

Some (logarithmic) calculation in advance will allow estimation (but will need to be tested) of what value to multiply at each step will result in the maximum intended value in the desired number of steps.

And the reverse of the process is to multiply the initial maximum value by less than 256 at each step.