Finch Robot Array Exercises:
1. First, get a little practice with straight arrays, no robot.
Create an array of 10 integers. Ask the user to enter 10 numbers,
putting each number into one box of the array. After the user is
done entering numbers, print all the numbers they entered.
(Print the array.)
import java.util.Scanner;
public class ActionArray2
{
public static void main(final String[] args)
{
Scanner mine = new
Scanner(System.in);
int block[] = new int[10] ;
for (int count = 1 ;
count < 10; count++){
System.out.println("Enter number to hold in block #" + count);
block[count] = mine.nextInt();
}
int tot = 0;
for (int count = 1 ;
count < 10; count++){
System.out.println("The number in block " + count + " is " +
block[count]);
tot = tot + block[count];
}
System.out.println("total
is " + tot);
}
}
2. Copy your program above and change it to ask the user to enter L,
R or F 10 times. Change your array to a character array so it can
old the L,R or F. When the user is done, tell the finch to
execute each instruction the user entered. If the user entered
L, turn left . For R, turn Right and for F go forward. (Right is
myFinch.setWheelVelocities(-255,255); ) After each instruction is
executed, ask the finch to sleep for 1 second so it has time to
move. Print All done to the screen when you are done.
import edu.cmu.ri.createlab.terk.robot.finch.Finch;
import java.util.Scanner;
public class ActionArray
{
public static void main(final String[] args)
{
Finch myFinch = new
Finch();
Scanner mine = new
Scanner(System.in);
char block[] = new
char[10] ;
for (int count = 1 ;
count < 10; count++){
System.out.println("which direction should it go (L or R or F) in
block #" + count);
String ans = mine.nextLine();
block[count] = ans.charAt(0);
}
for (int count = 1 ;
count < 10; count++){
if ( block[count] == 'L' )
{
myFinch.setWheelVelocities(255,-255);
myFinch.setLED(255,0,0);
}
else if (block[count] == 'R' )
{
myFinch.setWheelVelocities(-255,255);
myFinch.setLED(0,255,0);
}
else {
myFinch.setWheelVelocities(255,255);
myFinch.setLED(0,0,255);
}
myFinch.sleep(1000);
}
// Always end your
program with finch.quit()
// myFinch.sleep(10000);
System.out.println("done");
myFinch.quit();
System.exit(0);
}
}
3. Explore the light sensors. Make a new program that asks the user
if they are read to record the light value. When the user enters y,
get the value of the light sensors and print them both to the
screen. Keep asking the user and recording the value until the user
enters n. When you run your program, try covering each eye to see
how much light a covered and uncovered eye has.
import edu.cmu.ri.createlab.terk.robot.finch.Finch;
import java.util.Scanner;
public class SeeEye
{
public static void main(final String[] args)
{
Finch myFinch = new
Finch();
Scanner mine = new
Scanner(System.in);
int lights[] ;
System.out.println("are
you ready to record the light value");
String ans = "y" ;
while (ans.equals("y"))
{
ans = mine.nextLine();
if (ans.equals("y")){
lights = myFinch.getLightSensors();
System.out.println("I see that you have a left light sensor of " +
lights[0] +
" and a right light sensor of " + lights[1]);
}
}
// Always end your
program with finch.quit()
// myFinch.sleep(10000);
System.out.println("done");
myFinch.quit();
System.exit(0);
}
}
4. Make a game in which a random number tells the player to cover
either the right or left eye. Get a number of 0 or 1. 0 will mean
the left eye should be covered and red will mean the right eye
should be covered. When the left eye should be covered, show red
(myFinch.setLED(255,0,0 );) Otherwise, show green. The robot should
wait until the eye is covered and then change the color to Blue.
(Use a while loop that keeps checking for the expected light value.
It will have to check being less than a value, not an exact value.
Let the answer to #3 inform you of what values will work to indicate
a covered eye.) Enhance this game to repeat 10 times.
import edu.cmu.ri.createlab.terk.robot.finch.Finch;
import java.util.Random;
import java.util.Scanner;
public class CoverEye
{
public static void main(final String[] args)
{
Finch myFinch = new
Finch();
double[] actionLength =
new double[10];
double[] actionDirection
= new double[10];
double[] finchInput = new
double[3];
double[] finchInputStart
= new double[3];
int lights[] ;
// Instantiating the
Finch object
boolean correctAction =
false;
Random r = new Random();
//
myFinch.setWheelVelocities(255,-255,1000);
for (int x = 1; x <
10; x++){
int choice = r.nextInt(2); // 0= left; 1 = right;
correctAction = false;
if (choice == 0)
{
myFinch.setLED(255,0,0 ); // red means cover left eye
System.out.println("Cover my left eye");}
else
{myFinch.setLED(0,255,0 ); // green means cover right eye
System.out.println("Cover my right eye");}
while (!correctAction){
lights = myFinch.getLightSensors();
System.out.println("I see that you have a left light sensor of " +
lights[0] +
" and a right light sensor of " + lights[1]);
if (lights[choice] < 60) {
correctAction = true;
System.out.println("You covered the correct eye!");
}
}
}
myFinch.setLED(0,0,255 );
// Always end your
program with finch.quit()
// myFinch.sleep(10000);
System.out.println("done");
myFinch.quit();
System.exit(0);
}
}