Using a for loop to store string with spi flash memory

Hi
I’m hoping someone can help me, I have just started working with flash memory using the SPImemory library so I do apologize if this is something that is easy. I have a string of data 255,255,255 which represents a LED, I have 300 LED’s so I have 255,255,255 x 300, I want to store each string (each 255,255,255) inside the flash memory.

I had tried to store all of it into a string variable however that wastes a lot of space and doesn’t work so my idea was to store each led variable into a different memory address. Example address 0 = 255,255,255, address 1 = 255,255,255, address 3 = 255,255,255 etc, I believe this is the best way.

So I have a issues when it comes to storing all the data using a for loop as it doesn’t seem to work

#include <Tlc5940.h>

#include <SPIMemory.h>
SPIFlash flash;
#if defined(ARDUINO_SAMD_ZERO) && defined(SERIAL_PORT_USBVIRTUAL)
// Required for Serial on Zero based boards
#define Serial SERIAL_PORT_USBVIRTUAL
#endif

#if defined (SIMBLEE)
#define BAUD_RATE 250000
#define RANDPIN 1
#else
#define BAUD_RATE 115200
#if defined(ARCH_STM32)
#define RANDPIN PA0
#else
#define RANDPIN A0
#endif
#endif

String inputString;
//String inputString2;
String outputString;

String readString;
//uint32_t strAddr[3];

void setup() {
  // put your setup code here, to run once:
  Serial.begin(BAUD_RATE);
#if defined (ARDUINO_ARCH_SAMD) || (__AVR_ATmega32U4__) || defined(ARCH_STM32) || defined(NRF5)
  while (!Serial) ; // Wait for Serial monitor to open
#endif
  delay(50); //Time to terminal get connected
  //Serial.print(F("Initialising"));
  for (uint8_t i = 0; i < 10; ++i)
  {
    Serial.print(F("."));
  }
  Serial.println();
  randomSeed(analogRead(RANDPIN));
  flash.begin();

Serial.println("Start");
  
}

void loop() {
  // put your main code here, to run repeatedly:
while(Serial.available() > 0) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(5);  //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
  if(readString == "Delete"){
    Delete();
  }
  if(readString == "Upload"){
    Upload();
  }
   if(readString == "Read"){
    Read();
  }
}
readString="";
}
void Delete() {
   Serial.println("Deleting.....");
  if (flash.eraseChip()){
    Serial.println(F("Chip erased"));
  }
  else {
    Serial.println(F("Error erasing chip"));
  }
  
}

void Upload() {
  Serial.println("Uploading.....");
inputString = "255,255,255";
for (uint8_t i = 0; i < 300; i++) {
  if (flash.writeStr(i, inputString)) {
    Serial.println("Writing Success");
  }
  else {
    Serial.println("Write Failed");
  }
}
Serial.println("Upload Finished");
}

void Read() {

for (uint8_t i = 0; i < 300; i++) {
flash.readStr(i, outputString);
  Serial.println(outputString);
}
Serial.println("Read Finished");
}

As you can see from my above code which im using for testing, the upload functions starts a for loop using the int i as the memory address to put the string variable into, however it doesn’t seem to work the way I imagined, this is where I need help

I also have the function to read those address back, which means I need to know which address I wrote to hence another for loop using int i as the address point to read from.

I hope this is understandable and any help would be great

Thank you

flash.writeStr(i, inputString)

I am not familiar with the library or hardware that you are using. However, it looks likely that this function writes data to memory starting at address i

If that is so, then the next write will save data to address i+1 but the data is not a single byte so the second write will overwrite most of the previous data written

If I am right then can you see a problem ?

Hi

Thank you for the help i went about it a different way using bytes to store the data into addresses using for loop

byte testByte = 100;
for (int i = 0; i  < 200; i++) {
    if (flash.writeByte(i, testByte)) {
    Serial.println("Writing Success");
   // Serial.println(byteAddr[i]);
  }
  else {
    Serial.println("Write Failed");
    // Serial.println(byteAddr[i]);
  }
}
  delay(50);

that seemed to work however i have a number like 100 stored in each address which is a byte variable, is there a way to split the byte as an example

byte test = 100 
byte[0] = 1 
byte[1] = 0 
byte[2] = 0

If the value of number is base 10 then adapt this

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  byte number = 123;
  byte hundreds = number / 100;
  byte tens = (number % 100) / 10;
  byte ones = (number % 10 );
  Serial.println(hundreds);
  Serial.println(tens);
  Serial.println(ones);
}

void loop()
{
}

Hi

Thank you i have tried it that way and honestily what i had in mind had some values with leading zeros so i decided to do this

 Outputs:
  Red : 1
  Green : 2
  Blue : 3
  Red And Green : 4
  Red And Blue : 5
  Green And Blue : 6
  All On : 7
  All Off : 8

so those numbers would represent the bytes i want to store, my problem now is that no matter what i do, honestly a bit frustrated i cant seem to read a value im sending to the serial and storing it as a byte. Example i type in 1, 2, 3 into the serial however the output comes out as 49,50,51 etc.... i have tried strings converting to byte and all i get is zeros i have tried converting int to bytes and i get random numbers. i am literally super lost

Thank you

Welcome to the wonderful world of ASCII. What you are seeing is the value of the characters representing 1, 2, 3 etc. If you want to turn them into real integers then subtract '0' (or 48) from the value received

Thank you so much that literally worked for me

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.