Timing a low high event


evening,

i'm working towards end goal of creating solenoid v8 engine, run arduino. have sainsmart hall sensor board, used sense top dead centre. need record time 1 revolution, can take time, divide number of cylinders , give me time between firing each solenoid 1 revolution.

so there bit of backround start of project, need little help. i've looked @ stopwatch example , reworked time 1 revolution (or transition high --> low -->high -->low) (the sainsmart comparitor have 2 magnets in opposite poles). working should, i've realised going cause me issue going collect data every other revolution of engine because working stop watch, on first revolution see low high low start , stop timer, wait further set, i.e. time every other revolution = no good! have @ code please , see if advise me go here? guess needs start timing when sees 1 transition high low, time ever set of low high low, , @ end of each low high low save time taken "onecycle".

code: [select]
#define ledpin  13                  // led connected digital pin 13
#define buttonpin 4                 // button on pin 4

int value = low;                    // previous value of led
int buttonstate;                    // variable store button state
int lastbuttonstate;                // variable store last button state
int running;                        // condition running - timer timing
long interval = 100;                // blink interval - change suit
long previousmillis = 0;            // variable store last time led updated
long starttime ;                    // start time stop watch
long onecycle ;                     // elapsed time stop watch
int fractional;                     // variable used store fractional part of time
long cylinderinterval;               // time between cylinders in 1 rotation
long totalcylinders = 8;             // v8 = 8 cylinders

void setup()
{
   serial.begin(9600);

   pinmode(ledpin, output);         // sets digital pin output

   pinmode(buttonpin, input);       // not necessary, pins default input anyway
   digitalwrite(buttonpin, high);   // turn on pullup resistors. wire button press shorts pin ground.

}

void loop()
{
    // check button press
   buttonstate = digitalread(buttonpin);                   // read button state , store

   if (buttonstate == low && lastbuttonstate == high  &&  running == false){     // check high low transition
      // if true found new button press while clock not running - start clock

      starttime = millis();                                   // store start time
      running = true;                                     // turn on running while timing
      delay(5);                                               // short delay debounce switch
      lastbuttonstate = buttonstate;      // store buttonstate in lastbuttonstate, compare next time
      serial.println("timer started");

   }

   else if (buttonstate == low && lastbuttonstate == high && running == true){     // check high low transition
      // if true found new button press while clock running - stop clock , report

        onecycle =   millis() - starttime;              // store elapsed time 1 cycle
        running = false;                                  // turn off running, done timing
        lastbuttonstate = buttonstate;                     // store buttonstate in lastbuttonstate, compare next time

       // routine report elapsed time
        serial.println("timer stopped");
        serial.print( (int)(onecycle / 1000l));         // divide 1000 convert seconds - cast int print

        serial.print(".");                             // print decimal point

        // use modulo operator fractional part of time
       fractional = (int)(onecycle % 1000l);

       // pad in leading zeros - wouldn't nice if
       // arduino language had flag this? :)
       if (fractional == 0)
          serial.print("000");      // add 3 zero's
       else if (fractional < 10)    // if fractional < 10 0 ignored giving wrong time, add zeros
          serial.print("00");       // add 2 zeros
       else if (fractional < 100)
          serial.print("0");        // add 1 zero

       serial.println(fractional);  // print fractional part of time
     
      cylinderinterval = (onecycle/totalcylinders);   //total time 1 revolution divided number of cyinders
       serial.print("time between each cylinder: ");
       serial.println(cylinderinterval);
   }

   else{
      lastbuttonstate = buttonstate;                         // store buttonstate in lastbuttonstate, compare next time
   }
      if (running == true){
         digitalwrite(ledpin, high);
      }
      else{
         digitalwrite(ledpin, low);                         // turn off led when not running
      }
}

just because use 1 particular transition timing on 1 set, doesn't mean can't use on next set. 

so have low1 - high2 - low3 - high4.  you'll time low1 - high2 - low3, if had second set of same types of code, read high2 - low3 - high4.  keep low3 - high4 - low1 next set.   


Arduino Forum > Using Arduino > Programming Questions > Timing a low high event


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code