-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherSensor.java
More file actions
27 lines (24 loc) · 920 Bytes
/
WeatherSensor.java
File metadata and controls
27 lines (24 loc) · 920 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.Random;
import java.util.concurrent.TimeUnit;
/**
* Sensor Simulation: Produces continuous voltage values
*/
public class WeatherSensor {
public static void main(String[] args) {
Random random = new Random();
System.out.println("--- Weather Station: Sensor Component Started: ");
// Runs continuously with a message if failed
while (true) {
try {
// Generate a voltage value between 0.0 and 5.0
double voltage = 5.0 * random.nextDouble();
System.out.printf("[SENSOR] Current Output: %.2fV%n", voltage);
// Produces a value every 1 second to have a consistent value time
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("[SENSOR] Simulation interrupted.");
break;
}
}
}
}