Thursday, June 3, 2010

To Display Numbers

The command that makes the LED display numbers is "display".
Before the Cricket can access this command you will probably have to do the following:

  1. Make sure the procedures window ("Cricket Logo Programs") is empty.
  2. Click the download button to clear out the Cricket's memory.
  3. Now type "display 1234" in the command center and hit enter.
  4. If the LED display shows the number 1234 it is now working.

What is a conditional statement?

What is a conditional statement?
It's when you tell your robot to do something only if a certain condition is true. If the condition is not true, the robot won't do it.
We use conditional statements all the time in real life, like when we say, for example, "If they are serving fish for lunch, I'm going to the salad bar." 

In Logo, this would look something like this:

to eat.lunch
if fish
[eat saladbar]
end
(Note: I don't have anything against fish.)
Your robot can't eat, though, so let's see what it could do as a result of a conditional statement.

If you want your robot to start driving if you press a switch, this will do it:

to drive
if switcha
[ab, onfor 20]
end
Notice where the brackets go! They enclose the 'if true' command like bread on a sandwich.
If you want your robot to stop driving if you press a switch, this will do it:

to stop.driving
ab, on
if switcha
[ab, off]
end
Well, actually, I kind of lied. The two examples above won't really work because they check if the switch is pressed only once. We need it to check continuously so it will carry out the commands whenever we press the switch. So we have to "loop" the procedure, like this:

to drive

loop [
if switcha
[ab, onfor 20]
]
end

This example will keep checking whether the switch is pressed because loop makes the part inside the brackets repeat indefinitely.

Here's how you can use a switch to control your robot:
Use a switch to start your robot driving on its road course. First plug a switch into sensor port 'a'. 

You can keep your program the way it is and just wrap a conditional statement around it.
to start
loop [
if switcha
[drive]
]
end

to drive
ab, thisway
ab, on
wait 20
etc, etc,
end

Now be sure to put the name 'start' in the 'run this' window so when you press the run button on the robot, it will start the loop instead of just skipping the loop and going straight to the 'drive' procedure.

Using Sub-procedures

Sub-procedures are separate procedures in your program that are called upon in the main procedure. Here is what one looks like:


to drive
ab, thisway
ab, on
wait 20
turnright ;sub-procedure is being called upon here
wait 30
end

to turnright ;here is the sub-procedure
b, off
wait 10
b, on
end

So "turnright" is a sub-procedure of "drive." "Drive" is the main procedure. In other programming languages a sub-procedure is called a function but it basically works the same.

The benefit of using a sub-procedure is that it makes your program much more efficient and can save you a lot of work.

How will you use this in your program? 
In your procedures window, add a procedure called "turnright" and type in the commands that will make your car turn right. Do the same to add another procedure called "turnleft." In your main procedure every time there is a right turn, remove those commands and just type "turnright." Do the same for left turns. You will have to test your program to make sure the turnright and turnleft sub-procedures turn the correct amount, but all of the turns are right angles, so if it works once it should work for the rest.

Your Robot Follows Commands in a Sequence

How is this:

to drive
beep
ab, on
wait 30
ab, off
end
different from this?
to drive
ab, on
wait 30
ab, off
beep
end
The first procedure beeps, turns motors a and b on, waits 3 seconds, and turns motors a and b off. The second procedure beeps after the motors turn on and off.

What is the rule the Cricket is following?
The Cricket will follow the sequence of commands from the top of the procedure to the bottom.

Monday, December 21, 2009

Switch-Operated Elevator

Two of my 9th grade students created this elevator. It raises up at the press of a switch, then waits for another press before descending. The motors also gear down for increased torque.

Saturday, December 12, 2009

Ways to Program a Loop

If you need a switch to work repeatedly when you run its program, you need to loop it. Here are some ways depending on exactly how you want them to work:
;this strategy repeats indefinitely
to main
loop
[commands to loop]
end

;this strategy makes the program run itself indefinitely
to main
commands to loop
main
end

;this strategy repeats the commands many times
to main
repeat 100
[commands to loop]
end

Many Ways to Program Switches

;simplest way, one switch
to go
waituntil [switcha = 1]
motor commands
waituntil [switcha = 1]
more motor commands
end

;another way, one switch
to go
if switcha = 1
[motor commands]
if switcha = 0
[more motor commands]
end

;another way, one switch
to go
ifelse switcha = 1
[motor commands]
[more motor commands]
end

;two switches!
to go
if switcha = 1
[motor commands]
if switchb = 1
[more motor commands]
end