Author Topic: Measure PWM width  (Read 3653 times)

electrococo

  • Newbie
  • *
  • Posts: 2
Measure PWM width
« on: February 17, 2014, 01:43:53 pm »
Hello, I am trying to read the time when a push button is pressed by using micro () function and the to use delay function for commanding a motor.

I need help because it is not possible to have a good accuracy. Any idea? Thanks

// set pin numbers:
   int start_button = 1;     // the number of the pushbutton pin
   int pump = 2;           // the number of the LED pin
   int time_button = 0;      // the time captured pin

   //unsigned long time = 0;
   int start;

   volatile unsigned long injTime1;  //Time of front raising
   volatile unsigned long injTime2;  //Time of front falling
   unsigned long injTime = 0;

void setup() {
// initialize the LED pin as an output:
   pinMode(pump, OUTPUT);     
   pinMode(start_button, INPUT);
   pinMode(time_button, INPUT); 
   
  digitalWrite (pump, LOW);  //Pull-up pin - as our injector is driven by connecting it to ground, i suppose i need to pull-up pin to     get it's value as 1 when injector is turned off
  attachInterrupt(0, measure, CHANGE);  //interrupt on raising front
   }

void loop() {
 
// read the state of the pushbutton value:
   start = digitalRead(start_button);
 
   if (start == HIGH) {
      digitalWrite (pump, HIGH);
     delay (injTime);
     digitalWrite (pump, LOW);
      }
  else {
      digitalWrite (pump, LOW);
     }
  }
 
 
void measure()
{
  if ( digitalRead(time_button) == HIGH ) //Or i need to digitalRead it first? YES.[/glow]
 {
  injTime = 0;
  injTime1 = micros(); //get time of pulse going down
 }
 else
 {
  injTime2 = micros();  //get time of pulse going up
  injTime = injTime2 - injTime1;  //measure time between down and up
  injTime = injTime /4;
  injTime=  injTime / 1024;
 }
 
 }


dougal

  • Sr. Member
  • ****
  • Posts: 289
Re: Measure PWM width
« Reply #1 on: February 17, 2014, 11:20:19 pm »
Perhaps you need to debounce the timer button? Maybe with a small capacitor to smooth things out?

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: Measure PWM width
« Reply #2 on: February 18, 2014, 01:17:10 pm »
also PulseIn() might be useful here: http://arduino.cc/en/Reference/pulseIn

electrococo

  • Newbie
  • *
  • Posts: 2
Re: Measure PWM width
« Reply #3 on: February 22, 2014, 07:42:18 am »
Thank you both. I have used pulsein() function but without any interruption, shall I use pulsein into a interrupt?

O the other hand, what debounce means? You think that my no resulution comes from the buttons?

Thanks you

dougal

  • Sr. Member
  • ****
  • Posts: 289
Re: Measure PWM width
« Reply #4 on: February 25, 2014, 05:28:49 am »
Here's a pretty good article I found about switch debouncing:

http://www.labbookpages.co.uk/electronics/debounce.html

When most switches change state, the signal doesn't make a perfectly clean transition from 'off' to 'on' (or vice versa). You get some noise in the signal, which on a very small time scale looks like the switch turns off and on several times before settling in to the final value. That's the 'bounce' in the signal. Debouncing is when we clean that signal up. Often you can do it by just adding a small capacitor to the circuit.

You can also debounce in software, by only accepting state changes that last longer than a certain minimum amount of time.