Hello World. We\'re stuck on this assignment - and are hoping for some helpfull
ID: 3566911 • Letter: H
Question
Hello World.
We're stuck on this assignment - and are hoping for some helpfull input - anyone of you have a clue as how to solve this problem?
This is what we have so far (1-5):
package morseReader;
import lejos.nxt.LightSensor;
import lejos.nxt.MotorPort;
import lejos.nxt.SensorPort;
public class robot1
{
public static void main(String[] args)
{
long [] lightBlinks = new long[15];
LightSensor light = new LightSensor(SensorPort.S1);
// light.getLightValue() > 50 light is on
// light.getLightValue() < 50 light is off
for(int i = 0; i<lightBlinks.length; i++)
{
while(light.getLightValue() < 50)
{
; // do nothing while light is off
}
long turnON = System.currentTimeMillis();
while(light.getLightValue() > 50)
{
;
}
long turnOFF = System.currentTimeMillis();
long duration = turnOFF - turnON; //first blink
lightBlinks [i] = duration;
}
long highest = lightBlinks[0];
for(int i = 0; i < lightBlinks.length; i++)
{
if(lightBlinks[i] > highest)
{
highest = lightBlinks[i];
}
}
long secondHighest = -1;
for(int i = 0; i < lightBlinks.length; i++)
{
if(lightBlinks[i] > secondHighest && lightBlinks[i] < highest-5) //find a new number/sequence
{
secondHighest = lightBlinks[i];
}
}
turnRight((int)highest/10);
goForward((int)secondHighest/10);
// secondHighest = highest;
// for(int i = 0; i < lightBlinks.length; i++)
// {
// if(secondHighest == highest)
// secondHighest = lightBlinks[i];
// if(lightBlinks[i] > secondHighest && lightBlinks[i] < highest)
// secondHighest = lightBlinks[i];
// }
// general way of finding the second highest even if the numbers are negative
}
public static void goForward(int cm)
{
// set our goal - current tachocount + cm multiplied by 21 (1 cm is appr. 21 degrees in our case)
int goal = MotorPort.B.getTachoCount() + 21*cm;
// start both motors
MotorPort.B.controlMotor(80, 1);
MotorPort.C.controlMotor(79, 1);
// and do nothing until the tachocount reaches the goal
while(MotorPort.B.getTachoCount() < goal)
{
;
}
// Then stop the motors
MotorPort.B.controlMotor(80, 3);
MotorPort.C.controlMotor(80, 3);
}
public static void turnRight(int degrees)
{
// set our goal - current tachocount + 330 (that's how much one wheel needs to rotate to make the robot turn 90 degrees
int goal = MotorPort.C.getTachoCount() + (int) (degrees * 3.666); // 3.666 is the number that the wheel needs in order to turn 1 degree
// Stop one motor and start the other
MotorPort.B.controlMotor(80, 3);
MotorPort.C.controlMotor(70, 1);
// wait until tachocount reaches the goal
// check if the wheel is moving
while(MotorPort.C.getTachoCount() < goal)
{
;
}
MotorPort.C.controlMotor(80, 3);
}
Here is the assignment description:
It is a Lego Mindstorms Robot,with java programming.
The robot has the following properties:
Explanation / Answer
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
/**
* A Java Robot example class.
*
* Caution: Using the Java Robot class improperly can cause
* a lot of system problems. I had to reboot my Mac ~10
* times yesterday while trying to debug this code.
*
* I created this class to demonstrate the Java Robot
* class on a Mac OS X system, though it should run on Linux
* or Windows as well.
*
* On a Mac system, I place the TextEdit text editor in the
* upper-left corner of the screen, and put a bunch of blank lines
* in the editor. Then I run this Java Robot example from
* Eclipse or the Unix command line.
*
* It types the three strings shown in the code below into
* the text editor.
*
* Many thanks to the people on the Mac Java-dev mailing list
* for your help.
*
* @author Alvin Alexander, <a href="http://devdaily.com" title="http://devdaily.com">http://devdaily.com</a>
*
*/
public class JavaRobotExample
{
Robot robot = new Robot();
public static void main(String[] args) throws AWTException
{
new JavaRobotExample();
}
public JavaRobotExample() throws AWTException
{
robot.setAutoDelay(40);
robot.setAutoWaitForIdle(true);
robot.delay(4000);
robot.mouseMove(40, 130);
robot.delay(500);
leftClick();
robot.delay(500);
leftClick();
robot.delay(500);
type("Hello, world");
robot.mouseMove(40, 160);
robot.delay(500);
leftClick();
robot.delay(500);
leftClick();
robot.delay(500);
type("This is a test of the Java Robot class");
robot.delay(50);
type(KeyEvent.VK_DOWN);
robot.delay(250);
type("Four score and seven years ago, our fathers ...");
robot.delay(1000);
System.exit(0);
}
private void leftClick()
{
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(200);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(200);
}
private void type(int i)
{
robot.delay(40);
robot.keyPress(i);
robot.keyRelease(i);
}
private void type(String s)
{
byte[] bytes = s.getBytes();
for (byte b : bytes)
{
int code = b;
// keycode only handles [A-Z] (which is ASCII decimal [65-90])
if (code > 96 && code < 123) code = code - 32;
robot.delay(40);
robot.keyPress(code);
robot.keyRelease(code);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.