Skip to content

Commit

Permalink
bug fix VibrationMotorApp
Browse files Browse the repository at this point in the history
  • Loading branch information
DieterHolz committed Aug 6, 2023
1 parent 35a7883 commit 92f9665
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/main/java/com/pi4j/crowpi/applications/VibrationMotorApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import java.util.Scanner;

/**
* This example shows how to use the vibration motor. It basically represents a vibrating alarm clock which is going off and keeps making
* noises until the user confirms being awake by writing `yes` to the console / standard input. Please note that this example requires the
* This example shows how to use the vibration motor.
* It basically represents a vibrating alarm clock which is going off and keeps making
* noises until the user confirms being awake by writing `yes` to the console / standard input.
* <p>
* Please note that this example requires the
* DIP switch 2-1 to be activated for it to work.
*/
public class VibrationMotorApp implements Application {
Expand All @@ -20,26 +23,25 @@ public void execute(Context pi4j) {
vibrationMotor.on();
sleep(500);
vibrationMotor.off();
sleep(1000);

// Write to ask the user something
System.out.println("Are you awake? Answer with YES if you are...");
// Scanner is used to read the input from the commandline
// Scanner is used to read the input from the console
Scanner scanner = new Scanner(System.in);

// Loops until YES is found
while (!scanner.nextLine().equalsIgnoreCase("YES")) {
// Loops until YES is entered by user
do {
// Do some pulses to create a strange noise
for (int i = 10; i < 100; i++) {
for (int i = 100; i < 500; i=i+100) {
// As you see use pulse is a lot more comfortable than using .on -> sleep -> off
vibrationMotor.pulse(i);
sleep(i);
}
for (int i = 100; i > 10; i--) {
for (int i = 500; i > 100; i=i-100) {
vibrationMotor.pulse(i);
sleep(i);
}
System.out.println("Are you awake? Answer with YES if you are...");
}
} while (!scanner.nextLine().equalsIgnoreCase("YES"));

// User is awake, so we can turn off the vibration motor now.
vibrationMotor.off();
Expand Down

0 comments on commit 92f9665

Please sign in to comment.