In: Computer Science
arduino c code only
write a code that counts down a timer on serial monitor and if
A1 is typed into serial monitor prints the timer counting down and writes at 1 second hello and at 5 secs goodbye and repeats every 5 secs
A2 is typed as above at 2 seconds writes hello and 3 seconds writes goodbye
A3 same as above but at 3 seconds says hello and 2 seconds goodbye
This continues until c is pressed to cancel the timer.
When A and the numbers are pressed LED1 is turned on and when goodbye is printed LED1 is turned off until hello prints again and LED1 turns back on
#include <Wire.h>
#include "RTClib.h"
#include <SPI.h> //SPI.h must be
included as DMD is written by SPI (the IDE complains
otherwise)
#include <DMD.h> //
#include <TimerOne.h>
//
#include
"SystemFont5x7.h"
#define HC12 Serial
#define DISPLAYS_ACROSS 1
#define DISPLAYS_DOWN 1
DMD dmd(DISPLAYS_ACROSS,
DISPLAYS_DOWN);
RTC_DS1307 rtc;
void ScanDMD()
{
dmd.scanDisplayBySPI();
}
unsigned long Watch, _micro, time =
micros();
unsigned int Clock = 0,
R_clock;
boolean Reset = false, Stop = false,
Paused = false;
volatile boolean timeFlag =
false;
int i;
int s;
String str = "";
char stringa[10];
void setup()
{
Timer1.initialize( 3000 ); //period in
microseconds to call ScanDMD. Anything longer than 5000 (5ms) and
you can see flicker.
Timer1.attachInterrupt(ScanDMD);
//attach the Timer1 inte
//dmd.clearScreen(true ); //true is
normal (all pixels off), false is negative (all pixels
on)
dmd.selectFont(SystemFont5x7);
while (!Serial); // for
Leonardo/Micro/Zero
Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("Couldn't find
RTC");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT
running!");
// following line sets the RTC to the
date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__),
F(__TIME__)));
// This line sets the RTC with an
explicit date & time, for example to set
// FEBRUARY 2, 2019 at 5am you would
call:
rtc.adjust(DateTime(2019, 3, 3, 2, 40,
0));
}
HC12.begin(9600);
Stop = true;
}
void loop()
{
if (HC12.available() > 0)
{
int inbyte = HC12.read();
switch (inbyte) {
case 'a' : // SET TIMER IN
SECONDS
memset(stringa, 0, sizeof(stringa));
// set string contents to zero
i = HC12.readBytesUntil('\n', stringa,
sizeof(stringa)-1); // Read line of input
String sec(stringa);
i=sec.toInt();
SetTimer(0,0,i);
StartTimer();
break;
case 'r' : // Reset timer
Reset =true;
break;
}
}
CountDownTimer(); // run the
timer
//
this prevents the time from being constantly shown.
if (TimeHasChanged() )
{
Serial.print(ShowHours());
Serial.print(":");
if (((Clock / 60) % 60)<10)
{
Serial.print("0");
Serial.print(ShowMinutes());
dmd.drawString( 0, 0, minutes ,
strlen(minutes()), GRAPHICS_NORMAL );
}
else {
Serial.print(ShowMinutes());
//dmd.drawString( 0, 0, minutes ,
strlen(minutes()), GRAPHICS_NORMAL );
}
//
Serial.print(ShowMinutes());
Serial.print(":");
if (Clock % 60<10) {
Serial.print("0");
Serial.println(ShowSeconds());
//dmd.drawString( 10, 0, bar ,
strlen(bar), GRAPHICS_NORMAL );
}
else {
Serial.println(ShowSeconds());
dmd.drawString( 10, 0, ShowSeconds() ,
strlen(3), GRAPHICS_NORMAL );
}
}
DateTime now = rtc.now();
DateTime future (now +
TimeSpan(0,8,2,6)); //
Serial.print(future.hour(),
DEC);
Serial.print(':');
Serial.print(future.minute(),
DEC);
Serial.print(':');
Serial.print(future.second(),
DEC);
Serial.println();
dmd.drawString( 13, 8, ":",1,
GRAPHICS_NORMAL );
char h [3];
String str1;
str1=String(future.hour());
str1.toCharArray(h,3);
dmd.drawString( 0, 8, h , strlen(h),
GRAPHICS_NORMAL );
char m [3];
String str2;
str2=String(future.minute());
str2.toCharArray(m,3);
dmd.drawString( 19, 8, m , strlen(m),
GRAPHICS_NORMAL );
Serial.println();
delay(1000);
}
boolean CountDownTimer()
{
static unsigned long duration =
1000000; // 1 second
timeFlag = false;
if (!Stop && !Paused) // if
not Stopped or Paused, run timer
{
// check the time difference and see
if 1 second has elapsed
if ((_micro = micros()) - time >
duration )
{
Clock--;
timeFlag = true;
if (Clock == 0) // check to see if the
clock is 0
Stop = true; // If so, stop the
timer
// check to see if micros() has rolled
over, if not,
// then increment "time" by
duration
_micro < time ? time = _micro :
time += duration;
}
}
return !Stop; // return the state of
the timer
}
void ResetTimer()
{
SetTimer(R_clock);
Stop = false;
}
void StartTimer()
{
Watch = micros(); // get the initial
microseconds at the start of the timer
time = micros(); // hwd added so timer
will reset if stopped and then started
Stop = false;
Paused = false;
}
void StopTimer()
{
Stop = true;
}
void StopTimerAt(unsigned int hours,
unsigned int minutes, unsigned int seconds)
{
if (TimeCheck(hours, minutes, seconds)
)
Stop = true;
}
void PauseTimer()
{
Paused = true;
}
void ResumeTimer() // You can resume
the timer if you ever stop it.
{
Paused = false;
}
void SetTimer(unsigned int hours,
unsigned int minutes, unsigned int seconds)
{
// This handles invalid time overflow
ie 1(H), 0(M), 120(S) -> 1, 2, 0
unsigned int _S = (seconds / 60), _M =
(minutes / 60);
if(_S) minutes += _S;
if(_M) hours += _M;
Clock = (hours * 3600) + (minutes *
60) + (seconds % 60);
R_clock = Clock;
Stop = false;
}
void SetTimer(unsigned int
seconds)
{
// StartTimer(seconds / 3600, (seconds
/ 3600) / 60, seconds % 60);
Clock = seconds;
R_clock = Clock;
Stop = false;
}
int ShowHours()
{
return Clock / 3600;
}
int ShowMinutes()
{
return (Clock / 60) % 60;
}
int ShowSeconds()
{
return Clock % 60;
}
unsigned long
ShowMilliSeconds()
{
return (_micro - Watch)/
1000.0;
}
unsigned long
ShowMicroSeconds()
{
return _micro - Watch;
}
boolean TimeHasChanged()
{
return timeFlag;
}
// output true if timer equals
requested time
boolean TimeCheck(unsigned int hours,
unsigned int minutes, unsigned int seconds)
{
return (hours == ShowHours()
&& minutes == ShowMinutes() && seconds ==
ShowSeconds());
}