Adding a output limiter

hi there i am a complete arduino noob just wondering if someone can point me in th right direction.
im trying to make a super simple ecu i have a made a few turbo prjects over the years just running draw through turbo setups and have had the urge to make simple ecu's for them there all single cylinder applications so nothing too difficult this is the code i have atm its a 122hz output for the injector with pulse width on a pot(throttle) and the spark is a timming curved output the issue i have atm is a way to limit this output *rev limmter

basically in that code im seeing if i can count "Trig" input pulses in hz and set a max freq on my "SCR" output.
eg if "Trig" is 145hz (8750rpm) = "SCR" LOW ,until "Trig" 0<145hz then "SCR" high
im just not sure how to intergrate this into my code

#include "LiquidCrystal.h"
#include <math.h>
const int voltageSensor = A1;

int a;
int b;
int pwm_pin = 10;
int buttonState = 0;
int Trig = 2;
int SCR = 11;
int deg_retard =0;
long spark_time = 0;
long micro_wait;
unsigned long micro_per_deg;

//rpm stuff
volatile byte trigger_count;
unsigned long rpm;
unsigned long timeold;

float vOUT = 0.0;
float vIN = 0.0;
float R1 = 30000.0;
float R2 = 7500.0;
int value = 0;

LiquidCrystal lcd(7, 6, 5, 4, 3, 1); // RS, E, D4, D5, D6, D7




void setup()
{
pinMode(Trig, INPUT);
pinMode(SCR, OUTPUT);
attachInterrupt(digitalPinToInterrupt(Trig), pulse, FALLING);

Serial.begin(9600); Serial.println("Serial started");
//Serial.begin(9600);
lcd.begin(16, 2);
lcd.print(" boostcaboose  ");
delay(2000);
lcd.begin(16, 2);
lcd.print(" @6.5psi  ");
delay(2000);
lcd.begin(16, 2);
lcd.print(" air/fuel mix  ");
delay(2000);


pinMode(pwm_pin, OUTPUT);
TCCR1B = TCCR1B & B11111000 | B00000100;


}
void loop()
{



  //print out the value of the pushbutton



value = analogRead(voltageSensor);
vOUT = (value * 150.0) / 1024.0;
vIN = vOUT / (R2 / (R1 + R2));
//Serial.print("Input = ");
//Serial.println(vIN);
lcd.setCursor(0 , 0);
lcd.print("    a/f=           ");
lcd.setCursor(9, 0);
lcd.print(vIN);
delay(500);

a = analogRead(A0);
b = map(a, 0, 1023, 0, 255);
analogWrite(pwm_pin, b);

if (trigger_count > 0) {
micro_per_deg = (micros() - timeold) / 180;
timeold = micros();
trigger_count = 0;

if (micro_per_deg >= 37) {
deg_retard = 1;
}
if (micro_per_deg < 37 && micro_per_deg >= 24) {
deg_retard = 5;
}
if (micro_per_deg < 24 ) {
deg_retard = 10;
}
//    if (micro_per_deg >= 37){deg_retard = 0;}
//    if (micro_per_deg < 37 && micro_per_deg >= 24){deg_retard = 0;}
//    if (micro_per_deg < 24 ){deg_retard = 0;}
micro_wait = deg_retard * micro_per_deg;
  }


}
void pulse()
{
trigger_count++;
fire();
}

void fire()
{
delayMicroseconds(micro_wait);
digitalWrite(11, HIGH);                         //sends transistor high
delayMicroseconds(350);                         //1 millisecond spark duration
digitalWrite(11, LOW);                          //Transistor goes low
}

this is my plan atm

what's an "ecu"?

what's a "turbo setup"?

? ? ? ? ? ?

@gcjr sorry was more of a backstory for my project, basically im that code im seeing if i can count "Trig" input pulses in hz and set a max freq on my "SCR" output.
eg if "Trig" is 145hz (8750rpm) = "SCR" LOW ,until "Trig" 0<145hz then "SCR" high
im just not sure how to intergrate this into my code

oh and a ecu is a electronic control unit- the controller in cars that control spark-fueling-ect.
and turbo setup is this just a backstory i turbo gokarts motorbikes ect like this

what are you trying to do?

don't pulses to a spark plug on a combustion engine need to be synchronized to the piston cycle?

if you want to prevent some "red-line" speed, could you simply suppress the spark pulse if the pulses are < that some minimum period (time between pulses, the inverse of frequency)?

yeah so my Trig input is run off a hall sensor off the crank, the output "SCR" fires a transistor high that signal is sent to the coil on plug (its a 12v feed coil with a 5v signal trigger) that fires the sparkplug, the timming all fine.
im not sure the best way to set a freq limit for the output for redline(rev limiter) so 8750rpm is my max, or 145hz im not sure if i count my trigger input and then hold my output pin low or put a rule limit on my output that wont let it excced 145hz.

doesn't look like the code is actually counting pulses, just using it to recognize when a pulse occurs.

there's no need to scale the time in usec by 180 to determine the delay (micro_wait). consider

        if (delta >= 37 * 180)
            deg_retard = 1;
        else if (delta >= 24 * 180)
            deg_retard = 5;
        else
            deg_retard = 10;

        micro_wait = deg_retard * delta;

the ISR, pulse(), could capture the timestamp and calculate the delay and conditionally call fire()

145Hz is a period of 6,900us.

void pulse()
{
  static unsigned long lastPulse;
  unsigned long now = micros();
  trigger_count++;
  if (now - lastPulse < 6900)
    digitalWrite(SCR, HIGH);
  else
    digitalWrite(SCR, LOW);
  lastPulse = now;
  fire();
}

thanks man,cheers for your help sorry for the lack of info still wrapping my head around this stuff first time programming im a electrician by trade was a bit out of my field with programming :sweat_smile:

oh this is awesome worked a treat in my simu thanks for your time, i really appreciate it

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.