Looking for some help here. I have written out some code to add propane injection for my forklift. It is supposed to read three analog inputs, add them together and multiply the sum by three. There is an interrupt which is triggered by a hall effect sensor which is supposed to fire a solenoid valve for a duration of microseconds. I have mocked this up on a breadboard with a attiny85 pulsing the hall effect pin and nothing happens. Would appreciate some one having a look at the code and letting me know where I went wrong. Thanks.

const byte interruptPin = 2; //this is the interrupt pin hall sensor//
int mpPin = A3; //manifold pressure sensor//
int tpsPin = A4; //throttle position sensor//
int tunePin = A5; //tune pot 10k//
volatile byte inj; //this will end up being the duration of the injector pulse//
int mpValue = 0; //setting manifold variable to zero//
int tpsValue = 0; //setting throttle positon sensor value to zero//
int tuneValue = 0; //setting tune value sensor variable to zero//
int injValue = 0; //setting injection variable to zero//

void setup() {
    pinMode(12, OUTPUT);  //this is the pin that fires the injector//
    pinMode(interruptPin, INPUT_PULLUP); //set the interrupt pin to high pull up //
    attachInterrupt(digitalPinToInterrupt(interruptPin), shot, LOW);  //set the interrupt to trigger shot isr on low//

void loop() {
    tpsValue = analogRead(tpsPin); //read the throttle positon sensor//
    mpValue = analogRead(mpPin);//read the manifold pressure sensor//
    tuneValue = analogRead(tunePin); //read the tune sensor//
    injValue = (tpsValue + mpValue + tuneValue) * 4; // add the three sensors together and assign the value to inj//

void shot() {
    digitalWrite, 12, HIGH; // initilize injector powerup//
    delayMicroseconds(inj); //delay the calculated value  in microseconds//
    digitalWrite, 12, LOW; //shut down the injector//
  • 1
    please use the code button to format the code (select the code text and click the {} button) ... also indent the code properly
    – jsotola
    Feb 25, 2018 at 20:50
  • 2
    pulsing the hall effect pin and nothing happens ... hall-effect sensor is an input device. ... it is not an output device ..... do you have an actual circuit?
    – jsotola
    Feb 25, 2018 at 20:52
  • add the three sensors together and assign the value to inj - no, that's not what you're doing there. You're doing something else. Can you spot what it is?
    – Majenko
    Feb 25, 2018 at 21:15
  • @jsotola I assume he means "pulsing" as in "waving a magnet around near it to trigger it".
    – Majenko
    Feb 25, 2018 at 21:49
  • 1
    Also asked at: forum.arduino.cc/index.php?topic=531656 If you're going to do that then please be considerate enough to add links to the other places you cross posted. This will let us avoid wasting time due to duplicate effort and also help others who have the same questions and find your post to discover all the relevant information.
    – per1234
    Feb 26, 2018 at 4:28

1 Answer 1


I can see two things wrong with your code at first glance. First is the kind of interrupt you're using:

attachInterrupt(digitalPinToInterrupt(interruptPin), shot, LOW);

A LOW interrupt keeps triggering over and over again all the time the input is low. It's a special interrupt type that is seldom used for anything except waking up the MCU or handling external peripherals with persistent interrupt indication. Instead you should be using FALLING which will trigger once and once only when the input transitions from HIGH to LOW.

attachInterrupt(digitalPinToInterrupt(interruptPin), shot, FALLING);

Secondly you have two variables that seem to be intended to do the same thing - store the delay time for your output pulse. And that's caused confusion:

injValue = (tpsValue + mpValue + tuneValue) * 4; // add the three sensors together and assign the value to inj//

... and then later ...

delayMicroseconds(inj); //delay the calculated value  in microseconds//

In your comments you reference the second variable, but the code uses the first one. Change that so it uses just one (volatile) variable:

inj = (tpsValue + mpValue + tuneValue) * 4; // add the three sensors together and assign the value to inj//

Alternatively, and perhaps much safer, is to assign the result of the calculation inside a critical section, in which case having two variables is useful:

injValue = (tpsValue + mpValue + tuneValue) * 4; // add the three sensors together and assign the value to inj//
inj = injValue;

This will prevent your interrupt triggering whilst the value is being calculated and placed into the inj variable which could result in the inj variable containing gibberish, which could cause your engine to explode.

  • Thanks for the response. Silly mistakes, but my first time writing any code other really basic stuff. So to clarify. For this test setup I am pulsiing pin 2 high than low with the Attiny 1second on/off in place of the 2n2222 switched by the hall sensor. Pin 12 is connected to an LED, 1K resistor than ground. In place of the sensors A3,A4 and A5 are connected to +5V through a diode (to drop voltage to 4.2v) and 1K resistor. I made the changes suggested but pin 12 is still not getting triggered. I have tested the hardware parts and they are all working correctly. Hmmmm. Feb 25, 2018 at 23:16
  • Are you sure it's not getting triggered? It won't be triggered for long - you'd need an oscilloscope to see anything from it. Try making your delay much much longer instead of a few milliseconds at most.
    – Majenko
    Feb 25, 2018 at 23:18
  • Tried plugging (13000) into the delay instead of (inj) still not triggering the LED. Left my oscilloscope at work this weekend. Should be able to see 13ms flash though? Feb 25, 2018 at 23:31
  • If you're lucky. Try using delay(100) instead.
    – Majenko
    Feb 25, 2018 at 23:58
  • Tried using delay(100), no go. Flashed blink example program with the delays set to delayMicroseconds(1000) and can see the LED flash. Think i still have and error with my interrrupt. or maybe the way im asigning things at the start of the program. Is this correct. Feb 26, 2018 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.