PWM an AC pump!

Hi there,

What I am looking to do is in essence what the title suggest however I am fully aware that this is not entirely possible so I just want to make clear I am NOT trying to vary the flow rate at any fast PWM rate, in fact a very slow one.

What I want to do is get Arduino to generate a saw-tooth wave such that it counts up to its operating voltage (5v) and back down within a REGULAR time period of say 10 seconds, 0.1Hz. The key point here is it must take the same amount of time to do this each cycle and for that reason I believe I will need to make direct use of the Atmega's timers? Now in order to get my PWM effect I will have an analogue signal coming in, say from a POT, and I want to compare this value to the one from the saw-tooth such that when the analogue signal is greater than that of the saw-tooth's it digitally turns the pump ON. When lower, OFF. If my logic is correct and this is feasible, the effect will be the pump turning on and off for varying amounts of time dependant on the analogue input.

Any thoughts or advice on this would be great. Thanks

Cross-posted. Keep your questions in one post.

Sorry, I just thought it was getting out of topic from what I had first asked so decided to write it again in a more appropriate place. Is it best to leave it in one place then no matter if it meanders off topic?

Is it best to leave it in one place then no matter if it meanders off topic?

Isn't that better than getting contradictory advice in two different places?

I don't understand what you're trying to achieve. Is 'AC' alternating current? At what Voltage? Are you trying to do something similar to a main voltage dimmer switch?

I don't really understand exactly what you're wanting to do, but it sure doesn't seem as if actually generating a a sawtooth wave will be required, since you're only comparing the value of it to something else. So just compute that value itself and use it for comparison purposes. Should be pretty easy to do a sawtooth value using modulo arithmetic.

If you are trying to turn a pump on and off every 10 second or so but with a pot to decide what is the on off time, you could just use delay(); instead of trying to make a saw wave and comparing analog to it.

It looks like you have an analog background and try to convert that to a program.

If you use the analog read example, and modify it a little, it will do exactly what you want i think

Patgadget
Montreal

helo folks,

I'm using Arduino(Duemilanove). As I program any PWM pin(say pin9) out 6Pins(availabe) as the output pin with analogWrite(9, dutycycle), what voltage does this PWM signal have?

can I have access to that voltage value and is there any way i can manipulate the voltage of PWM output?

need help

thanx,
chiru

Right, OK I will try clear things up.

I want my pump (imagine it as an LED for now, as this is really a matter of programming) to turn ON for differing amounts of time. I also need the time that its ON, to be dependant on some input analogue signal.

My thinking:

#define LED1 13

int POT = 0;    // set value of POT to zero
int i = 0;    //set counter to 0

unsigned long timeStart = 0;    //number of milliseconds passed at beginning of loop
unsigned long timeFinish = 0;    //number of milliseconds passed at finish of loop
unsigned long frequency = 20;    //length of time we want the loop to run for
                                  // considering it must count to 1023 in say 5 seconds
unsigned long timeElapsed = 0;    // elapsed time of the loop
unsigned long timeFinal = 0;      // final time right at the end of loop for testing purposes

void setup() {
  pinMode(LED1,OUTPUT);    //set LED as an output
  //Serial.begin(9600);    //set serial port on
}

void loop() {
  timeStart = millis();
  for (i = 0; i < 1023; i++) { 
    //Serial.println(i);
    //Serial.println(POT);
    POT = analogRead(0); 
  if (POT <= i) {              // if less than or equal to count value
    digitalWrite(LED1, HIGH);    //turn LED on
  } else {                       
    digitalWrite(LED1, LOW);
  }
  }
  for (i = 1023; i > 0; i--) {
    //Serial.println(i);
    //Serial.println(POT);
    POT = analogRead(0); 
  if (POT <= i) {              // if less than or equal to count value
    digitalWrite(LED1, HIGH);    //turn LED on
  } else {                       
    digitalWrite(LED1, LOW);
  }
  }
  timeFinish = millis();      //take another time reading
    timeElapsed = timeFinish - timeStart;    //calculated elapsed time
    if (timeElapsed < frequency ) {          // if less than the desired time, which it should be
      delay(frequency - timeElapsed);        // wait for the difference in time
}
}

From this you will see, when the input signal is of a low value, the LED is on for longer than its off and vice versa. I am happy with this unless you think the same result can be achieved in a better manner.

Patgadget:
If you use the analog read example, and modify it a little, it will do exactly what you want i think

I don't think so, will it?

The bit I'm not happy with and I refer back to my initial post, is, I want Arduino to execute EACH loop in the same amount of time so that it creates this saw-tooth effect at regular time intervals. You can see I have attempted to do this in the latter of my code, but I'm aware it's not doing what I want. From what I've seen Arduino has no function that allows a loop to waitUntil(some_time_has_passed). Delay() is not what I want as it has no relation to the time passed up until that point.

Cheers

can I have access to that voltage value and is there any way i can manipulate the voltage of PWM output?

The voltage will be 5V (for a 5V board). PWM is turning the pin off and on many times per second, creating the effect, for some devices, of reduced voltage. No, you can not manipulate the on voltage, only the on duty cycle.

From what I've seen Arduino has no function that allows a loop to waitUntil(some_time_has_passed).

So, create one.

void waitUntil(unsigned long waitTime)
{
   while(millis() - lastTime <= waitTime) {} // Do nothing until...
}

Be sure to set lastTime to some appropriate time, using millis(), on each pass through loop().

Or, simply determine how long you need to wait, based on how long some code took, and how long you wanted it to take, and delay() that long.