Thursday, March 3, 2022

Reading HEX from SD card and updating the neopixel display

 /*

  the following is modified from SD card file dump


  This sketch shows how to read some data from a file in the SD card using the 

  SD library and update an array of ledMat.


  The circuit:

   SD card attached to SPI bus as follows:

 ** MOSI - pin 11

 ** MISO - pin 12

 ** CLK - pin 13

 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)


  created  22 December 2010

  by Limor Fried

  modified 9 Apr 2012

  by Tom Igoe

  modified 3 Mar 2022

  by Jeszki

  */

#include <SPI.h>

#include <SD.h>

#include <FastLED.h>


#define DATA_PIN    6

#define LED_TYPE    NEOPIXEL

#define COLOR_ORDER GRB

#define NUM_LEDS    128


CRGB ledMat[NUM_LEDS];

     

const int chipSelect = 4;


void setup() {

  // Open serial communications and wait for port to open:


 FastLED.addLeds<WS2812, DATA_PIN, COLOR_ORDER>(ledMat, NUM_LEDS);


  Serial.begin(9600);

  while (!Serial) {

    ; // wait for serial port to connect. Needed for native USB port only

  }

  Serial.print("Initializing SD card...");

  // 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:

    while (1);

  }

  Serial.println("card initialized.");

  // 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 dataFile = SD.open("test.bin");  //just a text file


  // if the file is available, write to it:

  if (dataFile) {

    int x = 0;

    while (dataFile.available()) {

      char x1 = dataFile.read();

      char x2 = dataFile.read();

      char x3 = dataFile.read();

      Serial.print(x1,HEX);

      Serial.print(x2,HEX);

      Serial.print(x3,HEX);

 if (((x+1)%16)==0) { Serial.println();}     // print 16 char on 1 line

        ledMat[x++]= CRGB(x1,x2,x3);

    }  

    dataFile.close();

  }

  // if the file isn't open, pop up an error:

  else {

    Serial.println("error opening datalog.txt");

  }

   FastLED.show();

   delay(100);

}

void loop() {

}


No comments:

Post a Comment