#include <SD.h>
#define CS_PIN 10
File root;
int ledPin = 8;
int button;
String filename = "arduino.txt";
File myFile;
int myInt = -52;
float myFloat = -12.7;
String myString = "HELLO";
char myCharArray[] = "ArduinoGetStarted.com";
byte myByteArray[] = {'1', '2', '3', '4', '5'};
/* code for pushbutton */
const int buttonPin = 2;
// int knob;
void setup() {
Serial.begin(9600);
/* code for pushbutton starts here */
// Serial.begin(9600);
//for (byte i = 0; i < 4; i++) {
// pinMode(ledPins[i], OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
//}
//pinMode(SPEAKER_PIN, OUTPUT);
// The following line primes the random number generator.
// It assumes pin A0 is floating (disconnected):
//randomSeed(analogRead(A0));
/* end of code for pushbutton */
Serial.print("Initializing SD card... ");
if (!SD.begin(CS_PIN)) {
Serial.println("Card initialization failed!");
while (true);
}
Serial.println("initialization done.");
Serial.println("Files in the card:");
root = SD.open("/");
printDirectory(root, 0);
Serial.println("");
// Example of reading file from the card:
File textFile = SD.open("wokwi.txt");
if (textFile) {
Serial.print("wokwi.txt: ");
while (textFile.available()) {
Serial.write(textFile.read());
}
textFile.close();
} else {
Serial.println("error opening wokwi.txt!");
}
// set the pin for the LED
pinMode(ledPin, OUTPUT);
// create new file by opening file for writing
myFile = SD.open(filename, FILE_WRITE);
if (myFile) {
Serial.println("file opened");
Serial.println("test");
Serial.println(myInt); // write int variable to SD card in line
Serial.println(myFloat); // write float variable to SD card in line
Serial.println(myString); // write String variable to SD card in line
Serial.println(myCharArray); // write char array to SD card in line
Serial.write(myByteArray, 5);
Serial.write("\n"); // new line
myFile.println("test");
myFile.println(myInt); // write int variable to SD card in line
myFile.println(myFloat); // write float variable to SD card in line
myFile.println(myString); // write String variable to SD card in line
myFile.println(myCharArray); // write char array to SD card in line
myFile.write(myByteArray, 5);
myFile.write("\n"); // new line
for (int i = 0; i < 5; i++) {
myFile.write(myByteArray[i]); // new line
if (i < 4)
myFile.write(","); // comma
}
myFile.write("\n"); // new line
// Write a set of data to the file and flash the LED
// Run the LED and knob for 10 cycles
for (int k = 1; k < 10; k++) {
//knob = analogRead(A0); // change to
button = digitalRead(buttonPin);
myFile.println(button);
Serial.print("button value = ");
Serial.println(button);
digitalWrite(ledPin, button);
delay(1000);
//digitalWrite(ledPin, );
//delay(1000);
}
myFile.close();
Serial.println("data written");
} else {
Serial.print(F("SD Card: error on opening file arduino.txt"));
}
//}
// Now read the data on the new file
//if (PrintFlag) {
myFile = SD.open(filename);
if (myFile){
Serial.print(filename);
Serial.println(": opened");
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
} else {
Serial.println("error opening file for reading!");
}
Serial.println("file reading complete");
//}
}
void loop() {
button = digitalRead(buttonPin);
Serial.println();
digitalWrite(ledPin, button);
delay(1000);
//digitalWrite(ledPin, LOW);
//delay(knob);
}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}