Using millis() to control fuel injector?

I’m trying to write a sketch to control the pulse width of a fuel injector for a methanol injection system on my car (among other related tasks). I can’t get the injector control to work right. I tried lighting LEDs at various points in the code, I ended up having one light after every single line of “real” code so it’s going through the whole thing, but the fuel injector was never commanded on. Everything above the dotted line I commented out for testing, I only included it in case someone sees a problem with it. Here is the code I have:

  if (MAPS > 500){                     // if MAP is high enough to need methanol...
    trim = analogRead(trimPin);           // read the trim pot and store value as 'trim'
    if (red > 1000){                      //  if the engine has been knocking for 1000 cycles through the loop
      digitalWrite (injPin, HIGH);          //  turn the injectors on 100%
      if (error2 < 65535 )error2 = error2++;//  increses error2 by 1 every time through the loop until 65535
    }
    else{                                 // else (the engine's not being hammered apart)
      injoff = trim / 25;  // turns the 0-1023 input from the trim pot into a usable injector off-time
      injon = MAPS * .5 + ontrim * .5 + (yellow * .1 + red *.5); // sets injector on time to (math stuff)
      if (injoff < 5){
        //        injoff = 5;  // ensures the injector is off for at least 5 mS
        if (error1 < 65535) error1 = error1++;  // increses error1 by 1 every time through the loop until 65535
      }
----------------------------------------------------------------------------------------------------------------------------------
      coninjpw = millis();                     // /
      coffinjpw = millis();                    // \sets current time to both variables
      if (injstate == LOW){                    // if the injector is off
        if (coffinjpw - poffinjpw >= injoff){  // if the injector has been off for >= than "injoff"
          injstate = HIGH;                     // turn the injector on
          poffinjpw = coffinjpw;               // set the 'previous' variable to the current time
        }
      }
      if (injstate == HIGH){                   // if the injector is on
        if (coninjpw - poninjpw >= injon){     // if the injector has been on for >= "injon"
          injstate = LOW;                      // turn the injector off
          poninjpw = coninjpw;                 // set the 'previous' variable to the current time
        }                                      
      }
      digitalWrite(injPin, injstate);
    }
  }
  else{                                        // if MAPS <= 500, turn the injector off
    digitalWrite(injPin, LOW);

Another question I have is about my “if” usage. I did it the way I did because I figured it would be faster to have two if statements instead of evaluating “&& coffinjpw - poffinjpw >= injoff” every time through. If I were to use “if (injstate == LOW && coffinjpw - poffinjpw >= injoff)” would it evaluate to false as soon as “injstate == LOW” is false or does it evaluate everything inside parenthesis before deciding? I remember reading that math with unsigned longs is “slower” but it didn’t say by how much.

Thanks for any help!

-Jason

If I were to use "if (injstate == LOW && coffinjpw - poffinjpw >= injoff)" would it evaluate to false as soon as "injstate == LOW" is false

Yes.

or does it evaluate everything inside parenthesis before deciding?

No.

It's called "short circuit evaluation".

I remember reading that math with unsigned longs is "slower" but it didn't say by how much.

One machine instruction per byte in the data-type. Char / byte math is one instruction. Int / short / unsigned is two instructions. Long / unsigned long is four instructions. One machine instruction per byte to load / store the data-type. Certainly not worth worrying about.

The floating-point math you have above the line is considerably more expensive.

Thanks for the input. I completely forgot about floating point math, that's good to be reminded of.

I got the code to work using fixed values for on and off (why I didn't think of that sooner, I don't know). I'm not yet far enough to know what was wrong with the code before, but I will post back when I get it working with the pots I have as "sensors".

You are welcome and thank you for the follow-up.

As it turns out, the problem was very simple. I find work more enjoyable if I shut my brain off while I'm there, and it apparently takes some time to turn back on when I get home (I work second shift, so it doesn't take that long :) ). Anyway, I wasn't modifying injon or injoff because I commented out the section of code that modified them, so they were still at 0, and the "injector" was being commanded on and off for 0 ms. As for the original problem that made me comment that out for troubleshooting, if I re-discover it I'll post again.