A few days ago, I try arduino calculate rpm and delayed ignition in the event dwell 10 us it's normal, but I increased dwell = 1500 us it has not stabilized, I use the hall effect the sensor, use a fan to simulate the rotation of the engin and flash. light simulation timing light
I might improve it to make it stable. Please help me
This is my code
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte flash = 11;
int coilOut = 7; //Coil output on pin 7
int led = 13; //arduino led
int trigger = 3;
volatile byte rpmcount;
unsigned long rpm;
unsigned long timeold;
volatile int advance = 0; //testing stuff
int degree;
//** 800,850,900,950,1000,1050,1100,1150,1200,1250**\\
int delay_1[9]={8333,7843,7407,6666,6349,6060,5797,5555,5333};
void setup()
{
pinMode(trigger,INPUT);
attachInterrupt(0, rpm_cal, FALLING);
pinMode(coilOut, OUTPUT); //Coil output
Serial.begin(9600); //Initialize serial port
lcd.begin();
pinMode(flash, OUTPUT);
}
void loop()
{
rpmcount = 0;
Serial.println(rpm,DEC);
//Serial.println(advance,DEC);
//Serial.println(" RPM");
lcd_show();
speed_fan();
}
void lcd_show()
{
lcd.setCursor(0,0);
lcd.print(rpm);
lcd.setCursor(5, 0);
lcd.print("RPM");
lcd.setCursor(9, 0);
lcd.print(advance);
lcd.setCursor(11, 0);
//lcd.print(" deg");
delay(500);
lcd.clear();
// Clear LCD
unsigned long last_t;
if (rpm < 150)
{
if (millis()-last_t >= 50000ul)
{
rpm = 0; advance = 0;
last_t = millis();
}
}
}
void rpm_cal() //Interrupt routine run when tach pulse goes high
{
if (rpm <= 1200) // LOW RPM
{
table();
}
if (rpm >= 1250 ) // HIGH RPM
{
advance = map(rpm,1250,2400,5333,100) ;
if (advance < 76 )
{
advance = 76;
}
}
delayMicroseconds(advance);
digitalWrite (coilOut, HIGH); //Charge coil
delayMicroseconds(1500);
digitalWrite (coilOut, LOW);
rpm = (60000000ul)/(micros() - timeold);
timeold = micros(); //save current since-on microseconds
rpmcount++;
}
void speed_fan()
{
const byte POTENTIOMETER = 0;
int reading;
int value;
reading = analogRead(POTENTIOMETER);
value = map(reading, 0, 1024, 0, 255);
analogWrite(flash, value);
}
void table() // map advance for low rpm
{
if (rpm <= 800)
{
advance = delay_1[0];
}
if (rpm > 800 && rpm <= 850 )
{
advance = delay_1[1];
}
if (rpm > 850 && rpm <= 900 )
{
advance = delay_1[2];
}
if (rpm > 900 && rpm <= 950 )
{
advance = delay_1[3];
}
if (rpm > 950 && rpm <= 1000 )
{
advance = delay_1[4];
}
if (rpm > 1000 && rpm <= 1050 )
{
advance = delay_1[5];
}
if (rpm > 1050 && rpm <= 1100 )
{
advance = delay_1[6];
}
if (rpm > 1100 && rpm <= 1150 )
{
advance = delay_1[7];
}
if (rpm > 1150 && rpm <= 1200 )
{
advance = delay_1[8];
}
if (rpm > 1200 && rpm <= 1250 )
{
advance = delay_1[9];
}
}
Thank you.