Skip to content

Instantly share code, notes, and snippets.

@vvzen
Created January 30, 2018 21:03
Show Gist options
  • Save vvzen/ced3cca02c1d839c586fb624a61cc3c5 to your computer and use it in GitHub Desktop.
Save vvzen/ced3cca02c1d839c586fb624a61cc3c5 to your computer and use it in GitHub Desktop.
/*
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
*/
// PINS
#define CS_PIN 4
// libs
#include <SPI.h>
#include <SD.h>
// SD vars
File encouraging_words_file;
// CSV stuff
String words[48];
void setup(){
Serial.begin(9600);
// initialize the sd
if (!SD.begin(CS_PIN)){
Serial.println("begin failed!");
}
// Read file
encouraging_words_file = SD.open("words.txt");
if (!encouraging_words_file){
Serial.println("opening of file failed");
}
Serial.println("--------------------------");
Serial.println(" started parsing ");
Serial.println("--------------------------");
//String current_line;
const int current_line_max_size = 20;
char current_line[current_line_max_size];
char * ptoken;
int c = 0;
while (encouraging_words_file.available()){
// convert arduino String to char array
encouraging_words_file.readStringUntil('\n').toCharArray(current_line, current_line_max_size);
Serial.println(current_line);
// the first token is the number
ptoken = strtok(current_line, ",");
//Serial.print("token 1 : ");
//Serial.print(ptoken);
// the second one is the actual word
// when we use NULL as parameter,
// strtok continues scanning from the last previous successfull call
ptoken = strtok(NULL, ",");
Serial.print("token 2 : ");
Serial.println(ptoken);
words[c] = ptoken;
c++;
}
encouraging_words_file.close();
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment