#include <SD.h>
#define CS_PIN 10
File root;
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = HIGH; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
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'};
int knob;
void setup() {
Serial.begin(9600);
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("");
//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!");
}
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
//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
for (int k = 1; k < 10; k++) {
knob = analogRead(A0);
myFile.println(knob);
Serial.print("knob value = ");
Serial.println(knob);
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
delay(knob);
delay(val);
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
delay(knob);
delay(val);
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
myFile.close();
Serial.println("data written");
} else {
Serial.print(F("SD Card: error on opening file arduino.txt"));
}
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() {
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
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();
}
}