Javelin 2 - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Javelin 2

Description:

However, if you don't use an import stamp.core. ... while (true) { // do while loop forever. for (int i=0; i 255; i ) { // loop 0v to 5v ... – PowerPoint PPT presentation

Number of Views:74
Avg rating:3.0/5.0
Slides: 19
Provided by: Arel8
Category:
Tags: forever | javelin | stamp

less

Transcript and Presenter's Notes

Title: Javelin 2


1
Javelin 2
  • By Wilmer Arellano

2
Overview
  • Making Decisions
  • Repetitive Operations
  • Displaying Messages from the Javelin
  • Circuit Examples

3
Making Decisions
  • if (xgt100)
  • System.out.println("Limit exceeded!")
  • Notice that the test expression is in
    parenthesis. You can also test for equality (two
    equal signs ), less than (lt), less than or
    equal to and greater than or equal to (lt or gt),
    and not equal (!). These operators all return
    Boolean values, either true or false. You can
    also put any expression that returns a Boolean in
    the parenthesis such as a Boolean variable.
  • The statement after the parenthesis will only
    execute if the expression in parenthesis is true.
    If you want more than one statement to be
    executed if the condition is true, youll need to
    surround the multiple statements with braces
  • if (xgt100)
  • System.out.println("Limit exceeded!")
  • System.out.println("Please press reset")
  • It is allowable to use braces even if you have
    one statement.

4
Making Decisions
  • if (xgt100)
  • System.out.println("Limit exceeded!")
  • System.out.println("Please press reset")
  • else
  • System.out.println("Process nominal.")
  • You may want to test several different conditions
    together. You can join Boolean expressions with
    the (logical and) and (logical or)
    operators. You can also reverse the sense of a
    Boolean expression with the ! (not) operator.
    This code fragment tests that x is greater than
    zero and also less than 100
  • if (xgt0 xlt100) System.out.println("In range")
  • For efficiency, the program will stop testing
    values as soon as it is certain what the end
    result is. For example, suppose x is 0 in the
    above example. The program will test xgt0. Since
    this is not true, the program will immediately
    stop testing and go to the next statement.

5
Repetitive Operations
  • Java has many ways to control program loops.
  • The dowhile loop always executes once. At the
    end of each execution, the program decides if it
    should execute the loop again or continue with
    further processing.
  • int i0
  • do
  • System.out.println(i)
  • ii1
  • while (ilt10)
  • If you initialized the i variable at, say, 100,
    the loop would print 100, compute a new i (101)
    and then exit the loop since 101 is not less than
    or equal to 10.
  • Adding one to a variable is so common that Java
    has a shortcut for it, the increment
    operator.i in place of ii1

6
Repetitive Operations
  • int i0
  • do
  • System.out.println(i)
  • while (ilt10)
  • The same principles apply to a while loop
  • int i0
  • while (ilt10)
  • System.out.println(i)
  • i
  • If you change this example so that i starts out
    at 100, nothing will print since the loop will
    never execute.

7
Repetitive Operations
  • int i
  • for (i0 ilt10 i)
  • System.out.println(i)
  • You can even declare the variable in the first
    clause (as long as you only need it within the
    loop)
  • for (int i0ilt10i)
  • System.out.println(i)
  • The for loop has three parts or clauses. The
    first clause executes code before the loop starts
    for the first time. The second clause tests for
    loop completion. The third clause executes after
    every loop.
  • You can even write endless loops using any of the
    three loop primitives
  • for () . . .
  • do . . . while (true)
  • while (true) . . .

8
Repetitive Operations
  • Sometimes you want to exit a loop early. You can
    do this with the break keyword. For example
  • for (i0ilt10i)
  • if (i 3)
  • break
  • System.out.print(i)
  • System.out.println(Skipping 3 and above)
  • You can also cause a loop to proceed to the next
    iteration (if any) by using continue. Suppose you
    wanted to count from 0 to 10, but you want to
    skip 5. There are many ways you might write this,
    heres one way
  • for (i0ilt10i)
  • if (i5) continue // proceed to i6
  • System.out.println(i)

9
Displaying Messages from the Javelin
  • public static void main()
  • System.out.print("Hello ") //Does not add a new
    line
  • System.out.println("World") //Adds a new line
  • System.out.println("The threshold is " t "
    degrees.") //Concatenates
  • CAUTION
  • The Javelin does not support garbage collection
    and the compiler will create strings that the
    Javelin can never recover. If you use a command
    that uses the (concatentaion) operator within a
    loop, you will run out of memory very quickly
  • same result without having to worry about the
    lack of garbage collection.
  • int t100
  • System.out.print(The threshold is )
  • System.out.print(t)
  • System.out.println( degrees.)

10
Displaying Messages from the Javelin
  • Using a string variable
  • StringBuffer bufnew StringBuffer(32) // 32 byte
    string
  • buf.append("The temperature is ")
  • buf.append('7')
  • buf.append('0')
  • buf.append(" degrees")
  • System.out.println(buf.toString())
  • Ussing the CPU.message method
  • String test"Parallax Javelin"
  • CPU.message(test.toCharArray(),test.length())
  • String test"\nParallax Javelin\nWow!"
  • CPU.message(test.toCharArray(),test.length())
  • Notice that the System and CPU objects are
    static. You dont need to create these objects
    before using them. They are always present.
    However, if you dont use an import stamp.core.
    or similar statement, youll have to refer to the
    CPU object by its full name stamp.core.CPU.
  • Run Example

11
Sending Messages to the Javelin
  • Capitalize Example
  • There is never a need to create a terminal
    object, it is always available. You must import
    stamp.core or use the full name
    stamp.core.Terminal

12
Circuit Examples
13
Digital to Analog Conversion
  • import stamp.core. // Import Javelins classes
  • public class MakeVoltage // class declaration
  • public static void main() // main declaration
  • DAC dac new DAC(CPU.pin14) // create new DAC
    object
  • while (true) // do while loop forever
  • for (int i0 ilt255 i) // loop 0v to 5v
  • dac.update(i) // Update DAC with new voltage
  • CPU.delay(1000) // Delay
  • // end for
  • for (int j255 jgt0 j--) // loop 5v to 0v
  • dac.update(j) // Update DAC with new voltage
  • CPU.delay(1000) // Delay
  • // end for
  • // end while
  • // end main
  • // end class declaration

14
Analog to Digital Conversion
  • import stamp.core.
  • public class ADCTest
  • final static char CLS '\u0010'
  • static int ADCValue
  • static ADC voltMeasurer new ADC(CPU.pin9,
    CPU.pin8)
  • public static void main()
  • while(true)
  • CPU.delay(5000)
  • ADCValue voltMeasurer.value()
  • System.out.print(CLS)
  • System.out.println("ADC value is ")
  • System.out.println(ADCValue)
  • // end while
  • // end main
  • // end class declaration

15
Background PWM Object
  • import stamp.core.
  • public class ServoControl
  • static PWM servo new PWM(CPU.pin12,173,2304)
  • public static void main()
  • System.out.println("Welcome to Servo Control ")
  • System.out.println(" r - right")
  • System.out.println(" l - left")
  • System.out.println(" c - center")
  • while (true)
  • switch ( Terminal.getChar() )
  • case 'r'
  • servo.update(130,2304)
  • break
  • case 'l'
  • servo.update(220,2304)
  • break
  • case 'c'
  • servo.update(173,2304)
  • break

Constructors PWM(int pin, int highTime, int
lowTime) Creates a PWM object on the specified
pin (for example, CPU.pin1 ) with the specified
high and low times (in 8.68us units).
16
Background PWM Object
  • static void removeVP(VirtualPeripheral vp)

17
Review
  • Making Decisions
  • Repetitive Operations
  • Displaying Messages from the Javelin
  • Circuit Examples

18

Questions
Answers
Write a Comment
User Comments (0)
About PowerShow.com