Thank you for reading this
I am quite new to arduino
I want to send SD card content in batches over I2C to a master to control motors with encoders
I2c is using Union buffer with 4 bytes unsigned long to send data
According to Arduino ref reading SD card to " readBytesUntil" will put answer to a buffer and the I2C sends from buffer
This is the slave code which will not compile on Uno
"No matching function for call to ‘SDLib File readBytesUntil(char)’ "
"buffer.longNumber=myfile.redBytesUntil(’,’); ".
#include <SD.h>
//SD card libbary
#include <SPI.h>
#include <Wire.h> //I2C libbary
union Buffer{ //union bufffer same as master
unsigned long longNumber;
byte longBytes[4];};
int LED=13;
Buffer buffer;
// Buffer2 buffer;
// Buffer3 buffer;
// Buffer4 buffer;
int x=0; //int to receive byte from mast to put into switch case
const int chipSelect=4;
char amount1=0;
char amount2=0;
char amount3=0;
char amount4=0;
int thisNum=0; //count for Sd card
void setup() {
// put your setup code here, to run once:
pinMode(LED,OUTPUT);
Wire.begin(4); //I2C slave address 4
Wire.onRequest(requestEvent); //function info requested by master
Wire.onReceive(receiveEvent); // function recived info from master
Serial.begin(9600);
Serial.println("Initializing SD card...."); //set up SD card
if(!SD.begin(chipSelect)){
Serial.println("Card Failed");
while(1);}
Serial.println("SD card ok");
}
void requestEvent(){
Wire.write(buffer.longBytes,4);
digitalWrite(LED,!digitalRead(LED)); //led on pin13 flashes as date is transmited
}
void receiveEvent(){ //function to set switch case number
while(Wire.available()){
x=Wire.read();
}
Serial.print(x); //print which switch case it is in
Serial.println("..X");
}
void loop(){
switch (x){
File myfile=SD.open("tested.txt"); //open SD card "tested txt" file
if(myfile){
Serial.println("reading tested txt");
while(myfile.available()){ //while myfile available
receiveEvent(); //function to set switch case number same as master
case 0:
if(thisNum=1){
buffer.longNumber=myfile.readBytesUntil('\,'); //read Byte until carage return or ,from Sd card myfile
Serial.print(buffer.longNumber,HEX); //and send to buffer for I2C longNumber
Serial.println("..LONGnumber HEX"); } //and print longNumber
thisNum++;
break;
case 1:
if(thisNum=2){ //same as above with next piece of info from SD card
buffer.longNumber=myfile.readBytesUntil('\,'); //max lenght 0x plus 8 digits
Serial.print(buffer.longNumber,HEX);
Serial.println("LongNumber HEX");
}
thisNum++;
break;
case 2:
if(thisNum=3){
buffer.longNumber=myfile.readBytesUntil('\,');
Serial.print(buffer.longNumber,HEX);
Serial.println("LongNumber HEX");
}
thisnum++;
break;
case 3:
if(thisNum=4){
buffer .longNumber=myfile.readBytesUntil('\,');
Serial.print(buffer.longNumber,HEX);
Serial.println("LongNumber HEX");
}
thisNum=0;
break;
}}}}
The Master code seems ok . The slave works when I don’t have the SD card conneccted and transmit
4 arbitrary numbers such as 0x346. The master will then print 346 in the appropriate switch case.
I also have a working code that will print the SD card content in batches of 4.
I can show these codes if it is helpful
Master code is written on Nano 33ble
#include <Wire.h>
int x=0; //switch case number
int amount1=0;
union Buffer{ //union buffer
unsigned long longNumber; //union longNumber to store incoming data
byte longBytes[4]; //union longByte incoming data
};
Buffer buffer;
void setup() {
// put your setup code here, to run once:
Wire.begin(); //I2C begin
Serial.begin(9600); //Serial begin at 9600 baud
}
void wiretransmit(){ //function transmit switch case number
Wire.beginTransmission(4); //I2c transmit to slave address 4
Wire.write(x); //transmit switch case number
Wire.endTransmission(); //transmit and end
}
void loop(){
switch (x){ //switch case x
case 0:
wiretransmit();
Wire.requestFrom(4,4); //request 4 bytes from slave4
if(Wire.available()>0){ //if info coming in
buffer.longBytes[0]=Wire.read(); //read the 4 bytes from buffer to longBytes 0 throgh 3
buffer.longBytes[1]=Wire.read();
buffer.longBytes[2]=Wire.read();
buffer.longBytes[3]=Wire.read();
Serial.println(buffer.longNumber,HEX); //print result
}
delay(1000); //delay to slow things down
x++; //go to next case
break;
case 1:
wiretransmit();
Wire.requestFrom(4,4);
if(Wire.available()>0){
buffer.longBytes[0]=Wire.read(); //same as above with next set of data
buffer.longBytes[1]=Wire.read();
buffer.longBytes[2]=Wire.read();
buffer.longBytes[3]=Wire.read();
Serial.println(buffer.longNumber,HEX);
}
delay(1000);
x++;
break;
case 2:
wiretransmit();
Wire.requestFrom(4,4);
if(Wire.available()>0){
buffer.longBytes[0]=Wire.read();
buffer.longBytes[1]=Wire.read();
buffer.longBytes[2]=Wire.read();
buffer.longBytes[3]=Wire.read();
Serial.println(buffer.longNumber,HEX);
}
delay(1000);
x++;
break;
case 3:
wiretransmit();
Wire.requestFrom(4,4);
if(Wire.available()>0){
buffer.longBytes[0]=Wire.read();
buffer.longBytes[1]=Wire.read();
buffer.longBytes[2]=Wire.read();
buffer.longBytes[3]=Wire.read();
Serial.println(buffer.longNumber,HEX);
}
delay(1000);
x=0;
break;
}}
Many thanks for any help you can give.