Serial port monitor program from Arduino Cookbook, 2nd ed by Michael Margolis, Chapter 4, Serial Communications. Used UNO board
void setup() {
Serial.begin(19200);
}
int number = 0;
void loop() {
Serial.print("The number is ");
Serial.println(number);
delay(1000);
number++;
}
Modified to 19200 baud and 1000ms/write.
need start with Serial.begin(baud) to set rate (same on PC?).
use Serial.println() to get line feeds
Next Project was to write out ascii table using same book, same chapter
void setup() {
Serial.begin(19200);
Serial.print("ASCII Dec Hex Oct Bin");
Serial.println();
}
int ascii_char = 33;
void loop() {
//Serial.print("ASCII Dec Hex Oct Bin");
//Serial.print("Character is : ");
Serial.print(" ");
Serial.write(ascii_char);
//Serial.print(" Decimal is : ");
Serial.print(" ");
Serial.print(ascii_char,DEC);
//Serial.print(" Hex is : ");
Serial.print(" ");
Serial.print(ascii_char,HEX);
//Serial.print(" Octal is : ");
Serial.print(" ");
Serial.print(ascii_char,OCT);
//Serial.print(" Binary is : ");
Serial.print(" ");
Serial.print(ascii_char,BIN);
Serial.println();
ascii_char++;
delay(1000);
}
had trouble figuring this out. Finally found http://arduino.cc/en/Tutorial/ASCIITable
fooled with spacings. could not put formatting in between setup and loop, needed to be put in loop
Next - type 0-9 in serial port and led blink rate is number * 100ms, still Chapter 4
int led = 13;
int blink_rate = 0;
void setup() {
Serial.begin(19200);
pinMode(led,OUTPUT);
}
void loop() {
if (Serial.available()) {
char num = Serial.read();
if (isDigit(num)) { // is ascii between 0 and 9?
blink_rate = (num - '0') * 100;
}
}
blink();
}
// blink routine
void blink() {
digitalWrite(led,HIGH);
delay(blink_rate);
digitalWrite(led,LOW);
delay(blink_rate);
}
pretty nifty!
Note : led does not blink unless there is a serial port active, either putty or the monitor
New program that builds on the last , so can specify the blink rate in ms.
int led = 13;
int blink_rate = 0;
int num_value;
void setup() {
Serial.begin(19200);
pinMode(led,OUTPUT);
}
void loop() {
if (Serial.available()) {
char num = Serial.read();
Serial.print(num,HEX);
Serial.print(" ");
if (isDigit(num)) { // is ascii between 0 and 9?
num_value = (num_value * 10) + (num - '0');
}
else if (num == 13) { // newline?
blink_rate = num_value;
Serial.println(blink_rate);
num_value = 0;
}
}
blink();
}
// blink routine
void blink() {
digitalWrite(led,HIGH);
delay(blink_rate);
digitalWrite(led,LOW);
delay(blink_rate);
}
had problems using putty with the original sketch - no LF at end of line? could not get putty to send 0xA
used println(num,HEX to figure out that putty was just sending 0xD, not oxA as required by original sketch.
changed to expect 0xD and all is well.
Serial port tx to program on PC - Processing - with "header" and "sensor" values, Chapter 4.4
int value1 = 10; // some hardcoded values to send
int value2 = 100;
int value3 = 1000;
void setup() {
Serial.begin(19200);
}
void loop() {
Serial.print('H'); // unique header to identify start of message
Serial.print(",");
Serial.print(value1,DEC);
Serial.print(",");
Serial.print(value2,DEC);
Serial.print(",");
Serial.print(value3,DEC);
Serial.print(","); // note that a comma is sent after the last field
Serial.println(); // send a cr/lf
delay(100);
value1++;
value2++;
value3++;
}
moved int value definitions out of loop in original sketch so could add ++
Processing Sketch :
// Processing Sketch to read comma delimited serial
// expects format: H,1,2,3,
import processing.serial.*;
Serial myPort; // Create object from Serial class
char HEADER = 'H'; // character to identify the start of a message
short LF = 10; // ASCII linefeed
// WARNING!
// If necessary change the definition below to the correct port
short portIndex = 1; // select the com port, 0 is the first port
void setup() {
size(200, 200);
println(Serial.list());
println(" Connecting to -> " + Serial.list()[portIndex]);
myPort = new Serial(this,Serial.list()[portIndex], 19200);
}
void draw() {
}
void serialEvent(Serial p)
{
String message = myPort.readStringUntil(LF); // read serial data
if(message != null)
{
print(message);
String [] data = message.split(","); // Split the comma-separated message
if(data[0].charAt(0) == HEADER && data.length > 3) // check validity
{
for( int i = 1; i < data.length-1; i++) // skip the header & end if line
{
println("Value " + i + " = " + data[i]); // Print the field values
}
println();
}
}
}
changed to 19200 baud rate and port to 1 in list - com3 com5 ..., index 0 is first in list
added to previous Processing sketch to display values received on the serial port from an Arduino (analog inputs)
import processing.serial.*;
Serial myPort;
String message = null;
int font_size = 12;
int max_num_labels = 12;
int rect_margin = 40;
int window_width = 600;
int window_height = rect_margin + (max_num_labels + 1) * (font_size * 2);
int rect_width = window_width - (rect_margin * 2);
int rect_height = window_height -rect_margin;
int min_value = 0;
int max_value = 255;
int origin = rect_margin;
float scale = float(rect_width) / (max_value - min_value);
String [] sensor_labels = {"s1","s2","s3","s4","s5","s6","s7","s8","s9","s10","s11","s12"};
int label_count = max_num_labels;
short LF = 10; // ASCII linefeed
void setup() {
size(window_width, window_height);
short port_index = 1; // select the com port, 0 is the first port
String port_name = Serial.list()[port_index];
println(Serial.list());
println(" Connecting to -> " + port_name);
myPort = new Serial(this,port_name, 19200);
}
void drawGrid() {
fill(0);
text(min_value,xPos(min_value),rect_margin - font_size);
line(xPos(min_value),rect_margin + font_size,xPos(min_value),rect_height + font_size);
text(max_value,xPos(max_value),rect_margin - font_size);
line(xPos(max_value),rect_margin,xPos(max_value),rect_height + font_size);
for (int i=0;i 0) {
try {
message = myPort.readStringUntil(LF); //read until find LF
if (message != null) {
print(message);
String [] data = message.split(",");
if (data[0].equals("Labels")) { //Label header?
label_count = min(data.length - 1,max_num_labels);
arrayCopy(data,1,sensor_labels,0,label_count);
}
else if (data[0].equals("Data")) { //Data header?
background(#00ffff);
drawGrid();
fill(#ff00aa);
println(data.length);
for (int i=1;i<=label_count && i < data.length - 1;i++) {
drawBar(i - 1,Integer.parseInt(data[i]));
}
}
}
}
catch (Exception e) {
e.printStackTrace(); //display error
}
}
}
stimulus is Arduino sketch :
void setup() {
Serial.begin(19200);
delay(1000);
Serial.println("Labels,L0,L1,L2,L3,L4,L5");
}
void loop() {
Serial.print("Data,");
for(int i=0;i<6;i++) {
Serial.print(analogRead(i));
Serial.print(",");
}
Serial.print('\n');
delay(100);
}
Processing sketch to control the mouse on a computer screen over the serial port, using Arduino to send the values.
from Section 4.10. needed some recoding because old Processing stuff was used
Processing Sketch
// use esc to stop sketch
import java.awt.AWTException;
import java.awt.Robot;
import processing.serial.*;
Serial myPort; //creates object from Serial class
arduMouse myMouse; //creates arduino controlled mouse
public static final short LF = 10;
public static final short port_index = 1; //com port, 0 is first
int posX, posY, btn; //data from message fields
void setup() {
size(200, 200);
println(Serial.list());
println(" Connecting to -> " + Serial.list()[port_index]);
myPort = new Serial(this,Serial.list()[port_index], 19200);
myMouse = new arduMouse();
btn = 0; //mouse off for now
}
void draw() {
if (btn != 0)
myMouse.move(posX,posY);
}
void serialEvent(Serial p) {
String message = myPort.readStringUntil(LF); // read serial data
if(message != null) {
print(message);
String [] data = message.split(","); // Split the comma-separated message
if(data[0].equals("Data")) {// check validity
if (data.length > 3) {
try {
posX = Integer.parseInt(data[1]);
posY = Integer.parseInt(data[2]);
btn = Integer.parseInt(data[3]);
}
catch (Throwable t) {
println("."); //parse error
print(message);
}
}
}
}
}
class arduMouse {
Robot myRobot; //create Robot class object
static final short rate = 1; //multiplier for movement rate
int centerX, centerY;
arduMouse() {
try {
myRobot = new Robot();
}
catch (AWTException e) {
e.printStackTrace();
}
//screen = java.awt.Toolkit.getDefaultToolkit().size();
//size(displayWidth,displayHeight); //creates big box
centerX = (int)width/2; //displayWidth not needed as variable?
centerY = (int)height/2;
print("width = " + width + " height = " + height);
}
void move(int offsetX,int offsetY) {
myRobot.mouseMove(rate * offsetX,rate * offsetY); //didn't like center as origin, probably should change
// myRobot.mouseMove(centerX + (rate * offsetX),centerY - (rate * offsetY));
//println(offsetX + " " + offsetY);
}
}
Arduino stimulus to run values Data,x,y,btn through paces from 1 to 1920 and 1 and 1080
int value1 = 1; // some hardcoded values to send
int value2 = 1;
int value3 = 1;
void setup() {
Serial.begin(19200);
}
void loop() {
Serial.print("Data"); // unique header to identify start of message
Serial.print(",");
Serial.print(value1,DEC);
Serial.print(",");
Serial.print(value2,DEC);
Serial.print(",");
Serial.print(value3,DEC);
Serial.print(","); // note that a comma is sent after the last field
Serial.println(); // send a cr/lf
delay(100);
value1 = value1 + 10;
if (value1 > 1920) {
value1 = 1;
}
value2 = value2 + 10;
if (value2 > 1080) {
value2 = 1;
}
//value3++;
}
Had lots of fun with this. Processing version did not support sketch in book because of screen.width and screen.height.
used width and height, without invoking anything, no Java, nothing. at first used size(displayWidth,displayHeight),
but created grey screen full display size. was going to use displayw.. and height as variable,
but not necessary. not sure why.