|
import josx.platform.rcx.*; /** * Plays Marry had a little lamb as the robot * moves arround the floor * **/ public class Mary{ public static void main (String[] args) throws Exception { // the length of the note plus the length // of the pause. 40 centiseconds = 400 milliseconds // so 400 milliseconds of note and 100 milliseconds of pause int pause = 500; // Robot starts forward Motor.A.forward(); Motor.C.forward(); // begin playing song Sound.playTone(494,40);// B Thread.sleep(pause); Sound.playTone(440,40);// A Thread.sleep(pause); Sound.playTone(392,40);// G Thread.sleep(pause); Sound.playTone(440,40);// A Thread.sleep(pause); Sound.playTone(494,40);// B Thread.sleep(pause); Sound.playTone(494,40);// B Thread.sleep(pause); Sound.playTone(494,40);// B Thread.sleep(pause); // Robot turns right Motor.A.backward(); Sound.playTone(440,40);// A Thread.sleep(pause); Sound.playTone(440,40);// A Thread.sleep(pause); Sound.playTone(440,40);// A Thread.sleep(pause); Sound.playTone(494,40);// B Thread.sleep(pause); Sound.playTone(294,40);// D Thread.sleep(pause); Sound.playTone(294,40);// D Thread.sleep(pause); Sound.playTone(494,40);// B Thread.sleep(pause); // Robot goes backward Motor.C.backward(); Sound.playTone(440,40);// A Thread.sleep(pause); Sound.playTone(392,40);// G Thread.sleep(pause); Sound.playTone(440,40);// A Thread.sleep(pause); Sound.playTone(494,40);// B Thread.sleep(pause); Sound.playTone(494,40);// B Thread.sleep(pause); Sound.playTone(494,40);// B Thread.sleep(pause); Sound.playTone(494,40);// B Thread.sleep(pause); // Robot goes forward Motor.A.forward(); Motor.C.forward(); Sound.playTone(440,40);// A Thread.sleep(pause); Sound.playTone(440,40);// A Thread.sleep(pause); Sound.playTone(494,40);// B Thread.sleep(pause); Sound.playTone(440,40);// A Thread.sleep(pause); Sound.playTone(392,40);// G Thread.sleep(pause); // Robot Stops Motor.A.stop(); Motor.C.stop(); } } |
|
import josx.platform.rcx.*; import java.util.Random; /** * Moves the robot randomly arround the floor * randomly turns right or left and makes a * different sound for each **/ public class Rand { public static void main (String[] args) throws Exception { // start the random number generator int rand; // var to temperarily hold random values // set motors to their top power Motor.A.setPower(7); Motor.C.setPower(7); // create an infinite loop so the robot will continue // the program till turned off while (true){ // robot forward for a random time 0 - 2 seconds Motor.A.forward(); Motor.C.forward(); rand = (int)(Math.random() * 2000); Thread.sleep( rand ); // robot backward for a random time 0-2 seconds Motor.A.backward(); Motor.C.backward(); rand = (int)(Math.random() * 2000); Thread.sleep( rand ); // randomly turn left or right for a random time // 0 - 2 seconds, play a different sound for // left and right rand = (int)(Math.random() * 2000); if (rand > 1000) { Motor.A.forward(); Sound.systemSound(true, 2); // play decending sound } else { Motor.C.forward(); Sound.systemSound(true, 3); // play accending sound } rand = (int)(Math.random() * 2000); Thread.sleep( rand ); } } } |