Hey guys, here me again with another problem i couldn't solve. This time i made work almost everything but in this case i have the big problem of how to send the data that i receive from my artyx 7 FPGA, more especifically the data that send the adt7420 of the artyx 7, i can show the data of the adt7420 with the LCD, i can show the data of both sensors MQ7 and MG811, but the problem is i can't send the data of the ADT7420 to other arduino using NRF24L01, it sends me 0. I don't know where i am wrong so i come to you for help
Here is my code for the transmitter where the sensors and my FPGA is connected:
#include "CO2Sensor.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "MQ7.h"
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <avr/io.h>
#include <avr/interrupt.h>
//Declaramos las partes de temperatura
int integerPart;
float fractionalPart;
//Declaremos los pines CE y el CSN
const int pinCE = 9;
const int pinCSN = 10;
//creamos el objeto radio (NRF24L01)
RF24 radio(pinCE, pinCSN);
//Variable con la dirección del canal por donde se va a transmitir
// Single radio pipe address for the 2 nodes to communicate.
const uint64_t pipe = 0xE8E8F0F0E1LL;
//vector con los datos a enviar
float datos[4];
//Declaramos caracteristicas para el LCD
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
//Declaramos sensor CO2 MG811
CO2Sensor co2Sensor(A1, 0.99, 100);
//Declaramos pines sensor CO MQ7
#define A_PIN 2
#define VOLTAGE 5
// init MQ7 device
MQ7 mq7(A_PIN, VOLTAGE);
//Declaramos valores provenientes del FPGA y valor para interrupcion
volatile byte lowerByte = 0;
volatile byte upperByte = 0;
volatile bool dataReady = false;
void setup() {
//inicializamos el NRF24L01
radio.begin();
//Abrimos un canal de escritura
radio.openWritingPipe(pipe);
//Inicializamos el LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.setCursor(0, 1);
lcd.print("CO2:");
lcd.setCursor(9, 1);
lcd.print("CO:");
//Inicializamos los sensores
Serial.begin(9600);
co2Sensor.calibrate();
mq7.calibrate(); // calculates R0
// UART setup
unsigned int ubrr1 = F_CPU / 16 / 9600 - 1; // Calculate UBRR value for 9600 baud
UBRR1H = (ubrr1 >> 8);
UBRR1L = ubrr1;
UCSR1B = (1 << RXEN1) | (1 << RXCIE1); // Enable receiver and RX interrupt
UCSR1C = (1 << UCSZ11) | (1 << UCSZ10); // 8 data bits, no parity, 1 stop bit
}
void loop() {
int val = co2Sensor.read();
lcd.setCursor(4, 1);
lcd.print(val);
lcd.setCursor(12,1);
lcd.print(mq7.readPpm());
// Check if data is ready to be processed
if (dataReady) {
// Combine the two bytes into a 12-bit value
int combinedValue = (lowerByte << 4) | upperByte;
combinedValue &= 0x0FFF; // Mask to ensure only 12 bits
// Calculate integer and fractional parts
int integerPart = (combinedValue >> 4) & 0xFF; // The upper 8 bits
float fractionalPart = (combinedValue & 0x0F) * 0.0625; // The lower 4 bits multiplied by 0.0625
// Print the temperature on the LCD
lcd.setCursor(6, 0); // Move cursor to start of temperature display
lcd.print(integerPart);
lcd.print(".");
lcd.print(fractionalPart * 10000, 0); // Print fractional part as an integer (multiplied by 100 for 2 decimal places)
// Print other values
//int parteentera = integerPart;
//float partedecimal = fractionalPart;
// Reset the flag
dataReady = false;
}
//cargamos los datos en la variable datos[]
datos[0]=val;;
datos[1]=mq7.readPpm();;
datos[2]=integerPart;
datos[3]=fractionalPart;
// datos[2]=integerPart.fractionalPart;;
//enviamos los datos
bool ok = radio.write(datos, sizeof(datos));
if(ok)
{
Serial.print("Datos enviados: ");
Serial.print(datos[0]);
Serial.print(" , ");
Serial.print(datos[1]);
Serial.print(" , ");
Serial.print(datos[2]);
Serial.print(".");
Serial.println(datos[3] * 10000, 0);
}
else
{
Serial.println("no se ha podido enviar");
}
delay(1000);
}
// ISR for receiving UART data
ISR(USART1_RX_vect) {
static bool receivingUpperByte = false;
// Read the received byte from the USART Data Register
byte receivedByte = UDR1;
if (!receivingUpperByte) {
// Store the lower byte
lowerByte = receivedByte;
receivingUpperByte = true;
} else {
// Store the upper byte and set the flag to indicate data is ready
upperByte = receivedByte;
receivingUpperByte = false;
dataReady = true;
}
}
And here is the code for my receiver where is connected the NRF24L01 and a LCD:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
//Declaremos los pines CE y el CSN
const int pinCE = 9;
const int pinCSN = 10;
//Variable con la dirección del canal que se va a leer
const uint64_t pipe = 0xE8E8F0F0E1LL;
//creamos el objeto radio (NRF24L01)
RF24 radio(pinCE, pinCSN);
//vector para los datos recibidos
float data[4];
//Declaramos caracteristicas para el LCD
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
//inicializamos el NRF24L01
radio.begin();
//Abrimos el canal de Lectura
radio.openReadingPipe(1, pipe);
//empezamos a escuchar por el canal
radio.startListening();
//Inicializamos el LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.setCursor(0, 1);
lcd.print("CO2:");
lcd.setCursor(9, 1);
lcd.print("CO:");
}
void loop() {
//if ( radio.available(&numero_canal) )
if ( radio.available() )
{
//Leemos los datos y los guardamos en la variable datos[]
radio.read(data, sizeof data);
Serial.print("CO2= " );
Serial.print(int(data[0]));
Serial.print("CO= " );
Serial.print(data[1]);
Serial.print("Temperatura: ");
Serial.print(data[2]);
Serial.print(".");
Serial.println(data[3] * 10000, 0);
//reportamos por el LCD los datos recibidos
}
lcd.setCursor(4, 1);
lcd.print(int(data[0]));
lcd.setCursor(12,1);
lcd.print(data[1]);
lcd.setCursor(6, 0); // Move cursor to start of temperature display
lcd.print(data[2]);
lcd.print(".");
lcd.print(data[3] * 10000, 0); // Print fractional part as an integer (multiplied by 100 for 2 decimal places)
delay(1000);
}
I appreciate your help