PLEASE, use this comment to fix/correct my code: 1. the counting of the digital
ID: 3786674 • Letter: P
Question
PLEASE, use this comment to fix/correct my code:
1. the counting of the digital clock should not happen in the main() function. we need to choose a tool for it (timer, timeout or ticker for it)
2. the interrupt service routine needs to be a separately designed function (your toggle) but it needs to read the temperature and then display it.
The below is my code/syntax:
#include "mbed.h"
#include "TextLCD.h"
#include "TMP102.h"
I2C temp(p9, p10); //A0 pin is connected to ground
InterruptIn event(p21);
TextLCD lcd(p15, p16, p17, p18, p19, p20);
int hours, minutes, seconds;
void trigger(){
lcd.printf("Temp:% 3.1f");
}
int main()
{
event.rise(&trigger);
while(1) {
lcd.cls();
lcd.locate(0,0);
if(seconds==60){
seconds=0;
minutes++;
}
if(minutes==60){
minutes=0;
hours++;
}
if(hours==24){
hours=0;
}
lcd.printf("%02d:%02d:%02d", hours, minutes, seconds);
seconds++;
wait(1);
}
}
Please your timely respond and answer will be much appreciated.
Thank you.
Explanation / Answer
1 program)
import java.util.*;
import java.awt.*;
import java.applet.*;
/* <applet code=DigitalClock width=300 height=150>
</applet>
*/
public class DigitalClock extends Applet implements Runnable
{
boolean stopFlag;
String display = "";
Thread th;
int hours, minutes, seconds;
public void init()
{
setBackground(Color.cyan);
setForeground(Color.blue);
stopFlag = true;
}
public void start() {
if(stopFlag)
{
th = new Thread(this);
stopFlag = false;
th.start();
}
}
public void stop() {
stopFlag = true;
th = null;
}
public void paint(Graphics g) {
g.setFont(new Font("Ariel", Font.BOLD, 36));
display = String.valueOf(hours) + ":" + String.valueOf(minutes) + ":" + String.valueOf(seconds);
g.drawString(display, 50, 50);
g.drawString(String.valueOf(hours), 50, 100);
}
public void run()
{
for(;;) {
repaint(500);
Calendar cal = Calendar.getInstance();
hours = cal.get(Calendar.HOUR);
minutes = cal.get(Calendar.MINUTE);
seconds = cal.get(Calendar.SECOND);
if(stopFlag)
break;
try { Thread.sleep(500);}
catch(InterruptedException e) {
System.out.println("e");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.