The document discusses microcontrollers and the Arduino platform for building robots and embedded systems. It provides examples of code for communicating between an Arduino and other devices like Java, C#, and Python. It also mentions some online resources and retailers for purchasing components and shares images from a presentation about building a tron-style line-following robot using an Arduino, ultrasonic sensors, and a neural network.
6. ^A microcontroller (also microcomputer, MCU
or ?C) is a small computer on a single
integrated circuit consisting internally of a
relatively simple CPU, clock, timers, I/O ports,
and memory. ̄ (wikipedia)
Wednesday, October 27, 2010
17. FRA
JAVA
import java.io.InputStream;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.Enumeration;
public class SerialTest implements SerialPortEventListener {
SerialPort serialPort;
private static final String portName = "/dev/ttyUSB0";
private InputStream input;
private OutputStream output;
private static final int TIME_OUT = 2000;
private static final int DATA_RATE = 115200;
public void initialize() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
if (currPortId.getName().equals(portName)) {
portId = currPortId;
break;
}
}
try {
serialPort = (SerialPort) portId.open(this.getClass().getName(),TIME_OUT);
serialPort.setSerialPortParams(DATA_RATE,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
} catch (Exception e) {
System.err.println(e.toString());
}
}
Wednesday, October 27, 2010
18. FRA
JAVApublic void writeMessage(String message){
try{
output.write(message.getBytes());
}Catch(Exception e){
System.out.println("Got error while writing, error was:"+e.getMessage());
}
}
public String readMessage(){
byte[] readBuffer = new byte[200];
try{
while (input.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
}catch(Exception e){
System.out.println("Got error while reading, error was:"+e.getMessage());
}
return new String(readBuffer);
}
Wednesday, October 27, 2010
19. FRA
C#private static System.IO.Ports.SerialPort serialPort1;
static void Main(string[] args)
{
System.ComponentModel.IContainer components = new System.ComponentModel.Container();
serialPort1 = new System.IO.Ports.SerialPort(components);
serialPort1.PortName = "COM4";
serialPort1.BaudRate = 115200;
serialPort1.Open();
if (!serialPort1.IsOpen)
{
Console.WriteLine("Oops");
return;
}
serialPort1.DtrEnable = true;
}
Wednesday, October 27, 2010
20. FRA
C#private void writeMessage(String message){
using(serialPort1){
serialPort1.Write(message);
}
}
private String readMessage(){
using (serialPort1){
return serialPort1.ReadExisting();
}
}
Wednesday, October 27, 2010
21. FRA
PYimport serial
ser = serial.Serial('/dev/ttyUSB0', 115200)
message = "dagen";
ser.write(message)
message = ser.readline()
Wednesday, October 27, 2010