IT Basic Wednesday 14 April 2010 27. august 2018 IT Basic Wednesday 14 April 2010 Technical Faculty - Campus Horsens, Henrik Kronborg Pedersen – hekp@viauc.dk Hanne Bundgaard
Agenda LeJOS and java elements Picking up blue or red balls Technical Faculty - Campus Horsens, Henrik Kronborg Pedersen – hekp@viauc.dk
Don’t forget the steps…. Eclipse New Java Project New Class Write the class Save Command Prompt Go to project folder (workspace\project\src) Compile the class (nxjc) Link and upload (nxj) Technical Faculty - Campus Horsens, Henrik Kronborg Pedersen – hekp@viauc.dk
The java class import lejos.nxt.*; public class HelloWorld { Class file must be named the same ( HelloWorld.java) import lejos.nxt.*; public class HelloWorld { public static void main (String[] aArg) throws Exception LCD.drawString("Hello World",3,4); Thread.sleep(2000); } Technical Faculty - Campus Horsens, Henrik Kronborg Pedersen – hekp@viauc.dk
The java class import lejos.nxt.*; public class HelloWorld { To use LeJOS we must import the tools This is where motors and sensors are defined import lejos.nxt.*; public class HelloWorld { public static void main (String[] aArg) throws Exception LCD.drawString("Hello World",3,4); Thread.sleep(2000); } Technical Faculty - Campus Horsens, Henrik Kronborg Pedersen – hekp@viauc.dk
The java class import lejos.nxt.*; public class HelloWorld { public static void main (String[] aArg) throws Exception LCD.drawString("Hello World",3,4); Thread.sleep(2000); } The main method must exist and is the starting method of your system Just write it this way Technical Faculty - Campus Horsens, Henrik Kronborg Pedersen – hekp@viauc.dk
The java class import lejos.nxt.*; public class HelloWorld { Sometimes exception handling is required – then add the throws exception to your method import lejos.nxt.*; public class HelloWorld { public static void main (String[] aArg) throws Exception LCD.drawString("Hello World",3,4); Thread.sleep(2000); } Technical Faculty - Campus Horsens, Henrik Kronborg Pedersen – hekp@viauc.dk
The java class import lejos.nxt.*; public class HelloWorld { public static void main (String[] aArg) throws Exception LCD.drawString("Hello World",3,4); Thread.sleep(2000); } In methods we execute statements Technical Faculty - Campus Horsens, Henrik Kronborg Pedersen – hekp@viauc.dk
For loop import lejos.nxt.*; public class TenCount { public static void main (String[] aArg) throws Exception for(int count=1; count<11; count++) LCD.drawString("Count is: " + i, 3, 4); Thread.sleep(1000); } Technical Faculty - Campus Horsens, Henrik Kronborg Pedersen – hekp@viauc.dk
while loop int count = 1; while (count < 11) { LCD.drawString("Count is: " + count, 3, 4); count++; Thread.sleep(1000); } Google: java while Count++ Is the same as Count = count+1 Technical Faculty - Campus Horsens, Henrik Kronborg Pedersen – hekp@viauc.dk
If statement if (bump.isPressed()) { right(); } else left(); Google: java if Technical Faculty - Campus Horsens, Henrik Kronborg Pedersen – hekp@viauc.dk
FollowLine so far import lejos.nxt.*; public class FollowLine { public static void main(String[] args) throws Exception LightSensor light = new LightSensor(SensorPort.S1); while (true) forward(); if (light.readValue() < 40) left(); } if (light.readValue() >= 40) right(); private static void forward() { Motor.A.forward(); Motor.C.forward(); } private static void left() throws Exception Motor.A.stop(); Thread.sleep(500); forward(); private static void right() throws Exception Motor.C.stop(); } //end class Technical Faculty - Campus Horsens, Henrik Kronborg Pedersen – hekp@viauc.dk
Exercise 1 Count to 10 If a button is pressed before 10, count backwards Technical Faculty - Campus Horsens, Henrik Kronborg Pedersen – hekp@viauc.dk
Exercise 2 Let your robot follow the line If it meets a red ball Write: ”Found red ball” in the display Pick it up Turn to the right Drop the ball If a button is pressed, pick up blue balls instead Technical Faculty - Campus Horsens, Henrik Kronborg Pedersen – hekp@viauc.dk