hi guys
i'm quite a newbie, i have arduino from about 5 months and it is my first software axperience, i'm a nautical mechanic
i'm working on a arduino cdi for quite hold gasoline engines that still have mechanic ignition sistem to swap them to elecrtonic ignition
here the code wich i'm working on right now, it has some bugs but not a lot and not heavy... something isn't how it should be especially the lighting coils control but i'm working on a simblpe breadboard with just a little pc fan, hall sensor, magnet and 2 leds as stroboscopic light to check what the board is doing.... and it works!!
so i want to show it to the community to ask suggestions also if i have already posted it on the italian forum
i'm thinking on connect my raspberry via I2C with the arduino to show engine statistic in real time and change them
map() is just fon now, i will improve a sistem who will leave the user free to change the timing with more flexibility as the real cdi
#include <StopWatch.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(11, 10, 7, 6, 5, 4); //pin lcd
const byte led1 = 13; // cilindri 1 e 4
const byte led2 = 12; // cilindri 2 e 3
const byte pin2 = 2; // interrupt startt
const byte pin3 = 3; // interrupt stopp
volatile int statestartt = HIGH; // stato normale startt
volatile int statestopp=HIGH; // stato normale stopp
StopWatch sw_micros(StopWatch::MICROS); // timer durata dente
StopWatch sw_micros1(StopWatch::MICROS); // timer attesa 1 e 4
StopWatch sw_micros2(StopWatch::MICROS); // timer attesa 2 e 3
long time = 0; //valore durata dente microsecondi
long ritardo = 0; //gradi ritardo da fine dente
long tritardo1 = 0; //valore durata ritardo in microsecondi 1 e 4
long tritardo2 = 0; //valore durata ritardo in microsecondi 2 e 3
long rpm = 0; //numero giri
int gradidente = 40; //ampiezza dente in gradi
void setup() {
Serial.begin(9600); //comunicazione seriale
lcd.begin(16, 2); // comunicazione lcd
pinMode(led1, OUTPUT); //bobina 1 e 4
pinMode(led2, OUTPUT); //bobina2 e 3
pinMode(pin2, INPUT_PULLUP);
pinMode(pin3, INPUT_PULLUP);
attachInterrupt(0, startt, FALLING); //interrupt in inizio dente
attachInterrupt(1, stopp, RISING); //interrupt fine dente
}
void loop()
{
//se il dente è iniziato
if(statestartt == LOW){
sw_micros.start();//inizia a contare durata dente
statestartt=HIGH;// reimposta startt
}
//se il dente è finito
if(statestopp == LOW) {
time=sw_micros.elapsed(); //memorizza durata dente
sw_micros1.start(); //inizia a contare da fine dente per 1 e 4
sw_micros2.start(); //inizia a contare da fine dente per 2 e 3
//Serial.println(time); //stampa durata dente
statestopp = HIGH; //reimposta stopp
sw_micros.reset(); //azzera timer durata dente
rpm=60000000/((time/gradidente)*360); //calcolo rpm
ritardo=map(rpm, 600, 1800, 70, 100); //determina ritardo
ritardo = constrain(ritardo, 70, 100); // mantieni il ritardo entro l'intervallo prestabilito
Serial.print(rpm); //stampo rpm
Serial.print(" ");
Serial.println(ritardo);
lcd1(); //void lcd
tritardo1=(time/gradidente)*ritardo; //calcola durata ritardo in microsecondi per 1 e 4
tritardo2=(time/gradidente)*(ritardo+180); //calcola durata ritardo in microsecondi per 2 e 3
}
//se il tempo che è passato dalla fine del dente è maggiore della durata del ritardo
if(sw_micros1.elapsed() >= tritardo1){
sw_micros1.reset(); //azzera timer ritardo
digitalWrite(led1, HIGH); //accendi led
delayMicroseconds(500);
digitalWrite(led1, LOW); //spegni led
}
//se il tempo che è passato dalla fine del dente è maggiore della durata del ritardo
if(sw_micros2.elapsed()>=tritardo2){
sw_micros2.reset(); //azzera timer ritardo
digitalWrite(led2, HIGH); //accendi led
delayMicroseconds(500);
digitalWrite(led2, LOW); //spegni led
}
} // End Loop
void startt()//interrupt startt
{
statestartt = !statestartt; //inverti startt
}
void stopp()//interrupt stopp
{
statestopp = !statestopp; //inverti stopp
}
void lcd1(){
lcd.setCursor(0, 0); //riga nomi
lcd.print("Rpm BTDC"); // stampa nomi
lcd.setCursor(0, 1); //riga dati
lcd.print(rpm); //stampa rpm
lcd.setCursor(4, 1);
lcd.print(" ");
lcd.setCursor(9, 1);
lcd.print(ritardo); //stampa ritardo
}