//
//
#include "SdFat.h"
#include <RTClib.h>
const byte RedLED = 13; //This LED will be toggled when the switch on this pin is pressed
const byte YellowLED = 12; //This LED will be toggled when the switch on this pin is pressed
byte lastRedSwitchState; //The state this switch was in
byte lastYellowSwitchState;
#define SPI_SPEED SD_SCK_MHZ(4)
#define CS_PIN 10
SdFat sd;
RTC_DS1307 rtc;
//======================================================================
//An example of Timer 'structure' coding
struct timer
{
//lastMillis = the time this "timer" was (re)started
//waitMillis = delay time (mS) we are looking for
//restart = do we start "this timer" again and again
//enableFlag = is "this timer" enabled/allowed to be accessed
//timeType = true = millis(), false = micros()
//**********************
//For each timer object you need:
//Example:
// timer myTimer = //give the timer a name "myTimer"
// {
// 0, 200UL, true, true, true //lastMillis, waitMillis, restart, enableFlag, timeType
// };
// You have access to:
// myTimer.lastMillis, myTimer.waitMillis, myTimer.restart, myTimer.enableFlag, myTimer.timeType, myTimer.CheckTime()
//**********************
unsigned long lastMillis;
unsigned long waitMillis;
bool restart;
bool enableFlag;
bool timeType;
unsigned long currentTime;
bool CheckTime() //Delay time expired function "CheckTime()"
{
if (timeType == true)
{
currentTime = millis();
}
else
{
currentTime = micros();
}
//is the time up for this task?
if (enableFlag == true && currentTime - lastMillis >= waitMillis)
{
//should this start again?
if (restart)
{
//get ready for the next iteration
lastMillis = currentTime;
}
//time was reached
return true;
}
//time was not reached
return false;
} //END of CheckTime()
}; //END of structure timer
//======================================================================
//**********************************************************************
// Create and initialize timer objects
//**********************************************************************
timer checkSwitches = //create a timer to check the switches
{
0, 50, true, true, true //lastMillis, waitMillis, restart, enableFlag, true=millis/false=micros
};
void setup() {
//
Serial.begin(9600);
rtc.begin();
// Setup LED/switch pins
Serial.print("Setting up pins..");
digitalWrite(RedLED, HIGH); //LED LOW = ON
pinMode(RedLED, OUTPUT);
digitalWrite(YellowLED, HIGH); //LED LOW = ON
pinMode(YellowLED, OUTPUT);
Serial.println(".done.");
// Setup SD card
Serial.print("Initialising SD card..");
if (!sd.begin(CS_PIN, SPI_SPEED)) {
if (sd.card()->errorCode()) {
Serial.println("SD initialization failed.");
} else if (sd.vol()->fatType() == 0) {
Serial.println("Can't find a valid FAT16/FAT32 partition.");
} else {
Serial.println("Can't determine error type");
}
return;
}
Serial.println(".done.");
}
void loop() {
//*************************************
//Is it time to check the Switch(s)?
if (checkSwitches.CheckTime())
{
handleSwitchPresses();
}
//*************************************
}
void handleSwitchPresses()
{
//Read our two switches
byte CurrentSwitchState;
//***************************
//Has the switch changed position for this pin?
CurrentSwitchState = ReadSwitchLED(RedLED);
Serial.print("R=");
Serial.print(CurrentSwitchState);
Serial.print(",");
if (lastRedSwitchState != CurrentSwitchState)
{
lastRedSwitchState = CurrentSwitchState;
Serial.println("Red button state changed");
if (CurrentSwitchState == LOW)
{
digitalWrite(RedLED, !digitalRead(RedLED));
Serial.println("Red LED state changed");
}
}
//***************************
//Has the switch changed position for this pin?
CurrentSwitchState = ReadSwitchLED(YellowLED);
Serial.print("L=");
Serial.print(CurrentSwitchState);
Serial.print(",");
if (lastYellowSwitchState != CurrentSwitchState)
{
//update to the new switch state
lastYellowSwitchState = CurrentSwitchState;
Serial.println("Yellow button state changed");
//when the switch goes from not pushed (HIGH) to pushed (LOW) then do something
if (CurrentSwitchState == LOW)
{
//example: toggle the LED on this pin
digitalWrite(YellowLED, !digitalRead(YellowLED));
Serial.println("Yellow LED state changed");
}
}
} // E N D O F h a n d l e S w i t c h P r e s s e s ( )
//**********************************************************************
// R e a d S w i t c h L E D ( )
//**********************************************************************
//Save the current state of the LED on this pin.
//Read the state of the switch on this pin.
//Return the LED state back to this pin.
//Takes about 14us for this process.
byte ReadSwitchLED(byte thisPin)
{
//Save the current state of this output pin
byte PinState = digitalRead(thisPin);
//read the switch connected to this pin
pinMode(thisPin, INPUT_PULLUP);
byte SwitchState = digitalRead(thisPin);
//return the pin to OUTPUT
pinMode(thisPin, OUTPUT);
//restore the pin state
digitalWrite(thisPin, PinState);
return SwitchState;
} // E N D O F R e a d S w i t c h L E D ( )