Showing posts with label LOGO. Show all posts
Showing posts with label LOGO. Show all posts

Thursday, June 3, 2010

Displaying messages

;here is a simple word
to sayHi

display-bits $00 $00 $76 $06
end

;this sub-procedure is required whenever you use the display-bits command
to display-bits :d1 :d2 :d3 :d4
bsend $110
bsend 64
bsend low-byte :d1
bsend low-byte :d2
bsend low-byte :d3
bsend low-byte :d4
end

Commands for Cricket-MicroWorlds communication

  • sendmessagetomicroworlds: This command will send a numerical message to the MicroWorlds program. For example, if you tell the Cricket "sendmessagetomicroworlds 1" the robot will send the number 1 to MicroWorlds.

  • messagefromrobot: This command reads the message sent from the robot. For example, if you tell MicroWorlds 
if messagefromrobot = 1 [melody1]

MicroWorlds will play a melody file if it reads the number 1 from the robot. You would want to loop this, as in a button set to forever.

Basic Sender and Receiver Procedures

For communicating between crickets:


to send-it

send 1
wait 10
send-it
end

to receive-it
waituntil [newir?]
if ir = 1 [beep]
receive-it
end

Using a Variable to Count

Until now you've had to write each new number you want the LED to display as it counts to 10. If you wanted it to count higher this would start to get tedious. To avoid the tedium you can use a variable. It is similar to a variable in algebra in that you use a name that stands for a value that can be changed.

Here is how:

global [number] ;this creates a variable and names it

to count

setnumber 1 ;this sets the value of the variable
repeat 10 [
display number ;this displays the current value of the variable
wait 5
setnumber number + 1 ;this sets the value again, adding one to the current value
]
end

What is tail recursion?

Tail recursion is another way to loop commands. With loop or repeat you give the command and then in brackets specify what is to be repeated. Tail recursion is simpler and a bit of a trick. Here's how:

to count

display 1
wait 5
display 2
wait 5
display 3
wait 5
count
end

See? If you put the name of the procedure at the end of the procedure itself, it will just keep looping itself!

Two Ways to Loop

Two Ways to Loop

There are two commands that can make other commands repeat.
Loop makes the commands repeat indefinitely:

to beepForever
loop [beep wait 2]
end

Repeat makes the commands repeat as many times as you tell it to:

to beepFiveTimes
repeat 5 [beep wait 2]
end

What if you want the LED display to not display anything?

Problem

The LED display will automatically turn off after a little while if it's idle to conserve battery power. But how can you make it go blank right away?

Solution

You have to use a procedure called "display-bits." Here it is:
to display-bits :d1 :d2 :d3 :d4
bsend $110
bsend 64
bsend low-byte :d1
bsend low-byte :d2
bsend low-byte :d3
bsend low-byte :d4
end

Bits are the individual bars that can light up in each of the four spaces. D1, d2, d3, and d4 stand for the four spaces. To make all of the bits turn off, you would send the command "display-bits $00 $00 $00 $00."

Applying the solution

If you only sent this command, the Cricket wouldn't know how to "display-bits," so here is an example of how you use this command. Notice that you have to include the "display-bits" procedure because it's acting as a sub-procedure to "count." The "count" procedure will make it count 1, 2, then go blank.
to count
display 1
wait 5
display 2
wait 5
display-bits $00 $00 $00 $00
end

to display-bits :d1 :d2 :d3 :d4

bsend $110
bsend 64
bsend low-byte :d1
bsend low-byte :d2
bsend low-byte :d3
bsend low-byte :d4
end

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

Tuesday, November 17, 2009

Programming a Switch

I've been working with RobotC for a bit now and I've come to realize something about the way switches can be programmed in Logo. Logo allows you to write this implied boolean equation with if switcha [a, on] or if not swticha [a, off], while boolean equations are true or false or 0 or 1 in other programming languages. So I tried programming a switch with if switcha = 1 [a, on] and it worked just as well. I much prefer that approach because of its similarity to most other programming languages.

Saturday, October 25, 2008

A better way to teach making a timer

Last year I taught my 6th graders how to make a timer for an application we were making by using all of the primitives available. So I showed them how to use resett and print the time every. second with 'timer / 10'. I missed a great opportunity that I'm finally talking advantage of. In one fell swoop I can teach them how Logo can do math, loop, and use variables. Here's the model for the timer we're working with now:
to start-timer
make "seconds "0
repeat 60
[time, pr :seconds
wait 10
ct
make "seconds :seconds + 1]
end
With this approach my students this year are getting what were doing, much better than last year.

Wednesday, October 22, 2008

Teaching is a political act

I came across this paper that traces the history of LOGO in terms of its function in most schools. Agalianos argues that, while the creators of LOGO envisioned it as a tool for children and teachers to subvert the institutional controls on learning and teaching by inventing their own individualized math curricula, it was introduced into schools in the 80s at a time when the dominant institutional philosophy was swinging from relatively progressive to conservative. In this "back to basics" climate, which continues today, LOGO wasn't rejected, but integrated into the standards and required as its own subject separate from the rest of the curriculum. Its function has ended up in many schools as no more than a "turtle graphics" program, a fun environment for kids to practice problem-solving skills. The tension between the possibilities inherent in LOGO and the constraints of the over-scheduled and fragmented school environment has been a preoccupation of mine since I began teaching technology. What is the purpose and larger context of what I'm teaching? For now I'll just keep asking.