Electronic Technologies for Art II (electric boogaloo)
Mark Allen
UCSD
Intro
- programming 101
- digital input
Loop
- tells the computer to go back to the beginning when it finishes its instructions
- in pbasic we do this using a label and a goto statement
- '------------------------------------------------------
- Main:
- 'do something
- Goto Main
Debug statement
- A debug statement allows the stamp to write output back to the pc
- debug statements are very useful for, well, debugging
- '--------------------------------------------------------------------------
- Main:
- debug "hello mark" ' we can write strings to the pc by enclosing them in quotation marks
- debug CR ' to generate a carriage return (so our output gets a new line) we use CR with no quotes
- debug DEC 1 ' to print numbers we proceed the value with DEC to tell the stamp we want a decimal number
- debug "happy ", dec 99, "th", " birthday", CR ' we can print multiple
things with one debug statement if we separate them with commas
- Goto Main
Variables
- Variables are a way of storing data in the computers memory
- In basic variables must declared.
- The variable declaration gives the variable a name and defines how data can be stored in it
- Variable names should be descriptive
- Variables in pbasic can be of size bit (0-1), nib (0-15), byte (0-255) and word (0-65535)
- When declaring a variable is pbasic we write the variable name, the
word var, then the size of the variable
- mybit var bit
- myword var word
Variable example
- An example program showing the use of a variable
- Note what happens when we try and make a byte variable hold more than 255
- '----------------------------------------------------------
- mycounter var byte ' note that variables should be declared in the very beginning of the program
- Main:
- mycounter = mycounter + 1 'increases the value of mycounter by 1 each time
- debug "mycounter = ", DEC mycounter, CR ' output the value of mycounter as a decimal to the debug window
- goto Main
Constants
- We can use constants to help us remember abstract values
- '----------------------------------------------
- greenled con 9 ' the green led is connected to pin 9
- redled con 10 ' the red led is connected to pin 10
- main:
- high redled
- low greenled
- pause 100
- low redled
- high greenled
- pause 100
- goto main
If statements
- If statements allow the computer to make decisions based on conditions we define
- If condition is true, the lines of code after the if statement are executed
- If condition is false, the lines of code after the if statement are skipped
If statement example
- Main:
- mycounter = mycounter + 1 'increases the value
of mycounter by 1 each time
- debug "mycounter = ", DEC mycounter, CR ' output
the value of mycounter as a decimal to the debug window
- if mycounter > 9 then ' checks value of mycounter
- mycounter = 0 ' if above statement is true, this
line is executed
- endif ' if statements must be terminated so the
computer knows where to start up again if condition is false
- goto Main
Measuring input
- To measure the voltage on a pin first we have to turn that pin into an input
- The following command turns pin 7 into an input
- Input 7
- Once the pin is an input we can read the value of it
- debug DEC in7
Digital input schematic
note the pull down resistor to prevent the input pin from floating
Digital input example
- greenled con 9 ' the green led is connected to pin 9
- redled con 10 ' the red led is connected to pin 10
- Main:
- if in0 = 1 then
- high redled
- low greenled
- else
- low redled
- high greenled
- endif
- goto main