CS 1020 - Winter 2013 - Class 3
Before class
- setup Sciborg Grand Prix
- bring prize
- bring in 8-ball course
- new groups
Sciborg Grand Prix!
Administrative
- Save the date: Wednesday 1/30 2-5pm Final project Talent Show
- Visit schools some afternoon?
- Friday 1/18 or
- Wednesday 1/23 or
- Friday 1/25
- During lectures, please don't work on your robots, code, etc.
- course web page resources: use them!
- keep up with the readings
- notes posted online for each class
- code examples from class posted in the resources section (and also generally available linked from the notes)
Handy board boot loader
- sometimes the Handy boards will crash or freeze up
- when this happens the fix is often to reinstall the boot load (basically the OS for the Handy board
- there is a link under the "Resources" section of the course web page
- follow the instructors
- you'll have to reload your program after reinstalling the boot loader
- if this doesn't fix it, you'll have to bug one of us since it's probably a bigger issue
Revisiting the print command
- Last time we saw that you can print a single word using
print "word
- or print multiple words by enclosing them in square braces
print [more than one word]
- or print sensor values
print (sensor 0)
type command
- Sometimes you might want to print words AND sensor values
- the way to do that is using the "type" command
- when you use the print command, the next time a print command is issues it resets the line and starts from scratch
- the type command doesn't do this!
- instead, the type command displays some text on the screen, but the next type/print command simply appends on to what is already there
- So, if you want to put a label and a sensor value on the screen you first type the label and then print the sensor value:
type [sensor 0:] ; print the text "sensor 0:"
print (sensor 0) ; adds the actual value of sensor 0
- You can do multiple type statements in a row to continually concatenate things, but the last thing will always be a print:
type "i
type "like
print "cs
top and bottom
- if you notice, the LCD screen actually consists of two lines
- by default, print and type modify the top line of the LCD display
- to top and bottom commands change where the text should be printed, e.g.
top
print [i like cs]
bottom
type "a
print "lot
say
- the say command works similarly to the print command, but instead of printing on the LCD screen it prints to the right side of the console
- this can be very useful for debugging
mathematical operators in Handy Logo
- As I mentioned before, Handy Log supports the basic mathematical operators (+, *, /, \--remainder)
- All math operators MUST be separated by a space:
- 2 + 3 (GOOD)
- 2+3 (BAD)
- Using say, we can see the value of some of these:
say 2 + 3
say 3 * 4
say 4 / 2
say 5 / 2
say 5 \ 2
say 2 + 3 * 2
say 2 * 3 + 2
say 2 * (3 + 2)
- Anything interesting from these examples?
- integer division (i.e. 5 / 2 gives us 2)
- evaluation happens left to right (operator precedence does NOT matter)
- parenthesis does affect precedence
repeat
- write a function that beeps twice:
to double-beep
beep
wait 5
beep
end
- write a function that beeps 4 times:
to quad-beep
repeat 4[
beep
wait 5
]
end
- write a function that beeps an arbitrary number of times based on how I call the procedure, e.g.
multi-beep 10
would beep 10 times
parameters
- so far all of our procedures have just been shortcuts for executing other statements
- parameters allow us to pass information to a procedure when we call it (like 10 above)
- parameters is Handy Logo are specified in the same line where we declare the name of the procedure and are prepended by a ':'
- you can have as many as you'd like
- the name you give them (with the :) are then usable inside the program
- Using parameters we can easily implement multi-beep
to multi-beep :n
repeat :n[
beep
wait 5
]
end
- Try and write these functions that use parameters
- beep-greater-ten: beeps if the value called is great than 10
- print-bottom: takes one parameters and prints it on the bottom of the screen
- print-top-bottom: takes two parameters and prints the first on the top and the second on the bottom
- print-sum: takes two numbers as parameters and adds them and prints them to the screen
returning values
- parameters allow us to pass values to functions
- we also may want to GET information back from our procedures
- this is called "returning" a value
- to do this we use the keyword "output"
- when the procedure executes the output statement it finishes the procedure, so don't have any statements following it
- For example, if we wanted to write a square procedure that squares a value:
to square :num
output :num * :num
end
Look at the
area_calculator.txt code
- We can use procedure that return values in places where values are expected
- the circle_area procedure calls the square procedure and then uses the value returned to calculate the area
- the print_circle_area procedure calls the circle_area procedure and uses the value to display it
- we can call the print_circle_area program from the HandyLogo.jar console with a radius to see it run:
print_circle_area 10
code style
Light sensors
- the light sensors record values from 0 to 255 from very light to very dark (though it's hard to get values much darker than 100 in any conditions where there's ambient light)