Hello, I'm having a problem with interrupts (I always had it when I try to used it in the past).
My system has an arduino UNO, three peripherals (SD, RTC and thermocouple(MAX 675) ) and a Flyport WIFI.
The main program (or loop) read temperatures and write them with the date and time in the SD. This part works fine.
But I've to integrate the openpicus Wifi module (Or FLYPORT) to send the data stored at the SD to my server via HTTP. To do this I'm using the UART so I've made a simple protocol to communicate them.
The flyport module turns on a signal which is wired to Arduino's digital Pin 2 (interrupt 0) and the interrupt sets "true" a (volatile) variable.
When this variable is high at the beginning of Loop() the program should enter in the first if (you can see it in the code) but, I don't have any idea why, the arduino enter in the interrupt when he wants... Some times he does, another doesn't.
Here is my code (without the funcions I'm using cause they work and there are a lot of them):
#include <SPI.h>
#include <stdlib.h>
#include <SD.h>
//Variables generales
float temperatura_1;
float suma_temp;
float media_temp;
volatile boolean Peticion_flyport=false;//Se utiliza en la interrupcion
File archivo;
//Declaración de puertos
const byte CS_temp=A0; //cs para el max675
const byte CS_SD=10; //cs para la SD
const byte CS_RTC=9; //cs para el reloj
const byte CS_temp2=A1;
const byte CS_temp3=A2;
const byte RL_ON=3; //Bobina de rele que da tensión a los perifericos
const byte flyport_signal=2; //Señal que nos envia el flyport para pedir dato
/*******************************************************************************
SETUP
*******************************************************************************/
void setup()
{
Serial.begin(57600);//Inicio el puerto serie (RX TX)
Serial.println(F("Iniciando..."));
//Inicio pines de cs
pinMode(CS_temp,OUTPUT);//Selecciono el cs de la temperatura como salida
pinMode(CS_RTC,OUTPUT); //Selecciono el cs del RTC como salida
pinMode(CS_SD,OUTPUT); //Selecciono el cs de la SD
pinMode(CS_temp2,OUTPUT);
pinMode(CS_temp3,OUTPUT);
pinMode(flyport_signal,INPUT);//Selecciono como entrada el pin de señal de peticion de dato del flyport
pinMode(RL_ON,OUTPUT);
digitalWrite(RL_ON,HIGH);
//Inicio SPI-------------------
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
//------------------------------
/************************************************
DEBUGGING
**************************************************
//Serial.println("Reiniciando...");
delay(1000);
//Serial.println("Iniciado");
****************************************************/
IniciarReloj();
Serial.println(F("Reloj iniciado"));
//Iniciar_hora_RTC();
SetTimeDate(06,05,14,11,32,40);
delay(1000);
Serial.println(F("Hora actual del reloj"));
Serial.println(ReadTimeDate());
delay(1000);
Serial.println(F("Iniciamos la SD"));
IniciarSD();
Serial.println(F("Tarjeta iniciada"));
if (!SD.exists("Conf.txt")) //Si el archivo de configuracion no existe lo creamos y rellenamos
{
archivo =SD.open("Conf.txt", FILE_WRITE);
archivo.println(F("Firmware V 1.0")); //<--------------------------------CONFIGURACION
archivo.print(F("Fecha de ultima carga: "));
archivo.print(__DATE__);
archivo.print(F(" "));
archivo.println(__TIME__);
archivo.close();
Serial.println(F("Creado archivo de configuracion"));
archivo =SD.open("Conf.txt",FILE_READ);
Serial.println(F("Archivo de Configuracion: "));
while (archivo.available())
{
Serial.write(archivo.read());
}
archivo.close();
}
else //Si el archivo de configuracion existe lo leemos
{ archivo =SD.open("Conf.txt",FILE_READ);
Serial.println(F("Configuracion:"));
while (archivo.available())
{
Serial.write(archivo.read());
}
archivo.close();
}
if (!SD.exists("Medidas.txt")) //Si el archivo de Medidas no existe lo creamos
{
archivo =SD.open("Medidas.txt",FILE_WRITE);
archivo.close();
Serial.println(F("Creado archivo de medidas"));
}
Serial.println(F("Setup finalizado con exito"));
attachInterrupt(0,peticion_dato,RISING);//Cuando hay un flanco ascendente en la señal debemos enviar un OK\r\n
//attachInterrupt(0,,FALLING);//Cuando hay un flanco descendente en la señal debemos enviar el siguiente dato o un FIN\r\n
Peticion_flyport=false; //Si no hago esto entra siempre en el bucle :S
}
/***********************************************************************************
***********************************************************************************/
/***********************************************************************************
BUCLE CONTINUO DE PROGRAMA
***********************************************************************************/
void loop()
{
if (Peticion_flyport==true)
{
if(!SD.exists("Medidas.txt"))
{
Serial.println("FIN");
archivo.close();
Peticion_flyport=false;
loop();
}
else //Si el archivo existe lo abrimos y empezamos a leer.
{
archivo=SD.open("Medidas.txt",FILE_READ);
//Empiezo a leer necesitaré un bucle aqui
while (archivo.available())
{
Serial.println("OK");//Añade \r\n al final
delay(500);//Le doy tiempo a borrar su buffer RX
//Para leer haré:
char dato_leido=' ';
while (dato_leido!=0x0A)
{
dato_leido=archivo.read();
Serial.write(dato_leido);//le envio el dato
}
Peticion_flyport=false;
delay(100);//Espero 100 ms
int espera=0;//Esta variable me permite salir si hay algun problema de este bucle<----------------------***********************
while(Peticion_flyport==false)//Espero a que me haga otra petición o a que pasen 15 seg
{
delay(200);//espero 200 ms
espera++;
//CODIGO EN CASO DE NO RESPUESTA DEL FLYPORT
if (espera==75)
{
Serial.println("Se ha pasado del tiempo");
archivo.close();
loop();//Si tarda mas de 15 seg sale del bucle y vuelve al inicio del bucle
}
//--------------------------------------------
}
}
//Cuando ya acabo enviamos un FIN
Serial.println("FIN");
archivo.close();
Peticion_flyport=false;
}
}
else
{
suma_temp=0;
//Leo una temperatura cada segundo durante 10 segundos y le hago la media aritmetica
// SPI_Temp(); //Funcion que configura el SPI para el MAX
for (int i=0; i<10; i++)
{
temperatura_1=LeerTemp(CS_temp);//Leo la temperatura proporcionada por el sensor
suma_temp +=temperatura_1;
delay(1000);
}
media_temp=suma_temp/10;
Serial.println(F("Media de ultimas 10 temperaturas"));
/**Convierto la temperatura en string
char media_string[5];
dtostrf(media_temp,4,2,media_string);
*/
Serial.print(media_temp);
Serial.print(" ");
String tiempo = ReadTimeDate();
Serial.println(tiempo);
SPI_SD(); //Funcion que configura el SPI para la SD
archivo = SD.open("Medidas.txt",FILE_WRITE);
// -----------------------------------------------------------------------------------NECESITO CARD DETECT PARA SABER SI PUEDO O NO ESCRIBIR
if (archivo) //Compruebo si se abrio el archivo
{
Serial.println(F("Ecribiendo en la SD"));
archivo.print(media_temp);
archivo.print(" ");
archivo.println(tiempo);
archivo.close();
Serial.println(F("Dato escrito correctamente"));
}
else
{
Serial.println(F("Error al abrir el archivo Medidas.txt"));
}
}
}
/**********************************************************************************
INTERRUPCIONES
**********************************************************************************/
void peticion_dato(void)
{
Peticion_flyport=true;//El sistema flyport me pide un dato
}
I hope you can help me cause it's my first Arduino program and I've a lot of doubts which I'm solving in this community.
PD: English isn't my first language so I'm sorry if there are some faults in my post ![]()