import org.firmata4j.firmata.*;import org.firmata4j.IODevice;import org.firmata4j.Pin;import java.io.IOException;import java.text.ParseException;import java.util.Date;import java.util.Scanner;import java.text.SimpleDateFormat;public class lab07P2 {public static void main(String[] args)throws IOException, ParseException {String myPort = "/dev/cu.usbserial-0001"; // modify for your owncomputer & setup.IODevice myGroveBoard = new FirmataDevice(myPort);try {myGroveBoard.start();System.out.println("Board started.");myGroveBoard.ensureInitializationIsDone();} catch (Exception ex) {System.out.println("couldn't connect to board.");} finally {/* you should develop this block */}}public static Date getAlarmDate() throws ParseException {Scanner input = new Scanner(System.in);System.out.println("This program uses Arduino board to act as analarm clock.");System.out.println("Below, you can enter your desire date and timeto set an alarm.");String dateStr;Date DT;while (true) {System.out.print("Enter four digits the year (e.g. 2022): ");int year = input.nextInt();System.out.print("Enter two digits the month (e.g. 06): ");int mm = input.nextInt();System.out.print("Enter two digits the day (e.g. 28): ");int dd = input.nextInt();System.out.print("Enter two digits the hour (e.g. 07): ");int HH = input.nextInt();System.out.print("Enter two digits the minutes (e.g. 12): ");int MM = input.nextInt();System.out.print("Enter two digits the second (e.g. 52): ");int SS = input.nextInt();if (!(year < 2022 || mm > 59 || mm < 0 || dd < 0 || dd> 31 || HH < 0 || HH > 24 || MM < 0|| MM > 59 || SS < 0 || SS > 59)) {dateStr = "" + dd + "/" + mm + "/" + year + " " + HH + ":" + MM +":" + SS;SimpleDateFormat frmt = new SimpleDateFormat("dd/MM/yyyyHH:mm:ss");DT = frmt.parse(dateStr);Date NOW = new Date();if (NOW.before(DT)) {System.out.println("The entered alarm time is " + DT);break;}}System.out.println("The entered date is not correct. Please reenteryour desire date");}return DT;}}
You should develop the rest of the main method (as mentioned inthe program above) such that your program runs as below:
1- The program asks user to enter the desire alarm date andtime. You should use the getAlarmDate method for this.
2- Your code should check continually whether the current timehas passed the entered date or not. (Hint: search whetherdate1.after(date2) or date1.before(date2) from java.util.Data isuseful)
3- Right after the current time passes the alarm time, Arduinobuzzer should start beeping for 30 times. The buzzer beep should beset up as 100 milliseconds on and 100 milliseconds off.
4- Note that you should be able to stop the buzzer by pressingthe button for less than half a second.
import org.firmata4j.firmata.*; import org.firmata4j.IODevice; import org.firmata4j.Pin; import java.io.IOException; imp
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am