Hello,
i have posted recently to solve a problem that i cannot seem to figure out. So i would like to ask for some help. Below i have pasted the code that i have. So the upper part of the code works wonderfull, buttons and leds all do as it should and the score increases (thank you to a fellow forumer). However, the problem i have is that when the button on digital pin 49 is pressed (the game stop button), i would like the function to compare the values of the high score and the current score (runningTotal in the code) and if the runningTotal is higher that the high score, runningTotal=highscore, the new high score should be written on the SD card and be displayed on an LCD. And than the program would be ready for another reading of the button(49) and rewrite the highscore if the condition is true.
/*
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>
File myFile;
long highscore = 0;
bool reading;
const int button = 49;
// the buttons
struct buttons
{
byte pin;
byte led;
bool state;
bool prevState;
byte score;
byte pressed;
};
const byte buttonPins = {2, 3, 4, 5 , 6};
const byte numberOfButtons = sizeof(buttonPins) / sizeof(buttonPins[0]);
buttons mybuttons[numberOfButtons];
const byte ledPins = {14, 15, 16, 17, 18};
const byte scores = {10, 20, 30, 40, 50};
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.println(" “);
Serial.print(“Initialising “); Serial.print(numberOfButtons); Serial.println(” buttons”);
for (int i = 0; i < numberOfButtons; i++)
{
mybuttons_.pin = buttonPins*;_
pinMode(mybuttons.pin, INPUT_PULLUP);
mybuttons_.led = ledPins;
pinMode(mybuttons.led, OUTPUT);
mybuttons.state = digitalRead(mybuttons.pin);
mybuttons.prevState = mybuttons.state;
mybuttons.score = scores;
mybuttons.pressed = 0;
Serial.print(“Button: “); Serial.print(i);
Serial.print(", Pin: "); Serial.print(mybuttons.pin);
Serial.print(", Led: "); Serial.print(mybuttons.led);
Serial.print(", Score: "); Serial.print(mybuttons.score);
Serial.print(", Pressed: "); Serial.print(mybuttons.pressed);
Serial.print(", State: "); Serial.print(mybuttons.state);
Serial.print(", Prev state: "); Serial.println(mybuttons.prevState);
}
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only*
* }_
pinMode(49, INPUT_PULLUP);
_ Serial.print(“Initializing SD card…”);
if (!SD.begin(53)) {
Serial.println(“initialization failed!”);
while (1);
}
Serial.println(“initialization done.”);
}
void loop() {
waitforbuttonpress();
checkForButtonStateChange();
if (doEffect) blinkLedsAndMakeANoise();
// score();
}
void checkForButtonStateChange()
{
for (int i = 0; i < numberOfButtons; i++)
{
mybuttons.state = digitalRead(mybuttons.pin);
// compare the buttonState to its previous state*
if (mybuttons.state != mybuttons*.prevState) // means it changed… but which way?
{
if (mybuttons.state == LOW) // changed to pressed*
* {
Serial.print(i);
Serial.print(” newly pressed”);
if (mybuttons.pressed == 0)
{
mybuttons.pressed = 1;
numberOfButtonsPressed++;
Serial.print(”, Unique buttons pressed “); Serial.print(numberOfButtonsPressed);
}
digitalWrite(mybuttons.led, HIGH);
runningTotal = runningTotal + mybuttons.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.prevState = mybuttons*.state;
}
if (numberOfButtonsPressed == 5)
{
Serial.print("All leds on, score "); Serial.println(runningTotal);
for (int i = 0; i < numberOfButtons; i++)
{
mybuttons.pressed = 0;
// audio.play(“mario.wav”, 1, 0);
}
doEffect = true;
numberOfButtonsPressed = 0;
}
} // checkForButtonStateChange()
void blinkLedsAndMakeANoise()
{
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.led, effectState);
}
numberOfEffects++;
}
if (numberOfEffects == 25)
{
doEffect = false;
numberOfEffects = 0;
for (int i = 0; i < numberOfButtons; i++)
{
digitalWrite(mybuttons.led, LOW);
}
}
}
void waitforbuttonpress()
{
reading = digitalRead(49);
if (reading == 0) { //button was pressed*
* delay(50);
if (runningTotal > highscore) { //check if current score > highsore*
* highscore = runningTotal; //if current score> highscore, write that score to SD card*
* myFile = SD.open(“test.txt”);
if (myFile) {
Serial.print(“Writing to test.txt…”);
char line[25];
byte index = 0;
while (myFile.available())
{
// read char and store in line*
* 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++;
}
highscore = atol(line);
myFile.println(highscore);
// close the file:
myFile.close();
}
}
}
}*_