I am fairly new to programming and Arduinos. For a project, I am using an Adafruit SD card module and an nRF24L01 together for a project that sends data between two nRF24L01's and then saves the data on the receiver end to the SD card module. I am able to get them to work separately, but when connected I am unable to use the code that I have for them together. For example, combining the code for sending the data and then storing the data, all within the loop causes the received data to not be sent at all. Thanks!
This is the code I have of them working separately, with one in the loop and the other in setup:
#include <SD.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 7 //
#define CSN_PIN 8 //chip select for rf
#define csPin 10 // chip select for SDCard
RF24 radio(CE_PIN, CSN_PIN); // rf ce,cs
const byte address[6] = "00001";
File myFile;
void setup()
{
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
Serial.print("Initializing SD card...");
// pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop()
{
if (radio.available()) {
char text[32] = "";
radio.read(&text, sizeof(text));
Serial.println(text);
}
}
I am not sure if this portion would help, but this is what I am sending with the transmitter:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
void setup() {
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop() {
const char text[] = "Hello World";
radio.write(&text, sizeof(text));
delay(1000);
}