Hola, estoy haciendo un sistema de encendido automático para un generador a gasolina. Consigo encenderlo pero no que se apague. Uso un arduino uno.
Este es mi código:
//para interrupción botón:
volatile int contador = 0;
int n = contador;
long T0 = 0;
//para interrupción parada:
volatile int count = 0;
int a = count;
//Medir corriente y potencia AC
const float FACTOR = 30; //30A/1V
const float multiplier = 0.0625F;
uint16_t samples[NUMSAMPLES];
void setup() {
Serial.begin(9600);
ads.setGain(GAIN_TWO); // ±2.048V 1 bit = 0.0625mV
ads.begin();
lcd.begin(20, 4); // initialize the lcd for 20 chars 4 lines
lcd.backlight(); // backlight on
analogReference(EXTERNAL);
digitalWrite(rele1, HIGH);
digitalWrite(rele2, HIGH);
digitalWrite(rele4, HIGH);
digitalWrite(enablePin, HIGH);
pinMode(rele1, OUTPUT);
pinMode(rele2, OUTPUT);
pinMode(rele4, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(PinTrig, OUTPUT);
pinMode(PinEcho, INPUT);
pinMode(interruptPin, INPUT);
// Activar comparador para 0.01V en canal 0
// (Donde 13333 = 10 / 0.0625F)
ads.startComparator_SingleEnded(0, 0); //habrá que comprobar si es necesario
// Inicializamos el array para el control de nivel de gasolina
for (int i = 0; i < numLecturas; i++)
{
lecturas[i] = 0;
}
//attachInterrupt(digitalPinToInterrupt(buttonPin), interruptButton, FALLING);
enableInterrupt(buttonPin, interruptButton, FALLING);
disableInterrupt(interruptPin);
}
void button()
{ if (n != contador)
{
rutinaBoton();
n = contador ;
}
}
void interruptButton()
{
if (millis() > T0 + 50)
{
noInterrupts();
contador++;
T0 = millis();
//attachInterrupt(digitalPinToInterrupt(interruptPin), interruptCount, FALLING);
//detachInterrupt(digitalPinToInterrupt(buttonPin));
enableInterrupt(interruptPin, interruptCount, RISING);
disableInterrupt(buttonPin);
interrupts();
}
}
void rutinaBoton()
{
step(true, 600);
digitalWrite(rele1, LOW); // activamos rele1: CONEXION GENERADOR-CASA
for (int z = 0; z < 3; z++)
{
digitalWrite(rele4, LOW); // activamos rele4: ENCENDIDO GENERADOR
delay(3000);
digitalWrite(rele4, HIGH); // desactivamos rele4
delay(100);
}
step(false, 600);
}
//Método para el motor
void step(boolean dir, int steps) {
digitalWrite(enablePin, LOW);
digitalWrite(dirPin, dir);
for (int k = 0; k < steps; k++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(800);
digitalWrite(stepPin, LOW);
delayMicroseconds(800);
}
digitalWrite(enablePin, HIGH);
}
void parada()
{
if (n != count)
{
rutinaApagadoGen;
a = count;
}
}
void interruptCount()
{
noInterrupts();
count++;
//attachInterrupt(digitalPinToInterrupt(buttonPin), interruptButton, FALLING);
//detachInterrupt(digitalPinToInterrupt(interruptPin));
enableInterrupt(buttonPin, interruptButton, FALLING);
disableInterrupt(interruptPin);
interrupts();
}
void rutinaApagadoGen()
{
digitalWrite(rele2, LOW); // activamos rele2 y rele3: APAGAMOS GENERADOR
delay(100);
digitalWrite(rele2, HIGH); // desactivamos rele2 y rele3
digitalWrite(rele1, HIGH); // desactivamos rele1: CORTAMOS CONEXION GENERADOR-CASA
delay(100);
}
void impresionCorriente()
{
float currentRMS = getCorriente();
float power = 230 * currentRMS;
lcd.setCursor(0, 0);
printMeasure("Amp:", currentRMS, " ");
lcd.setCursor(10, 0);
printMeasure("Wat:", power, "");
}
float getCorriente()
{
float voltage;
float corriente;
float sum = 0;
long tiempo = millis();
int counter = 0;
while (millis() - tiempo < 1000)
{
voltage = ads.readADC_Differential_0_1() * multiplier;
corriente = voltage * FACTOR;
corriente /= 1000.0;
sum += sq(corriente);
counter = counter + 1;
}
corriente = sqrt(sum / counter);
return (corriente);
}
void printMeasure(String prefix, float value, String postfix)
{
lcd.print(prefix);
lcd.print(value, 3);
lcd.print(postfix);
}
void impresionBateria()
{
lcd.setCursor(0, 2);
lcd.print("Bat: ");
lcd.print(voltaje);
lcd.print(" V. ");
lcd.setCursor(14, 2);
lcd.print(porcent);
lcd.print(" %");
}
void impresionGasolina()
{
// Solo mostramos si hemos calculado por lo menos una media
if (primeraMedia)
{
float distanciaLleno = distanciaVacio - media;
float cantidadLiquido = distanciaLleno * 5.755 / distancia55;
int porcentaje = (int) (distanciaLleno * 139 / distanciaVacio);
// Mostramos en la pantalla LCD
// Cantidad de líquido
lcd.setCursor(0, 3);
lcd.print("Gas " + String(cantidadLiquido) + " lts");
// Porcentaje
lcd.setCursor(15, 3);
lcd.print(String(porcentaje) + " %");
}
}
void impresionTemp()
{
uint8_t i;
float average;
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(10);
}
// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
Serial.print("Average analog reading ");
Serial.println(average);
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
Serial.print("Thermistor resistance ");
Serial.println(average);
float steinhart;
steinhart = average / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
Serial.print("Temperature ");
Serial.print(steinhart);
Serial.println(" *C");
lcd.setCursor(0, 1);
lcd.print("TempGen " + String(steinhart) + " " + ((char)223) + "C");
delay(1000);
}
void refrescoLCD()
{
tAhora = millis(); // Comprobamos para refrescar el LCD
if ( tAhora - tAntes >= tEjec)
{ // cada 10 segundos
tAntes = tAhora; // actualizamos variables
lcd.clear(); // Refrescamos
}
}
// The loop function is called in an endless loop
void loop()
{
//para ver por pantalla la lectura del ads1115.
int16_t adc0;
adc0 = ads.getLastConversionResults();
// lcd.setCursor(17, 1);
//lcd.print(adc2);
Serial.println(adc0);
button();
gasolina();
bateria();
impresionCorriente();
impresionBateria();
impresionGasolina();
impresionTemp();
refrescoLCD();
parada();
}
He tenido que recortar el código porque no entraba. espero que sea suficiente
Agradezco cualquier sugerencia.
Gracias
Xosé