Lots of volunteers will help, but you need to put effort into learning.
An RTC (ex: DS3231) is something that is needed for accurate time of day operations.
Here is a skeleton sketch I often use when a new sketch is started.
It does not use an RTC, it just uses the millis() technique BWD for timing.
//*************************************^****************************************
// FileName.ino
// LarryD
// Version YY/MM/DD Comments
// 1.00 20/01/01 Running code
//
//
//*************************************^****************************************
// M A C R O S
//*************************************^****************************************
// Resistor LED
#define LEDon HIGH //pin----[220R]-----[>|]----GND
#define LEDoff LOW
#define noCHANGE -1 //there was no change in switch state
#define isPUSHED LOW // ~50k normally open
#define isRELEASED HIGH //INPUT_PULLUP-----pin----[N.O. switch]----GND
#define enable true
#define disable false
//*************************************^****************************************
// C l a s s m a k e T i m e r
//*************************************^****************************************
//
class makeTimer
{
#define MILLIS true
#define MICROS false
//Note: since code takes time to execute, precise micro second timing is difficult.
//StartTime = the time this "TIMER" was (re)started
//Interval = Interval/delay time we are looking for
//Restart = do we start "this TIMER" again and again
//EnableFlag = is "this TIMER" enabled/allowed to be accessed
//TimerType = true/MILLIS = millis(), false/MICROS = micros()
//****************************
//For each TIMER object you need:
//Example:
// makeTimer myTimer = //create a Timer named "myTimer"
// {
// 0, 200UL, true, true, MILLIS //StartTime, Interval, Restart, EnableFlag, TimerType (MILLIS/MICROS)
// };
//
//Each TIMER object is made up of 5 variables:
//myTimer.StartTime, myTimer.Interval, myTimer.Restart, myTimer.EnableFlag and myTimer.TimerType
//
//You have the following class functions:
//myTimer.CheckTime() and myTimer.EnableTimer() and myTimer.DisableTimer()
//****************************
public:
unsigned long StartTime;
unsigned long Interval;
bool Restart;
bool EnableFlag;
bool TimerType;
unsigned long currentTime;
// m a k e t i m e r ( )
//*************************************^****************************************
//Constructor
//
makeTimer(unsigned long ST, unsigned long INT, bool RES, bool ENA, bool TT)
{
StartTime = ST;
Interval = INT;
Restart = RES;
EnableFlag = ENA;
TimerType = TT;
} //END of makeTimer()
// C h e c k T i m e r ( )
//*************************************^****************************************
//Delay time expired function "CheckTime()"
//
bool CheckTime()
{
//StartTime = the time TIMER was (re)started
//Interval = interval/delay we are looking for
//Restart = do we restart TIMER automatically
//EnableFlag = is TIMER enabled/allowed to be accessed
//TimerType = use ms or us timing, MILLIS/true = millis(), MICROS/false = micros()
if (TimerType == MILLIS)
{
//using millis() for this TIMER
currentTime = millis();
}
else
{
//using micros() for this TIMER
currentTime = micros();
}
//is the TIMER enabled and has the TIMER expired?
if (EnableFlag == true && currentTime - StartTime >= Interval)
//Note: if delays of < 2ms are needed use micros() and adjust 'Interval' as needed
{
//should we restart the TIMER automatically?
if (Restart)
{
//Restart the TIMER
StartTime = currentTime;
//optionally
//StartTime = StartTime + Interval;
}
//this TIMER did expired
return true;
} //END of if(EnableFlag == true && currentTime - StartTime >= Interval)
//this TIMER did not expire or it is disabled
return false;
} //END of CheckTime()
// E n a b l e T i m e r ( )
//*************************************^****************************************
//Function to enable and initialize a TIMER, ex: myTimer.EnableTimer();
//
void EnableTimer()
{
EnableFlag = true;
//initialize lastTime to current millis() or micros()
if (TimerType == true)
{
StartTime = millis();
}
else
{
StartTime = micros();
}
} //END of EnableTimer()
// D i s a b l e T i m e r ( )
//*************************************^****************************************
//Function to disable a TIMER, ex: myTimer.DisableTimer();
//
void DisableTimer()
{
EnableFlag = false;
} //END of DisableTimer()
}; //END of class makeTimer
//*************************************^****************************************
// Instantiate TIMER objects
//*************************************^****************************************
makeTimer heartbeatLED = //create a millisecond TIMER 'heartbeatLED'
{
0, 200UL, true, true, MILLIS //StartTime, Interval, Restart, EnableFlag, TimerType (MILLIS/MICROS)
};
//*************
makeTimer checkMySwitches = //create a millisecond TIMER 'checkMySwitches'
{
0, 20UL, true, true, MILLIS //StartTime, Interval, Restart, EnableFlag, TimerType (MILLIS/MICROS)
};
//*************************************^****************************************
// C l a s s d e f i n e S w i t c h
//*************************************^****************************************
//
class defineSwitch
{
public:
byte pin; //physical pin being used for this switch
byte closedLevel; //pin level when the switch is closed
unsigned long switchMillis; //how long the switch was held closed
byte lastState; //last state the switch was in
// d e f i n e S w i t c h ( )
//*************************************^****************************************
//Constructor
//
defineSwitch(byte _pin, byte _closedLevel)
{
pin = _pin;
closedLevel = _closedLevel;
//how long the switch was closed
switchMillis = 0;
//the level of the switch when released
lastState = 1 - closedLevel;
pinMode(pin, INPUT_PULLUP);
} //END of defineSwitch()
// C h a n g e I n S t a t e ( )
//*************************************^****************************************
//check to see if there is a change in switch state
//
char ChangeInState()
{
byte currentState = digitalRead(pin);
if (lastState != currentState)
{
//update to the new state
lastState = currentState;
if (currentState == closedLevel)
{
//the time the switch was closed
switchMillis = millis();
//the switch was closed/pushed
return closedLevel;
}
else
{
//calculate the time the switch was closed
switchMillis = millis() - switchMillis;
//then switch was opened/released
return 1 - closedLevel;
}
} //END of if (lastState != currentState)
//there was no change in switch state
return -1;
} //END of ChangeInState()
// P i n S t a t e ( )
//*************************************^****************************************
//check to see if there is a change in switch state
//
byte PinState()
{
byte state = digitalRead(pin);
return state;
} //END of PinState()
}; //END of class defineSwitch
//*************************************^****************************************
// Instantiate switch objects
//*************************************^****************************************
// ~50k normally open
//switch are wired as such INPUT_PULLUP-----pin-----[N.O. switch]----GND
//
// pin#, closed level
defineSwitch SWITCH1 = {2, isPUSHED};
defineSwitch SWITCH2 = {3, isPUSHED};
//*************************************^****************************************
// G l o b a l v a r i a b l e s
//*************************************^****************************************
// I N P U T S
// O U T P U T S
const byte PIN_13 = 13;
const byte PIN_12 = 12;
const byte PIN_11 = 11;
// S R A M
int counter;
// s e t u p ( )
//*************************************^****************************************
//
void setup()
{
Serial.begin(9600);
//***************************
pinMode(PIN_13, OUTPUT);
pinMode(PIN_12, OUTPUT);
pinMode(PIN_11, OUTPUT);
} //END of setup()
// l o o p ( )
//*************************************^****************************************
//
void loop()
{
//****************************
//is it time to toggle the heartbeat LED
if (heartbeatLED.CheckTime())
{
//Toggle heartbeatLED
//digitalWrite(heartLED, !digitalRead(heartLED)); //generic
PINB = 0x20; // Toggles pin D13 UNO
//Toggle pins specific to UNO
//PINB=0x20; //13 PINB=0x10; //12 PINB=0x08; //11 PINB=0x04; //10 PINB=0x02; //9 PINB=0x01; //8
//PIND=0x80; //7 PIND=0x40; //6 PIND=0x20; //5 PIND=0x10; //4 PIND=0x08; //3 PIND=0x04; //2
//if you only want this section of code to happen once
//uncomment the next line
//heartbeatLED.DisableTimer();
} //END of if(heartbeatLED.CheckTime())
//****************************
//is it time to check the switches ?
if (checkMySwitches.CheckTime())
{
checkSwiches();
} //END of if(checkMySwitches.CheckTime())
//****************************
//None blocking code goes here
//****************************
} //END of loop()
// c h e c k S w i t c h e s ( )
//*************************************^****************************************
//
void checkSwiches()
{
//****************************************************** SWITCH1
//3 possibilities: noCHANGE, isPUSHED or isRELEASED
switch (SWITCH1.ChangeInState())
{
//******************
case noCHANGE:
//do nothing
break;
//******************
case isPUSHED:
//digitalWrite(12, !digitalRead(12)); //generic
PINB = 0x10; //Toggles pin D12, specific to UNO
counter++;
Serial.println(counter);
break;
//******************
case isRELEASED:
//how long was the switch pushed for ?
Serial.print("SWITCH1 was pressed for: ");
Serial.println(SWITCH1.switchMillis);
break;
} //END of switch/case
//****************************************************** SWITCH2
//3 possibilities: noCHANGE, isPUSHED or isRELEASED
switch (SWITCH2.ChangeInState())
{
//******************
case noCHANGE:
//do nothing
break;
//******************
case isPUSHED:
//digitalWrite(11, !digitalRead(11)); //generic
PINB = 0x08; //Toggles pin D11, specific to UNO
counter--;
if (counter < 0)
{
counter = 0;
}
Serial.println(counter);
break;
//******************
case isRELEASED:
//how long was the switch pushed for ?
Serial.print("SWITCH2 was pressed for: ");
Serial.println(SWITCH2.switchMillis);
break;
} //END of switch/case
//****************************************************** Other switches
} //END of checkSwitches()
//*************************************^****************************************
// E N D O F C O D E
//*************************************^****************************************