Question about byte array.

Kind people. I need your help. I modified two codes and made a single code.

The codes belong to the programmers Lucario 448 and Ben Eater.

My project is to dump a file on the SD card, create a byte array with hex dump and write this data to an EEPROM, using two shift registers . The data is a set of binary information that will light some LEDs and leave others in low state.

This is my code.

 //

#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
File file;

int buttonState1;
int buttonState2;

int btnDump = 5;

int btnWrite = 4;




#define SHIFT_DATA 7
#define SHIFT_CLK 8
#define SHIFT_LATCH 9
#define EEPROM_D0 10
#define EEPROM_D7 11
#define WRITE_EN 12




/*
 * Output the address bits and outputEnable signal using shift registers.
 */
void setAddress(int address, bool outputEnable) {
  shiftOut(SHIFT_DATA, SHIFT_CLK, MSBFIRST, (address >> 8) | (outputEnable ? 0x00 : 0x80));
  shiftOut(SHIFT_DATA, SHIFT_CLK, MSBFIRST, address);

  digitalWrite(SHIFT_LATCH, LOW);
  digitalWrite(SHIFT_LATCH, HIGH);
  digitalWrite(SHIFT_LATCH, LOW);
}



/*
 * Write a byte to the EEPROM at the specified address.
 */
void writeEEPROM(int address, byte dados) {
  setAddress(address, /*outputEnable*/ false);
  for (int pin = EEPROM_D0; pin <= EEPROM_D7; pin += 1) {
    pinMode(pin, OUTPUT);
  }

  for (int pin = EEPROM_D0; pin <= EEPROM_D7; pin += 1) {
    digitalWrite(pin, dados & 1);
    dados = dados >> 1;
  }
  digitalWrite(WRITE_EN, LOW);
  delayMicroseconds(1);
  digitalWrite(WRITE_EN, HIGH);
  delay(10);
}



byte dados[] = {Serial.read()};






void setup() {

  pinMode(SHIFT_DATA, OUTPUT);
  pinMode(SHIFT_CLK, OUTPUT);
  pinMode(SHIFT_LATCH, OUTPUT);
  digitalWrite(WRITE_EN, HIGH);
  pinMode(WRITE_EN, OUTPUT);
  
  Serial.begin(57600);

  
pinMode(btnDump, INPUT_PULLUP);
pinMode(btnWrite, INPUT_PULLUP);  

 buttonState1 = digitalRead(btnDump);
 buttonState2 = digitalRead(btnWrite);
 if (buttonState1 == LOW){


   

    for (int seconds = 10; seconds >= 0; seconds--)
    {
       
        delay(1000);
    }



  openFileOnSD();
 
  if (file) dumpTheOpenedFile();
delay (5);

  
  } 
   

 

  
}



void writeEEPROM(){
   // Erase entire EEPROM
  
  for (int address = 0; address <= 2047; address += 1) {
    writeEEPROM(address, 0xff);

    if (address % 64 == 0) {
      Serial.print(".");
    }
  }


  // Program data bytes
 
  for (int address = 0; address < sizeof(dados); address += 1) {
    writeEEPROM(address, dados[address]);

    if (address % 64 == 0) {
      Serial.print(".");
    }
  }

  
}



  
void loop() {
 
 

 
  
    
  }// end void loop
  
  
  
  
  
void openFileOnSD()
{


  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present ******************");
    // don't do anything more:
    return;
  }
 

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  file = SD.open("led.bin"); //<<<<<<<<<<<<<<<<<<< set the name here <<<<<<<<<<<<<<<
  
 //printDirectory(file, 0);
  if (file)
  {
  //Serial.println(" ");
    delay(150);
  }
  // if the file isn't open, pop up an error:
  else
  {
    Serial.println("Error opening file... check the name ******************");
  }
}




void dumpTheOpenedFile()
{
 // Assuming the file is already opened for reading.

const byte bytesPerRow = 16; // 4 as minimum

char hexVal[9];
byte buffer[bytesPerRow];


file.seek(0);
// Typing file's content
while (file.available()) {

   
    byte amount = file.read(buffer, bytesPerRow);
   
    //Print hex values
    for (byte i = 0; i < amount; i++)
   {
      sprintf(hexVal, "0x%02X,", buffer[i]);
      Serial.print(hexVal);
    }
  
   
  
Serial.println(); // Next line
 delay(25);
} // End of while loop



file.close();
 pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
 //   for (unsigned int counter = 0; counter < 5; counter++)
    //{
        delay(1000);
   //}
}

This is how the data is printed in Serial Monitor .

0x3D,0x80,0x3D,0x80,0x3E,0x80,0x3D,0x80,0x3E,0x80,0x3E,0x80,0x3D,0x80,0x3E,0x80,
0x37,0x03,0x0F,0x0B,0x00,0x2F,0x06,0x0B,0x01,0x3E,0x38,0x0C,0x08,0x3C,0x30,0x3F,
0x05,0x0B,0x03,0x02,0x00,0x30,0x06,0x0C,0x0F,0x08,0x3A,0x36,0x03,0x0A,0x30,0x3F,

I have not implemented the write button function, because I need to understand something.

I want the byte array dados in my code, to contain this hex dump information. I would just like to remove the last comma present in hex dump. So, my array will be correctly formatted to write data to EEPROM.

I’ts possible? If the answer is “yes”, please, how can I do this?

All help will be greatly appreciated.

So you want the read binary from the file, print them in hex to Serial Monitor, have someone copy them from Serial Monitor and paste them back into Serial Monitor so you can parse the hex back into binary so you can write them to an EPROM? Can’t you just read the binary bytes from the file and write them directly to the EPROM without doing the double conversion, copy, and paste?

johnwasser: So you want the read binary from the file, print them in hex to Serial Monitor, have someone copy them from Serial Monitor and paste them back into Serial Monitor so you can parse the hex back into binary so you can write them to an EPROM? Can't you just read the binary bytes from the file and write them directly to the EPROM without doing the double conversion, copy, and paste?

Good night, Sir John. Thanks for your answer.

Your answer is completely cohesive.

Yes, I could write directly. But, I want to create a particular device and this device, will extract information of various sizes, ranging from 16 kb to 512 kb(but, is the samte file type). And this device will not be connected on a computer, thats means, I will not use the COM port of my computer. For this reason, I will not be able to copy and paste the hex code into the byte array data in the Arduino IDE. I will just insert the SD card with the data , extract this data with a button , create the array and write this array data in the EEPROM.

So, I need to delete the last comma from my array before starting the process of writing in the EEPROM.

Is it possible to do that?

Thanks.

This part is unnecessary.

create a byte array with hex dump

Just read the data file from the SD card, binary byte by binary byte, and write those binary bytes directly to EEPROM.

What Arduino are you using? The Arduino UNO has only 2k bytes of RAM. There is no way to read in and store 512k of data, let alone 2.56 Megabytes of ascii representing hex.

So, I need to delete the last comma from my array before starting the process of writing in the EEPROM.

First off you don’t need to remove just thee last comma you need to remove all of them.

Second:- The EEPROM is only 1K so how will that work? Some Arduinos don’t even have any EEPROM.

I don’t think you are describing what you want to do, you are describing what you think is a solution to what you want to do. This is known as an X-Y problem, look it up.

Grumpy_Mike: Second:- The EEPROM is only 1K so how will that work? Some Arduinos don’t even have any EEPROM.

It's an external EPROM, not the internal EEPROM. The code shows a 16-bit shift register for the a ddre ss bits and eight (consecutively numbered) data pins for the data bits. It appears that they are building an EPROM burner.

jremington: This part is unnecessary. Just read the data file from the SD card, binary byte by binary byte, and write those binary bytes directly to EEPROM.

johnwasser: What Arduino are you using? The Arduino UNO has only 2k bytes of RAM. There is no way to read in and store 512k of data, let alone 2.56 Megabytes of ascii representing hex.

Kind Sir. johnwasser and kind Sir. Jremington. You are absolutely right! I just get it now! true, I can write directly to EEPROM. I am a complete idiot! forgive me!

Sir johnwasser and Sir. Grumpy_Mike , I'm working with a Arduino Uno Rv3 , plus two 74hc595 shift registers and a external EEPROM 28c256 for tests at moment. But, I'll change this to other EEPROMS with more address lines and more memory.

Grumpy_Mike: First off you don’t need to remove just thee last comma you need to remove all [...]

I did not understand what the Sir.Grumpy_Mike want to suggest. Could you help me? to declare a byte array, the declaration should be byte array dados [] = {0x42, 0x60, 0x42, 0x60, 0x42, 0x60}; with no comma in the last byte, or else byte array dados [] = {0x42 0x60 0x42 0x60 0x42 0x60}; with no comma at all? I did not understand. Can you help me?

In fact, dear gentlemen, my question also makes sense to know some Arduino properties better, such as removing context-specific characters and other things.

I've even worked out something. It has a website called "File to Hex Converter". It has the option of copy output to clipboard, after the fast hex dump. Just copy it to a text file from notepad, remove the last comma, save and now, I can do the process which I asked for help from the forum.

Please forgive me for my idiot mind, I am too slow to get the messages correctly.

Before putting my case as solved I would like to know two things. First, if my byte declaration in "byte array dados" is right for me to achieve my goal.

And second, I would also like to know, if there is a possibility within the SD library, to build a function that allows reading files of any title, as long as they contain a specific extension, as in my case, the extension *.txt.

Thank you, gentlemen.

to declare a byte array, the declaration should be byte array dados [] = {0x42, 0x60, ........

That is only how you do it in source code. If you are taking the bytes from a file all you need to do is to declare an empty array with the number of elements you want say you want an array of 500 bytes use byte dados [500] ; and then read the bytes from the file and save them in the array.

Grumpy_Mike:
That is only how you do it in source code. If you are taking the bytes from a file all you need to do is to declare an empty array with the number of elements you want say […]

Oh, Sir!. Now I understand! yes! I’ll do this! Thank you!