Greetings,
i've posted before about this problem burt i cannot seem to make it work... I've tried the SD.remove commmand in the original library, millions of examples from the internet, tried if the sensors actually work...IT all works except the damn SD.remove. Why i want to to this SD.remove is because by deleteing the file and that creating a new one, in which the highscore should be stored, i can easily than read from the sd card to write the high score on a LCD.
In the test.txt file the high score should be saved, but first the previously saved one should be removed...In the picture attached you can see a failed attempt on the serial monitor...Thank you very much for any kind of help.
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
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 Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char line[25];
byte index = 0;
File myFile;
long highscore = 0;
bool reading;
const int button = 49;
unsigned long prejsni = 0;
const long interval = 100;
// the buttons
struct buttons
{
byte pin;
byte led;
bool state;
bool prevState;
byte score;
byte pressed;
};
const byte buttonPins[] = {13, 14, 15 , 16, 17, 18, 19, 20 , 21, 22};
const byte numberOfButtons = sizeof(buttonPins) / sizeof(buttonPins[0]);
buttons mybuttons[numberOfButtons];
const byte ledPins[] = {23, 24, 25 , 26, 27, 28, 29, 30, 31, 32};
const byte scores[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
long runningTotal = 0;
byte numberOfButtonsPressed = 0;
//the effect led
int effectLedInterval;
int effectLedIntervalMax = 100;
int effectLedIntervalMin = 30;
unsigned long previousMillisEffect;
bool effectState;
bool doEffect = false;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("SUPER MARIO");
lcd.setCursor(5, 1);
lcd.print("PINBALL");
delay(2500);
Serial.println(" ");
Serial.print("Initialising "); Serial.print(numberOfButtons); Serial.println(" buttons");
for (int i = 0; i < numberOfButtons; i++)
{
mybuttons[i].pin = buttonPins[i];
pinMode(mybuttons[i].pin, INPUT_PULLUP);
mybuttons[i].led = ledPins[i];
pinMode(mybuttons[i].led, OUTPUT);
mybuttons[i].state = digitalRead(mybuttons[i].pin);
mybuttons[i].prevState = mybuttons[i].state;
mybuttons[i].score = scores[i];
mybuttons[i].pressed = 0;
Serial.print("Button: "); Serial.print(i);
Serial.print(", Pin: "); Serial.print(mybuttons[i].pin);
Serial.print(", Led: "); Serial.print(mybuttons[i].led);
Serial.print(", Score: "); Serial.print(mybuttons[i].score);
Serial.print(", Pressed: "); Serial.print(mybuttons[i].pressed);
Serial.print(", State: "); Serial.print(mybuttons[i].state);
Serial.print(", Prev state: "); Serial.println(mybuttons[i].prevState);
}
// Open serial communications and wait for port to open:
pinMode(49, INPUT_PULLUP);
Serial.print("Initializing SD card...");
if (!SD.begin(53)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
////////////////////////////////////////////////////////////////////////
//Izpis high score-a iz SD kartice ob ponovnem zagonu.
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
line[index] = (myFile.read());
// check if character is '\r'
if (line[index] == '\r')
{
// add terminatin NUL character
line[index] == '\0';
// done
break;
}
// next character will go in next element of line
index++;
}
// close the file:
highscore = atol(line);
myFile.close();
}
}
void loop() {
waitforbuttonpress();
checkForButtonStateChange();
if (doEffect) blinkLeds();
text();
}
void checkForButtonStateChange()
{
for (int i = 0; i < numberOfButtons; i++)
{
mybuttons[i].state = digitalRead(mybuttons[i].pin);
// compare the buttonState to its previous state
if (mybuttons[i].state != mybuttons[i].prevState) // means it changed... but which way?
{
if (mybuttons[i].state == LOW) // changed to pressed
{
Serial.print(i);
Serial.print(" newly pressed");
if (mybuttons[i].pressed == 0)
{
mybuttons[i].pressed = 1;
numberOfButtonsPressed++;
Serial.print(", Unique buttons pressed "); Serial.print(numberOfButtonsPressed);
}
digitalWrite(mybuttons[i].led, HIGH);
runningTotal = runningTotal + mybuttons[i].score;
Serial.print(", Score "); Serial.println(runningTotal);
}
// poor man's de-bounce
delay(50);
}
// save the current state as the last state, for next time through the loop
mybuttons[i].prevState = mybuttons[i].state;
}
if (numberOfButtonsPressed == 5)
{
Serial.print("All leds on, score "); Serial.println(runningTotal);
for (int i = 0; i < numberOfButtons; i++)
{
mybuttons[i].pressed = 0;
// audio.play("mario.wav", 1, 0);
}
doEffect = true;
numberOfButtonsPressed = 0;
}
} // checkForButtonStateChange()
void blinkLeds()
{
static byte numberOfEffects = 0;
if (millis() - previousMillisEffect >= effectLedInterval)
{
effectLedInterval = random(effectLedIntervalMin, effectLedIntervalMax);
previousMillisEffect = millis();
effectState = !effectState;
for (int i = 0; i < numberOfButtons; i++)
{
digitalWrite(mybuttons[i].led, effectState);
}
numberOfEffects++;
}
if (numberOfEffects == 25)
{
doEffect = false;
numberOfEffects = 0;
for (int i = 0; i < numberOfButtons; i++)
{
digitalWrite(mybuttons[i].led, LOW);
}
}
}
void waitforbuttonpress()
{
reading = digitalRead(49);
if (reading == 0) { //button was pressed
delay(100);
if (runningTotal > highscore) { //check if current score > highsore
highscore = runningTotal; //if current score> highscore, write that score to SD card
delay(100);
if (SD.remove("test.txt")) {
Serial.println("test.txt removed.");
} else {
Serial.println("test.txt wasn't removed.");
}
if (SD.exists("test.txt")) {
Serial.println("test.txt exists.");
} else {
Serial.println("test.txt doesn't exist.");
}
// delay(100);
// myFile = SD.open("test.txt", FILE_WRITE);
// if (myFile) {
// Serial.print("Writing to test.txt...");
// myFile.println(highscore);
// myFile.close();
// }
}
}
}
void text() {
if (millis() - prejsni >= interval) {
prejsni = millis();
lcd.clear();
lcd.setCursor(4, 0);
lcd.print(runningTotal);
lcd.setCursor(4, 1);
lcd.print(highscore);
}
}