Go Down

### Topic: variating an ouput over time (Read 3519 times)previous topic - next topic

#### Joes

##### Sep 06, 2012, 10:50 pm
hi there
im trying to alter a pwm output over a period of time which can be adjusted by a potentiometer from 5mins to 25mins and also another potentiometer to adjust the maximum position it will go up to any ideas of how to do this or any examples anyones got ?

#### johnwasser

#1
##### Sep 07, 2012, 12:06 am
What do you want to happen when the end of the 5-to-25-minute time interval is over?

Code: [Select]
`static unsigned long startTime = 0;int maxBrightness = map(analogRead(brightnessPot), 0, 1023, 0 255);int maxSeconds = map(analogRead(timePot),0, 1023, 5*60, 25*60);int elapsedSeconds = (millis() - startTime) / 1000;// Assuming you want to start over at dark when the time limit is upif (elapsedSeconds >= maxSeconds) {    startTime = millis();    elapsedSeconds = 0;    }analogWrite(outputPin, map(elapsedSeconds, 0, maxSeconds, 0, maxBrightness));`
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

#### Joes

#2
##### Sep 07, 2012, 06:55 pm
Quote

What do you want to happen when the end of the 5-to-25-minute time interval is over?

nothing just want it to variate an output over a period of time when a switch go's high

#### PeterH

#3
##### Sep 07, 2012, 11:38 pm

Quote

What do you want to happen when the end of the 5-to-25-minute time interval is over?

nothing just want it to variate an output over a period of time when a switch go's high

"Variate" isn't a real word. Do you mean "vary"? If so, how do you want it to vary - just a linear change in the duty cycle starting at zero and ending at the maximum defined by your pot? Is it possible that either of the pots will be adjusted during this timed processing? You mentioned a switch - do you need to deal with the switch going low or high after it has triggered this process? Do you want to respond to any further changes to the switch state during the time processing? How about after it has completed?

#### Joes

#4
##### Sep 07, 2012, 11:59 pm
Quote
just a linear change in the duty cycle starting at zero and ending at the maximum defined by your pot?

yes and to go the other way when the switch is in the opposite direction

Quote
Is it possible that either of the pots will be adjusted during this timed processing?

no

Quote
You mentioned a switch - do you need to deal with the switch going low or high after it has triggered this process? Do you want to respond to any further changes to the switch state during the time processing? How about after it has completed?

i was using the switch to trigger the process for example when the switch goes high it will slowly increase the pin over the duration the one pot is set to and to the maximum point the other pot is set to when that point has reached it will stay there until the switch goes low and then it will wind back down to 0 over the period of time the pot is set at

#### PeterH

#5
##### Sep 08, 2012, 02:06 am

i was using the switch to trigger the process for example when the switch goes high it will slowly increase the pin over the duration the one pot is set to and to the maximum point the other pot is set to when that point has reached it will stay there until the switch goes low and then it will wind back down to 0 over the period of time the pot is set at

OK, I think I understand what you want now.

You need to read the switch state repeatedly until it changes. When it changes, read the analog pots to establish the timing and maximum level settings.

Hard-code the relationship between the timing pot and the time interval between adjusting the output value.

Each time the time interval passes, adjust the output value. The new output value will be determined by multiplying the proportion of the time that has passed, and the maximum level. This will give you a duty cycle which you output by doing an analog write.

For what it's worth, I suggest you continue to monitor the switch and reverse the direction if the switch is changed during this process - just because systems that lock up and won't allow you to interrupt what they're doing irritate me.

#### Joes

#6
##### Sep 08, 2012, 10:18 am
yes ok
so how do we do that then?

#### PeterH

#7
##### Sep 08, 2012, 01:29 pm

yes ok
so how do we do that then?

Well, you take each of the sentences above and implement the corresponding behaviour in your code.

Do you know how to read the state of a switch? Do you know how to do it repeatedly? Do you know how to record the previous state and detect when the current state is different to the previous state? It's not hard, and you don't need to do the whole thing in one go, just start from the basics and work up. Make sure you test each part and get it working as you want before you move on to the next part. A sketch that prints a message each tiome the switch changes would be a good start. Then add code to read the analog inputs and print those values at the same time. And so on.

#### Joes

#8
##### Sep 08, 2012, 07:22 pm
Quote
Do you know how to read the state of a switch?

yes

Quote
Do you know how to do it repeatedly? Do you know how to record the previous state and detect when the current state is different to the previous state?

no

#### AWOL

#9
##### Sep 08, 2012, 08:01 pm
How would you (not programming) determine if a light had been switched on, if you had no long term memory, but you did have a sheet of paper, a pencil and an eraser?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### Joes

#10
##### Sep 08, 2012, 11:51 pm
forget it, as by the time ive started writing it down i have already forgotten what i was going to write lol

#### johnwasser

#11
##### Sep 09, 2012, 12:04 am
Code: [Select]
`void loop()   {   static unsigned long startTime = 0;   static boolean switchState = LOW;   if (digitalRead(switchPin) != switchState) {      startTime = millis();      switchState = digitalRead(switchPin);   }   int maxBrightness = map(analogRead(brightnessPot), 0, 1023, 0 255);   int maxSeconds = map(analogRead(timePot),0, 1023, 5*60, 25*60);   int elapsedSeconds = (millis() - startTime) / 1000;   if (switchState && elapsedSeconds < maxSeconds) {      // Getting brighter      analogWrite(outputPin, map(elapsedSeconds, 0, maxSeconds, 0, maxBrightness));   }   if (!switchState && elapsedSeconds < maxSeconds) {     // Getting dimmer      analogWrite(outputPin, map(elapsedSeconds, 0, maxSeconds, maxBrightness, 0));   }}`
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

#### PeterH

#12
##### Sep 09, 2012, 12:21 am

no

Are you serious about completing this project, or just speculating wildly about something in the hope that somebody might write for you?

#### Joes

#13
##### Sep 09, 2012, 08:36 pm
thanks johnwasser for that can you explain what is going on in it?

#### johnwasser

#14
##### Sep 09, 2012, 08:54 pm

thanks johnwasser for that can you explain what is going on in it?