Pages: [1]   Go Down
Author Topic: variating an ouput over time  (Read 1101 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 296
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Tesla Member
***
Karma: 208
Posts: 8849
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 296
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 296
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 296
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes ok
so how do we do that then?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 296
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Brattain Member
*****
Karma: 302
Posts: 26285
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 296
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Tesla Member
***
Karma: 208
Posts: 8849
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 296
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8849
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks johnwasser for that can you explain what is going on in it?
You'll learn more if you try to understand it yourself.

If you don't want to learn, I will write the complete sketch for $50.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: