Hello i am using an atmel based SAMD21 microcontroller Feather M0 express board.
I want to increase the serial buffer size to receive 100 bytes of data from uart of a different microcontroller.
I am trying to follow the following link, will this be the right approach ?
I have a doubt that in this tutorial the core mentioned is of AVR, but i am using a samd21 which is arm based. Please let me know what should be my approach?
Study Robin's Serial Input Basics - updated to get ideas how to solve your problem (without hacking the core). Change const byte numChars = 32 to the desired size.
@ sterretje Yes, this is a correct way of solving the issue. But i don't want to change my existing program logic as implementing this will require to change the existing logic of the code.
westfw Thanks, i will check. Actually the task is that i am reading a 100 byte string terminated by \n from a SD card via SPI protocol using arduino function readStringUntil('\n') and after reading the string i am doing some data processing on it. So when i am reading upto 56 bytes. The results are as expected but when i am increasing the number of bytes to be read then the results are not correct.
What should i do ?
@ sterretje Okay, so now this is the problem i have to solve. Actually i was earlier using uart instead of spi. But now i am using SPI.
Here is my code:
if (Filec) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
int i=0;
int j=0;
while (Filec.available())
{
String Dc = Filec.readStringUntil('\n');
int ind1 = Dc.indexOf(',');
delay (20);
int ind2 = Dc.indexOf(',',ind1+1);
delay (20);
int ind3 = Dc.indexOf(',',ind2+1);
delay (20);
int ind4 = Dc.indexOf(',',ind3+1);
delay (20);
A = Dc.substring(0,ind1);
delay (20);
B = Dc.substring(ind1+1,ind2);
delay (20);
C = Dc.substring(ind2+1,ind3);
delay (20);
D = Dc.substring(ind3+1);
delay (20);
Serial.print(A);
Serial.print(" ");
delay (25);
Serial.print(B);
Serial.print(" ");
delay (25);
Serial.print(C);
Serial.print(" ");
delay (25);
Serial.print(D);
Serial.println();
delay (25);
if (i<32)
{
arr [i][j] = A.toFloat();
arr [i][j+1] = B.toFloat();
arr [i][j+2] = C.toFloat();
arr [i][j+3] = D.toFloat();
i=i+1;
}
else
{
i=0;
}
Serial.println(" ");
Serial.println("Now printing float values");
Serial.println(" ");
Serial.print(arr[i][j]);
Serial.print(" ");
delay(25);
Serial.print(arr[i][j+1]);
Serial.print(" ");
delay(25);
Serial.print(arr[i][j+2]);
Serial.print(" ");
delay(25);
Serial.println(arr[i][j+3]);
delay(25);
}
Filec.close();
}
// close the file:
else
{
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
I have attached the snapshot for the result, i am getting 4 "0.00" which should not be such values.
Also you need to check how float is implemented on the Feather M0, there is a limit to how many significant digits can be stored, might need to use double.
@ david_2018 yes, i am now using the following code below but i am not getting the exact decimal places matching. I am attaching a snapshot of image showing comparison of what i am able to read as float and how the numbers are stored in txt file.
#include <SPI.h>
#include <SD.h>
#include <Arduino.h> // required before wiring_private.h
#include "wiring_private.h" // pinPeripheral() function
#include <U8x8lib.h>
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define PIN 8
#define NUMPIXELS 1
#define SERIAL_BUFFER_SIZE 256
Uart Serial2 (&sercom1, 11, 10, SERCOM_RX_PAD_0, UART_TX_PAD_2);
void SERCOM1_Handler()
{
Serial2.IrqHandler();
}
File Filec;
String Data;
String A;
String B;
String C;
String D;
float arr [32][4]; //3 column
const int chipSelect = 6;
void setup()
{
Serial1.begin(9600);
Serial2.begin(9600);
Wire.begin();
// Assign pins 10 & 11 SERCOM functionality
pinPeripheral(10, PIO_SERCOM);
pinPeripheral(11, PIO_SERCOM);
Serial2.print("Initializing SD card...");
if (!SD.begin(chipSelect))
{
Serial2.println("Card failed, or not present");
return;
}
Serial2.println("card initialized.");
delay(2000);
Filec = SD.open("test.txt");
if (Filec) {
// read from the file until there's nothing else in it:
int i=0;
int j=0;
delay(1000);
Serial.println(" Printing coefficients ");
delay(1000);
while (Filec.available())
{
String Dc = Filec.readStringUntil('\n');
int ind1 = Dc.indexOf(',');
delay (20);
int ind2 = Dc.indexOf(',',ind1+1);
delay (20);
int ind3 = Dc.indexOf(',',ind2+1);
delay (20);
int ind4 = Dc.indexOf(',',ind3+1);
delay (20);
A = Dc.substring(0,ind1);
delay (20);
B = Dc.substring(ind1+1,ind2);
delay (20);
C = Dc.substring(ind2+1,ind3);
delay (20);
D = Dc.substring(ind3+1);
delay (20);
if (i<32)
{
arr [i][j] = A.toFloat();
arr [i][j+1] = B.toFloat();
arr [i][j+2] = C.toFloat();
arr [i][j+3] = D.toFloat();
Serial.print(arr[i][j],16);
Serial.print(" ");
delay(25);
Serial.print(arr[i][j+1],16);
Serial.print(" ");
delay(25);
Serial.print(arr[i][j+2],16);
Serial.print(" ");
delay(25);
Serial.println(arr[i][j+3],16);
delay(25);
i=i+1;
}
else
{
i=0;
}
}
Filec.close();
}
// close the file:
else
{
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop()
{
}
@ david_2018 It worked using double thanks. Now my doubt is when i am using double and storing this number into a variable will it use all 15 decimal digits for calculation or less digits it will use? Since for serial.print i am doing serial.print(arr[i][j],16) but for calculation there is no such command but i want to use all decimal digits in the formula so that the results are accurate.
Thanks,
Shivam