Bonjour,
je me lance dans un projet: Il y a un premier Arduino qui est celui qui envoie le message et un deuxième qui le reçoit (par radio).
Le deuxième Arduino(celui qui reçoit le message), enregistre le message entrant dans la carte SD sous cette forme:
[msg1]
[msg2]
etc...
Grâce à deux boutons, on peut voir les différents messages. Jusqu’à maintenant pas trop de problème, mais maintenant, je voulais ajouter une fonction pour supprimer un message, et la c'est la panique, car quand je supprime un message, ça supprime la totalité des messages de la carte SD!!!!
Voici le code(un peu brouillon car en plein développement, dîtes moi si c'est illisible ;)):
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <SD.h>
static const unsigned char PROGMEM enveloppe[] =
{ B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B11111111,B11111111,B11111111,B11111111,B11111111,B11111110,
B11110000,B00000000,B00000000,B00000000,B00000000,B00001110,
B10001100,B00000000,B00000000,B00000000,B00000000,B00110010,
B10000011,B00000000,B00000000,B00000000,B00000000,B11000010,
B10000000,B11000000,B00000000,B00000000,B00000011,B00000010,
B10000000,B00110000,B00000000,B00000000,B00001100,B00000010,
B10000000,B00001100,B00000000,B00000000,B00110000,B00000010,
B10000000,B00000011,B00000000,B00000000,B11000000,B00000010,
B10000000,B00000000,B11000000,B00000011,B00000000,B00000010,
B10000000,B00000000,B00110000,B00001100,B00000000,B00000010,
B10000000,B00000000,B00001100,B00110000,B00000000,B00000010,
B10000000,B00000000,B00000011,B11000000,B00000000,B00000010,
B10000000,B00000000,B00000000,B00000000,B00000000,B00000010,
B10000000,B00000000,B00000000,B00000000,B00000000,B00000010,
B10000000,B00000000,B00000000,B00000000,B00000000,B00000010,
B10000000,B00000000,B00000000,B00000000,B00000000,B00000010,
B10000000,B00000000,B00000000,B00000000,B00000000,B00000010,
B10000000,B00000000,B00000000,B00000000,B00000000,B00000010,
B10000000,B00000000,B00000000,B00000000,B00000000,B00000010,
B10000000,B00000000,B00000000,B00000000,B00000000,B00000010,
B10000000,B00000000,B00000000,B00000000,B00000000,B00000010,
B10000000,B00000000,B00000000,B00000000,B00000000,B00000010,
B10000000,B00000000,B00000000,B00000000,B00000000,B00000010,
B10000000,B00000000,B00000000,B00000000,B00000000,B00000010,
B11111111,B11111111,B11111111,B11111111,B11111111,B11111110,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
};
typedef struct{
char body[32];
}Message;
String message = "";
int lettre = 0;
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
int nombre_msg = 0;
int msg_view = 2;
void setup() {
Serial.begin(9600);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
digitalWrite(A1,1);
digitalWrite(A2,1);
pinMode(2,OUTPUT);
digitalWrite(2,1);
display.begin();
display.setContrast(60);
display.setRotation(2);
display.clearDisplay();
display.display();
Mirf.cePin = 9;
Mirf.csnPin = 10;
Mirf.spi = &MirfHardwareSpi;
Mirf.init();
//definition de cannal utilisé ainsi que la taille max d'un msg
Mirf.channel = 1;
Mirf.payload = sizeof(Message);
Mirf.config();
Mirf.setTADDR((byte *) "nrf01");
Mirf.setRADDR((byte *) "nrf02");
Serial.println("init...");
if(!SD.begin(A0)){
display.clearDisplay();
display.println("SD erreur");
display.display();
}
}
void loop() {
if(Mirf.dataReady()){
for(int i = 0; i < 3;i++){
digitalWrite(2,0);
delay(500);
digitalWrite(2,1);
delay(500);
}
message = recupMsg();
Serial.println(message);
File liste_msg = SD.open("MESSAGE",FILE_WRITE);
liste_msg.print("[");
liste_msg.print(message);
liste_msg.println("]");
liste_msg.close();
}
else{
nombre_msg = compte_nb_msg();
accueil();
}
}
String recupMsg(){
String msg_complet = "";
while(1){
Message msg;
Mirf.getData((byte *) &msg);
String paquet = "";
for(int i = 0;msg.body[i] != '\0';i++){
paquet.concat(msg.body[i]);
}
Serial.print("[");
Serial.print(paquet);
Serial.println("]");
if(paquet == "stop")return msg_complet;
msg_complet.concat(paquet);
while(!Mirf.dataReady());
}
}
void read_msg(File liste_msg){
display.clearDisplay();
for(int i = 0; i < msg_view;i++){
liste_msg.readStringUntil(']');
}
liste_msg.readStringUntil('[');
display.println(liste_msg.readStringUntil(']'));
display.display();
liste_msg.close();
}
int compte_nb_msg(){
File liste_msg = SD.open("MESSAGE",FILE_READ);
int nombre = 0;
while(liste_msg.available()){
liste_msg.readStringUntil(']');
liste_msg.read();
liste_msg.read();
nombre++;
}
liste_msg.close();
return nombre;
}
void accueil(){
display.clearDisplay();
display.drawBitmap(17,5,enveloppe,48,27,BLACK);
display.setCursor(38,21);
display.println(nombre_msg);
display.drawLine(0,38,84,38,BLACK);
display.drawLine(42,38,42,48,BLACK);
display.drawLine(0,38,0,48,BLACK);
display.drawLine(83,38,83,48,BLACK);
display.setCursor(8,40);
display.println("Voir");
display.setCursor(59,40);
display.println("-");
display.display();
if(digitalRead(A1) == 0){
for(int i = 38; i< 48;i++){
display.drawLine(0,i,42,i,BLACK);
}
display.setCursor(8,40);
display.setTextColor(WHITE);
display.println("Voir");
display.display();
display.setTextColor(BLACK);
while(digitalRead(A1)==0);
aff_liste_msg();
}
if(digitalRead(A2) == 0){
for(int i = 38; i< 48;i++){
display.drawLine(42,i,84,i,BLACK);
}
display.setCursor(59,40);
display.setTextColor(WHITE);
display.println("-");
display.display();
display.setTextColor(BLACK);
while(digitalRead(A2)==0);
}
}
void aff_liste_msg(){
File liste_msg = SD.open("MESSAGE",FILE_READ);
for(int msg = 0;msg < nombre_msg;msg++){
liste_msg.readStringUntil('[');
display.clearDisplay();
String message = "";
message = liste_msg.readStringUntil(']');
message.replace(" ","\n");
display.println(message);
display.drawLine(0,38,84,38,BLACK);
display.drawLine(42,38,42,48,BLACK);
display.drawLine(0,38,0,48,BLACK);
display.drawLine(83,38,83,48,BLACK);
display.setCursor(5,40);
display.println("Suppr");
display.setCursor(59,40);
display.println(">");
display.display();
while(digitalRead(A2) == 1){
if(digitalRead(A1) == 0){
while(digitalRead(A1) == 0);
liste_msg.close();
suppr_msg(message);
return;
}
}
while(digitalRead(A2) == 0);
}
liste_msg.close();
}
void suppr_msg(String message_suppr){
File liste_msg = SD.open("MESSAGE",FILE_READ);
String message = "";
message = liste_msg.readString();
message_suppr.replace("\n"," ");
String message_replace = "";
message_replace = "[";
message_replace.concat(message_suppr);
message_replace.concat("]");
message.replace(message_replace," ");
Serial.println(message);
SD.remove("MESSAGE");
delay(10);
liste_msg = SD.open("MESSAGE",FILE_WRITE);
delay(100);
liste_msg.print(message);
liste_msg.close();
display.clearDisplay();
display.println("msg suppr!!!");
display.display();
delay(1000);
}
Merci d'avance!