Comunication API

I made a small weather station with two SX1276, simple hardware no problem.The receiver is driven by an Arduino UNO.I wanted to apply an SD card on this to record data but knowing that it could conflict with LoRa SX1276 which uses the same pin I looked up and saw that another CS pin could be assigned to the SD via software. Found a solution on the forum I made the sketch but it doesn't work. It tells me that the SD card is active and when it waits to receive the data it nails them and does not go forward.

#include <Vcc.h>

const float VccMin = 0.0; // Minimum expected Vcc level, in Volts.
const float VccMax = 5.0; // Maximum expected Vcc level, in Volts.
const float VccCorrection = 1.0/1.0; // Measured Vcc by multimeter divided by reported Vcc

Vcc vcc(VccCorrection);

#include <SimpleDHT.h>
SimpleDHT22 dht22(A0);

#include <Wire.h>
#include <SPI.h>
#include <LoRa.h>
#include <SD.h> // SD library

// Used for software SPI
#define LoRa_CLK 13
#define LoRa_MISO 12
#define LoRa_MOSI 11
// Used for hardware & software SPI
#define LoRa_CS1 10

#define SD_SCK 13
#define SD_MISO 12
#define SD_MOSI 11
#define SD_CS2 8

// software SPI
//LoRa = (LoRa_CS1, LoRa_MOSI, LoRa_MISO, LoRa_CLK);
//SD sd(SD_CS2, SD_MOSI, SD_MISO, SD_SCK); // software SPI

int CS1= 10;
int CS2= 8;

File file; // file object that is used to read and write data

void setup() {

Serial.begin(9600);

float temperature = 0;
float humidity = 0;

dht22.read2(&temperature, &humidity, NULL);

Serial.println(" DATI INTERNO");
Serial.println("");
Serial.print("Temperature: ");Serial.print ((float)temperature); Serial.println(" C");
Serial.print("Humidity: ");Serial.print((float)humidity); Serial.println(" RH%");
Serial.println("");

delay(1000);

float v = vcc.Read_Volts();
Serial.print("VCC = ");
Serial.print(v);
Serial.println(" Volts");

float p = vcc.Read_Perc(VccMin, VccMax);
Serial.print("VCC = ");
Serial.print(p);
Serial.println(" %");
Serial.println("");

LoRa.begin(CS1);

if (!LoRa.begin(CS1)){
Serial.println("Starting LoRa failed!");
while (1);
}
Serial.println("LoRa Receiver waiting...");

delay(1000);
}

void loop() {

LoRa.begin(CS1);
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
Serial.println("Received packet: ");
// read packet

while (LoRa.available()) {
Serial.print((char)LoRa.read());
//LoRa.end();
}
delay(10);
}
}
void writeFile() //writing something to the SD card
{

if (!SD.begin(CS2)) { // Initialize SD card

Serial.println("Errore SD");
return;
}
Serial.println("SD pronta");
Serial.println("");

if (SD.exists("file.txt")) {
SD.remove("file.txt");

file = SD.open("file.txt", FILE_WRITE); // open "file.txt" to write data; make sure that you want to write in the same file that you created in the setup()
if (file) {
file.println((char)LoRa.read()); // write number to file; in this case, the temperature with 2 decimals precision
file.close(); // close file

} else {
Serial.println("Could not open file (writing).");
} } }

Can you edit your post to place your code between code tags - the </> symbol. It's explained in the "How to get the best out of this forum" pinned post.

At a quick glance, I would have thought that the LoRa.begin() and SD.begin() would only occur inside setup().

What do you mean by "nails them"?

I notice you don't call "writeFile()" from loop() so there will be no file writing.

It is also strange that you have three calls to "LoRa.begin(CS1)" (two in setup() and one in loop()).