Program flow

I am very new to C type programming and have a few problems with program flow.
In the code below my brain wants the flow to be thus;

initiate variables >>
void set up >>
void RPMpulse >>
void calc >>

void loop<< continuously loops and never goes back to the void RPMpulse or void calc

// Program to count pulses per revolution in an automobile
// TODO: Includes a running average to insure proper RPM output
// TODO: Output a clean averaged 5v Sq wave of RPM
// NOTE: May need to go to (us) as opposed to (ms) for better resolution

int Cycle = 0;                  // set to 0 for PulseStartTime and set to 
                        //   1 for PulseEndTime
unsigned long PulseStartTime;   // Saves Start of pulse in ms
unsigned long PulseEndTime;     // Saves End of pulse in ms
unsigned long PulseTime;        // Stores dif between start and stop of pulse
unsigned long RPM = 0;          // RPM to ouptut (30*1000/PulseTime)

void setup()
{
 Serial.begin(9600);            // OPENS SERIAL PORT SETS DATA TO 9600 bps
 attachInterrupt(0, RPMPulse, RISING); // Attaches interrupt to Digi Pin 2
}

void RPMPulse() {
  if (Cycle)                // Check to see if start pulse
  {
    PulseStartTime = millis();  // stores start time
                                // CycleOnOrOff = 1;           // not defined.  Maybe this should be just "Cycle = true;" ??sets counter for start of pulse
  }
  else // otherwise it must be end pulse
  {
    detachInterrupt(0);         // Turns off inturrupt for calculations
    PulseEndTime = millis();    // stores end time
    Cycle = false;                  // resets counter for pulse cycle
    calcRPM();                  // call to calculate pulse time
  }
}

void calcRPM()
{
  PulseTime = PulseEndTime - PulseStartTime; // Gets pulse duration
  Serial.print("PulseTime =");               // Output pulse time for debug
  Serial.print(PulseTime);                   // Pulse debug output
  Serial.print(" ");                         
  RPM = 30*1000/PulseTime*2;                 // Calculates RPM
  attachInterrupt(0, RPMPulse, RISING);      // re-attaches interrupt to Digi Pin 2
}

void loop()
{ 
 Serial.print("RPM = ");      // Output RPM for debug
 Serial.print(int(RPM));      // RPM debug output
 Serial.print(" ");
 delay(1000);                  // 1 sec delay for debug output
}

I don't really agree with the last post, but anyway the most important question right now is how many RPM are you expecting ?

Duane B

rcarduino.blogspot.com

You shouldn't be doing serial prints, directly or indirectly, from an interrupt service routine.

The buffer will fill up, it will block and thus hang.

In the code below my brain wants the flow to be thus;

initiate variables >>
void set up >>
void RPMpulse >>
void calc >>

void loop<< continuously loops and never goes back to the void RPMpulse or void calc

The compiler, of course, has other ideas. The actual code that is generated from your sketch has a main function that calls init(), setup(), and loop(), in an endless loop. It may also call a serial event function, if you have defined one, and there is serial data to deal with, after each call to loop().

The rpm and calc functions in your code are called because you have registered the functions to be called when an external event occurs, using the attachInterrupt() function. That is the only reason that they get called at all.

DuaneB:
I don't really agree with the last post, but anyway the most important question right now is how many RPM are you expecting ?

Duane B

rcarduino.blogspot.com

4000 to 6000rpm

100Hz is a pretty slow rate, assuming 1 pulse per revolution.