Hi there
Im new in the Arduino family but very entusiastic
Im running a sketch in order to simulate the fuel inyection and spark control of a system using the sensors (temperature, pressure, HEI module) etc from my old car.
The "polling" part from the analog read of the sensors runs smooth
I also made a "PANTALLA()" (display, im latin speaker) function to display in a 16x2 LCD my 4 variables (MAP, ECT, TPS, and RPM)
Problem is, now I got to the digital part of the ecuation.
I was reading documentation about using the interrupt pin and ISR function to measure RPM (from a PC fan) for example.
I got that running
However, if I enable the "PANTALLA()" function (remember is the function that runs the display) the RPM variable does not show on the serial monitor (as the code copied runs inside the if(milis() function)
I also tried to make the display take the global variable RPM (wich the ISR has part in modifing) and it always shows 0
If I put the "PANTALLA()" function INSIDE the code running and calculating the RPM taking the rpmcounts++ of the ISR() I got the value in the display but refreshing at the interrupts rate.
Any ideas what I may be missing here? It seems like the RPM variable is constantly reset to 0 or like the global variable only works inside the function if(millis()....) and once the function ends it´s reseted to 0 again
Here is a bit of my code
//RPM
volatile int rpmcount = 0;
int RPM = 0;
unsigned long lastmillis = 0;
//RPM
//CHK_Tiempo_Inyeccion = pulseIn(pinINJ, LOW); muestra microsegundos, para ver tiempo de inyeccion
/*** FUNCIONES ARDUINO ***/
void setup()
{
Serial.begin(9600);
lcd.begin (16,2); // Inicializar el display con 16 caraceres 2 lineas
attachInterrupt (digitalPinToInterrupt(2), rpm_fan, FALLING);
//Pines Analogicos
pinMode(A0,INPUT); //MAP
pinMode(A1,INPUT); //TPS
pinMode(A2,INPUT); //ECT
pinMode(A3,INPUT); //CO2
/pinMode(6,INPUT);
pinMode(7,INPUT);
pinMode(5,OUTPUT); //Posible Señal HEI/
//Pines Digitales
pinMode(9,OUTPUT); //Salida Inyector PWN
pinMode(12,OUTPUT); //Salida Rele 1 ELECTROVENTILADOR
pinMode(13,OUTPUT); //Salida Rele 2
}//Cierra funcion Setup
void loop()
{
if (millis() - lastmillis == 1000){ /Uptade every one second, this will be equal to reading frecuency (Hz)./
detachInterrupt(digitalPinToInterrupt(2)); //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.*/
S
erial.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(digitalPinToInterrupt(2), rpm_fan, FALLING); //enable interrupt
}// Cierra if millis()
int MAP=analogRead(A0); // Cable Azul 0.5V Cerrada 1V arranca a acelerar 4.5V Abertura Maxima
int TPS=analogRead(A1); // Cable verde
int ECT=analogRead(A2); // Cable Amarillo
int CO2=analogRead(A3); // Cable naranja
TEMP = NTC10K(ECT);
PRESION = map(MAP,245,1023,600,0);//600 1,2 0 4,8
ACCEL = map(TPS,124,1023,100,0);
VENT(); //Llama funcion para prender electro si temp supera 50
PANTALLA(); // Maneja display
}// Cierra funcion Loop
/*** FUNCIONES ***/
void PANTALLA(){ //Funcion que controla display y variables
lcd.print("MAP ECT TPS RPM");
lcd.setCursor ( 0, 1 );
lcd.print(String(PRESION));
lcd.setCursor ( 4, 1 );
lcd.print(String(TEMP));
lcd.setCursor ( 8, 1 );
lcd.print(String(ACCEL));
lcd.setCursor ( 12, 1 );
lcd.print(String(RPM));
delay(300);
lcd.clear();
}//Cierra PANTALLA
void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
rpmcount++;
}// Cierra rpm_fan()