Question

In: Computer Science

arduino c code only write a code that counts down a timer on serial monitor and...

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

Solutions

Expert Solution

#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());
}


Related Solutions

Write code in C language in the Arduino IDE ADC data using Serial plotter Serial plot...
Write code in C language in the Arduino IDE ADC data using Serial plotter Serial plot : raw data, delay data (int) Purpose: Delay Block (**Using Class**) Input : u(t) Output : o(t)=u(t-h) sample time=0.02 Delay (h) = 0.4
Write a program that captures the transmission of a character from an Arduino board over serial...
Write a program that captures the transmission of a character from an Arduino board over serial communication (UART)
write pseudocode for the following problems not c code Pseudocode only Write a C program to...
write pseudocode for the following problems not c code Pseudocode only Write a C program to print all natural numbers from 1 to n. - using while loop Write a C program to print all natural numbers in reverse (from n to 1). - using while loop Write a C program to print all alphabets from a to z. - using while loop Write a C program to print all even numbers between 1 to 100. - using while loop...
Write the following in C language for Arduino: Write a program that turns on the LED...
Write the following in C language for Arduino: Write a program that turns on the LED at 25%, 50%, 75%, 100%, and then 0% brightness with a one second delay in between each change. Remember you are going to need to use a PWM pin and use the "analogWrite" command. The maximum value for our Arduino R3 boards is 255 and you need five steps (25%, 50%, 75%, 100%, and 0%) so you will need to determine the values for...
THIS IS FOR ARDUINO PROGRAMMING Write code that checks the sensor data and meets the following...
THIS IS FOR ARDUINO PROGRAMMING Write code that checks the sensor data and meets the following conditions: For night conditions, turn on white LED 1 For day conditions, turn off white LED 1 Code should check intervals every 5 seconds. NEED THE BELOW CODE TO FIT THE ABOVE REQUIREMENTS. const int lightSensor = 5; //light sensor variable float sensValue = 0; //variable to hold sensor readings const int button = 3; //pin for button reads const int LED1 = 5;...
Write an Arduino code that calls a web service to show the location of the iss...
Write an Arduino code that calls a web service to show the location of the iss after a couple of seconds and displays in on an LCD
Write code using the Arduino IDE that compiles with no errors. 2-bit adder: The code must...
Write code using the Arduino IDE that compiles with no errors. 2-bit adder: The code must read two digital input signals and turn on two LEDS as needed to show the sum of the inputs. e.g. 0 + 1 = 01.
Constant Current Code for Arduino Develop a Constant Current Program in C and upload it into...
Constant Current Code for Arduino Develop a Constant Current Program in C and upload it into your Arduino #include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Write the following in C language for Arduino: Write a program that increases the LED brightness...
Write the following in C language for Arduino: Write a program that increases the LED brightness in five steps with each press of the button. Remember you are going to using a digital input and the "digitalRead" command. You are going to need to use a PWM pin and use the "analogWrite" command. The maximum value for our Arduino R3 boards is 255 and you need five steps (25%, 50%, 75%, 100%, and 0%) so you will need to determine...
Write an assembly code the counts the number of accuracies of the byte AAh in memory...
Write an assembly code the counts the number of accuracies of the byte AAh in memory from address 120Ah to address 130Ah. You need to use a subroutine and call it 'COUNT' to do so. You also need to provide the count in BCD if it was less than 64h so that you need to include another subroutine called 'ToBCD' to do so. assembly 8086
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT