Hello, this is my first time posting on the forum. So, i'm trying to make an SPI injection for a Trabant engine. I have never programmed before so i do not have extended programming skills. That's the code i have written till now
int injector = 13; //pin injector
int ledrosu = 3; // pin program
int hallinjectie = A0; //pin semnal hall injectie
int pompa = 4; // pompa relay signal
volatile int rpmcount = 0;
int rpm = 0;
unsigned long lastmillis = 0;
int hallState=0;
void setup() {
pinMode(injector,OUTPUT);
pinMode(ledrosu,OUTPUT);
pinMode(hallinjectie,INPUT);
pinMode(pompa,OUTPUT);
digitalWrite(pompa,HIGH);
Serial.begin(9600);
attachInterrupt(0,rpm_fan, FALLING);
}
void loop(){
hallState = analogRead(hallinjectie);
if (hallState == LOW) {
digitalWrite(injector, HIGH);
delay(4);
}
else {
digitalWrite(injector, LOW);}
if (millis() - lastmillis == 1000){ /Uptade every one second, this will be equal to reading frecuency (Hz)./
detachInterrupt(0);//Disable interrupt when calculating
rpm = rpmcount * 60; /* Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/
Serial.print("RPM =\t"); //print the word "RPM" and tab.
Serial.print(rpm); // print the rpm value.
Serial.print("\t Hz=\t"); //print the word "Hz".
Serial.println(rpmcount); /print revolutions per second or Hz. And print new line or enter./
rpmcount = 0; // Restart the RPM counter
lastmillis = millis(); // Uptade lasmillis
attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
}
}
void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
rpmcount++;
}
It works fine till now but, when i'm triyng to spin the magnet around both hall effect sensors, the serial display gets blocked immediatly showing just one value. After that it won't work neither separatly. To make it work again i have to unplug the power source from the board.
P.s. i know i'm new into programming and building an ecu is a very hard project but this is what i wanna learn even for the future, so i will make almost any effort to learn.