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

