Pages: [1]   Go Down
 Author Topic: variating an ouput over time  (Read 549 times) 0 Members and 1 Guest are viewing this topic.
0
Offline
Full Member
Karma: 0
Posts: 236
Arduino rocks
 « on: September 06, 2012, 03:50:18 pm » Bigger Smaller Reset

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 ?
 Logged

Massachusetts, USA
Offline
Tesla Member
Karma: 108
Posts: 6607
 « Reply #1 on: September 06, 2012, 05:06:54 pm » Bigger Smaller Reset

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

Code:
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 up
if (elapsedSeconds >= maxSeconds) {
startTime = millis();
elapsedSeconds = 0;
}

analogWrite(outputPin, map(elapsedSeconds, 0, maxSeconds, 0, maxBrightness));
 Logged

0
Offline
Full Member
Karma: 0
Posts: 236
Arduino rocks
 « Reply #2 on: September 07, 2012, 11:55:18 am » Bigger Smaller Reset

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
 Logged

UK
Offline
Tesla Member
Karma: 100
Posts: 6784
-
 « Reply #3 on: September 07, 2012, 04:38:41 pm » Bigger Smaller Reset

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?
 Logged

0
Offline
Full Member
Karma: 0
Posts: 236
Arduino rocks
 « Reply #4 on: September 07, 2012, 04:59:17 pm » Bigger Smaller Reset

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
 Logged

UK
Offline
Tesla Member
Karma: 100
Posts: 6784
-
 « Reply #5 on: September 07, 2012, 07:06:00 pm » Bigger Smaller Reset

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

0
Offline
Full Member
Karma: 0
Posts: 236
Arduino rocks
 « Reply #6 on: September 08, 2012, 03:18:47 am » Bigger Smaller Reset

yes ok
so how do we do that then?
 Logged

UK
Offline
Tesla Member
Karma: 100
Posts: 6784
-
 « Reply #7 on: September 08, 2012, 06:29:52 am » Bigger Smaller Reset

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

0
Offline
Full Member
Karma: 0
Posts: 236
Arduino rocks
 « Reply #8 on: September 08, 2012, 12:22:20 pm » Bigger Smaller Reset

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
 Logged

Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
 « Reply #9 on: September 08, 2012, 01:01:27 pm » Bigger Smaller Reset

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?
 Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

0
Offline
Full Member
Karma: 0
Posts: 236
Arduino rocks
 « Reply #10 on: September 08, 2012, 04:51:48 pm » Bigger Smaller Reset

forget it, as by the time ive started writing it down i have already forgotten what i was going to write lol
 Logged

Massachusetts, USA
Offline
Tesla Member
Karma: 108
Posts: 6607
 « Reply #11 on: September 08, 2012, 05:04:15 pm » Bigger Smaller Reset

Code:
void loop()
{
static unsigned long startTime = 0;
static boolean switchState = LOW;

startTime = millis();
}

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

UK
Offline
Tesla Member
Karma: 100
Posts: 6784
-
 « Reply #12 on: September 08, 2012, 05:21:44 pm » Bigger Smaller Reset

no

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

0
Offline
Full Member
Karma: 0
Posts: 236
Arduino rocks
 « Reply #13 on: September 09, 2012, 01:36:43 pm » Bigger Smaller Reset

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

Massachusetts, USA
Offline
Tesla Member
Karma: 108
Posts: 6607
 « Reply #14 on: September 09, 2012, 01:54:22 pm » Bigger Smaller Reset

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