Multiple serial values from arduino to processing? - serial-port

I have a small problem.
I am passing information from an arduino attached 3 axis accelerometer + 3 axis magnetometer + compass heading. These are scaled to midi ranges (0-127).
ARDUINO:
This is passed in a serial print with a format like 76a45b120c23d12e23f34g
Serial.print(shiftAx);
Serial.print("a");
Serial.print(shiftAy);
Serial.print("b");
Serial.print(shiftAz);
Serial.print("c");
Serial.print(shiftMx);
Serial.print("d");
Serial.print(shiftMy);
Serial.print("e");
Serial.print(shiftMz);
Serial.print("f");
Serial.print(shiftHead);
Serial.print("g");
I can see this works using my serial monitor. (however I am unsure as to print it as println at "g" or not.)
PROCESSING 2:
I buffer until g
in my void setup()
port = new Serial(this, "/dev/tty.usbmodem411", 9600);
port.bufferUntil('g');
i have the function
void serialEvent (Serial port)
{
data = port.readStringUntil('g');
AxVal = data.substring(0, data.indexOf('a'));
AyVal = data.substring(data.indexOf("a") + 1, data.indexOf("b"));
AzVal = data.substring(data.indexOf("b") + 1, data.indexOf("c"));
MxVal = data.substring(data.indexOf("c") + 1, data.indexOf("d"));
MyVal = data.substring(data.indexOf("d") + 1, data.indexOf("e"));
MzVal = data.substring(data.indexOf("e") + 1, data.indexOf("f"));
HeadVal = data.substring(data.indexOf("f") + 1, data.indexOf("g"));
}
THE PROBLEM
So this doesn't work. No text displayed. (its just a simple fill(), text())
I don't see why. Is the problem my protocol (if I can call it that), how I'm unpacking the string? Or some other problem.
I am very confused.
I can get it to work with just two values as in this tutorial
notes:
The arduino main loop is on delay(100);
arduino is at 9600 baud rate
I shall attach the whole sketches if needed to help me, which I shall extend my thanks beforehand.
CODE:
PROCESSING
import themidibus.*; //Import the library
import javax.sound.midi.MidiMessage; //Import the MidiMessage classes http://java.sun.com/j2se/1.5.0/docs/api/javax/sound/midi/MidiMessage.html
import javax.sound.midi.SysexMessage;
import javax.sound.midi.ShortMessage;
import processing.serial.*;
MidiBus myBus; // The MidiBus
Serial port;
String AxVal = " ", AyVal = " ", AzVal = " ";
String MxVal = " ", MyVal = " ", MzVal = " ";
String HeadVal = " ";
String AxString="Ax",AyString = "Ay",AzString = "Az";
String MxString="Mx",MyString = "My",MzString = "Mz";
String HeadString="Heading";
String data = " ";
PFont font;
int status_byte = 0xB0; // send control change
int channel_byte = 0; // On channel 0
int first_byte; // cc number;
int second_byte; // value
void setup()
{
size(1000,500);
port = new Serial(this, "/dev/tty.usbmodem411", 9600);
port.bufferUntil('g');
font = loadFont("NanumBrush-48.vlw");
textFont(font, 48);
MidiBus.list(); // List all available Midi devices on STDOUT. This will show each device's index and name.
myBus = new MidiBus(this, 1, 0); // Create a new MidiBus object
}
void draw()
{
background(0,0,0);
//first_byte = 1;
//second_byte = int(AxVal); // But with less velocity
//myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte);
fill(46, 209, 2);
text(AxVal, 60, 75);
text(AxString, 60, 125);
//first_byte = 2;
//second_byte = int(AyVal); // But with less velocity
//myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte);
fill(0, 160, 153);
text(AyVal, 120, 75);
text(AyString,120,125);
// first_byte = 3;
//second_byte = int(AzVal); // But with less velocity
//myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte);
fill(0, 160, 153);
text(AzVal, 180, 75);
text(AzString,180,125);
/*
first_byte = 4;
second_byte = int(MxVal); // But with less velocity
myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte);
fill(0, 160, 153);
text(MxVal, 240, 75);
text(MxString,240,125);
first_byte = 5;
second_byte = int(MyVal); // But with less velocity
myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte);
fill(0, 160, 153);
text(MyVal, 300, 75);
text(MyString,300,125);
first_byte = 6;
second_byte = int(MzVal); // But with less velocity
myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte);
fill(0, 160, 153);
text(MzVal, 360, 75);
text(MzString,360,125);
first_byte = 7;
second_byte = int(HeadVal); // But with less velocity
myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte);
fill(0, 160, 153);
text(HeadVal, 420, 75);
text(HeadString,420,125);
*/
}
void serialEvent (Serial port)
{
data = port.readStringUntil('g');
data = data.substring(0, data.length() - 1);
AxVal = data.substring(0, data.indexOf('a'));
AyVal = data.substring(data.indexOf("a") + 1, data.indexOf("b"));
AzVal = data.substring(data.indexOf("b") + 1, data.length());
/*
index = data.indexOf("c")+1;
MxVal = data.substring(index, data.indexOf("d"));
index = data.indexOf("d")+1;
MyVal = data.substring(index, data.indexOf("e"));
index = data.indexOf("e")+1;
MzVal = data.substring(index, data.indexOf("f"));
index = data.indexOf("f")+1;
HeadVal = data.substring(index, data.indexOf("g"));
*/
}
/*
void serialEvent (Serial port)
{
data = port.readStringUntil('g');
AxVal = data.substring(0, data.indexOf('a'));
AyVal = data.substring(data.indexOf("a") + 1, data.indexOf("b"));
AzVal = data.substring(data.indexOf("b") + 1, data.indexOf("c"));
MxVal = data.substring(data.indexOf("c") + 1, data.indexOf("d"));
MyVal = data.substring(data.indexOf("d") + 1, data.indexOf("e"));
MzVal = data.substring(data.indexOf("e") + 1, data.indexOf("f"));
HeadVal = data.substring(data.indexOf("f") + 1, data.indexOf("g"));
}
ARDUINO:
// Add lastvalue check
#include <Wire.h>
#include <LSM303DLH.h>
LSM303DLH glove;
//set max min magnetometer
int maxMx = +353, maxMy = +527, maxMz = 426;
int minMx = -700, minMy = -477, minMz = -561;
int maxA = 2019;
int minAx = -1043, minAy = -2048, minAz = -2048;
int shiftMx,shiftMy,shiftMz;
int shiftAx,shiftAy,shiftAz;
float shiftHeadTemp;
int shiftHead;
void setup()
{
Wire.begin();
glove.enableDefault();
Serial.begin(9600);
}
void loop()
{
glove.read();
shiftMx = ((glove.m.x - minMx) / (maxMx - minMx)) * 127;
shiftMy = ((glove.m.y - minMy) / (maxMy - minMy)) * 127;
shiftMz = ((glove.m.z - minMz) / (maxMz - minMz)) * 127;
shiftAx = ((glove.a.x - minAx) / (maxA - minAx)) * 127;
shiftAy = ((glove.a.y - minAy) / (maxA - minAy)) * 127;
shiftAz = ((glove.a.z - minAz) / (maxA - minAz)) * 127;
shiftHeadTemp = (glove.heading((LSM303DLH::vector){0,-1,0}));
shiftHead = (shiftHeadTemp/360)*127;
if (shiftMx < 0){shiftMx=0;}
if (shiftMx >127){shiftMx=127;}
if (shiftMy < 0){shiftMy=0;}
if (shiftMy >127){shiftMy=127;}
if (shiftMz < 0){shiftMz=0;}
if (shiftMz >127){shiftMz=127;}
if (shiftAx < 0){shiftAx=0;}
if (shiftAx >127){shiftAx=127;}
if (shiftAy < 0){shiftAy=0;}
if (shiftAy >127){shiftAy=127;}
if (shiftAz < 0){shiftAz=0;}
if (shiftAz >127){shiftAz=127;}
if (shiftHead < 0){shiftHead=0;}
if (shiftHead >127){shiftHead=127;}
Serial.print(shiftAx);
Serial.print("a");
Serial.print(shiftAy);
Serial.print("b");
Serial.print(shiftAz);
Serial.print("c");
Serial.print(shiftMx);
Serial.print("d");
Serial.print(shiftMy);
Serial.print("e");
Serial.print(shiftMz);
Serial.print("f");
Serial.print(shiftHead);
Serial.println("g");
delay(100);
}

I can't test your code right now but here are some ideas.
You could send JSON from your Arduino to processing. Here is the example I'm using in one of my own project but you could easily adapt it to suit your needs:
void sendJson(){
String json;
json = "{\"accel\":{\"x\":";
json = json + getXYZ(0);
json = json + ",\"y\":";
json = json + getXYZ(1);
json = json + ",\"z\":";
json = json + getXYZ(2);
json = json + "},\"gyro\":{\"yaw\":";
json = json + getYPR(0);
json = json + ",\"pitch\":";
json = json + getYPR(1);
json = json + ",\"roll\":";
json = json + getYPR(2);
json = json + "}}";
Serial.println(json);
}
The good thing about JSON is that it is easily "parsable" with processing using the getType() function. No more readUntil, it all goes like this: (note that in void setup() you need to change port.bufferUntil('g'); to port.bufferUntil('\n'); because in the Arduino, json is sent with Serial.println();)
void serialEvent (Serial port){
String json = port.readString();
shiftAx = getInt("shiftAx");
shiftAy = getInt("shiftAy"); // and so on...
}
You will have to change your code a little but it will be a lot more maintainable in the long run, say if you add more sensors. You'll just need to change the json in arduino and simply get the value in processing.
Another thing: you could use the constrain function to constrain your value between 0 and 127.
value = constrain(value, 0, 127);
Hope it helps!

I've got the same issue in c# ( the second parameter, in c# is the length of the substracting string). However, I saw that if I do not wait a little bit of time, the string transmitted is not always in the same form: such that: somtime, your string will be (value)a(value)b(value)c(value)d(value)e(value)f(value)g , BUT other times will be: (value)a(value)b(value)c(value)d(value) and the next string will be e(value)f(value)g(value)a(value)b(value) and so on... Well after all, all the data will be transmited, but it will not have the same structure, so I added a little bit of delay before every serial reading (to be sure that the previous serial string was read till the and and every timpe I get the string in the same form), and it works fine for me. Hope it is helpful for you too.

Related

Printing PDF doc to esc/pos Thermal printer

We are developing a POS APP using xamarin.forms, in that we need to print the receipt to an esc/pos thermal printer connected via LAN.
We have multi language support with the App, printing multiple language with the esc/pos commands by changing code page works perfectly.
But its working for some supported language only, for other language its printing garbage characters(unreadable ones).
so we thought of creating a pdf for the receipt and print that one. we tried to create the pdf and then convert to bitmap and then send to the printer by using esc pos commands, but its not printing anything.
public BitImage(String filename)
{
Java.IO.File file = new Java.IO.File(filename);
var pdfRenderer = new PdfRenderer(ParcelFileDescriptor.Open(file, ParcelFileMode.ReadOnly));
PdfRenderer.Page page = pdfRenderer.OpenPage(0);
Bitmap bmp = Bitmap.CreateBitmap(page.Width, page.Height, Bitmap.Config.Argb8888);
page.Render(bmp, null, null, PdfRenderMode.ForPrint);
load(bmp);
}
private void load(Bitmap bmp)
{
int w = bmp.Width;
int h = bmp.Height;
int bw = (w + 7) / 8;
if (bw > 255)
bw = 255;
int bh = h / 8;
if (bh > 24)
{
bh = 24;
}
initData(bw * 8, bh * 8);
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
if (bmp.GetPixel(x, y) == Color.Black)
setPixel(x, y);
}
}
}
private void initData(int w, int h)
{
width = w;
height = h;
pitch = h / 8;
data = new byte[w * pitch];
}
private void setPixel(int x, int y)
{
if (x >= width || y >= height)
{
return;
}
int mask = (0x0080 >> (y % 8));
data[(x * pitch) + (y / 8)] |= (byte)mask;
}
public void PrintData()
{
byte[] CMD_INIT = { 0x1B, 0x40 };
byte[] CMD_UPLOAD_IMAGE = { 0x1D, 0x2A, 0, 0 };
byte[] CMD_PRINT_IMAGE = { 0x1D, 0x2F, 0 };
byte[] CMD_CUT = { 0x1D, 0x56, 0x01 };
CMD_UPLOAD_IMAGE[2] = (byte)(width / 8);
CMD_UPLOAD_IMAGE[3] = (byte)(height / 8);
#region Print Via Lan
Socket pSocket = new Socket(SocketType.Stream, ProtocolType.IP);
pSocket.SendTimeout = 1500;
pSocket.Connect("192.168.15.168", 9100);
pSocket.Send(CMD_INIT);
pSocket.Send(CMD_UPLOAD_IMAGE);
pSocket.Send(data);
pSocket.Send(CMD_PRINT_IMAGE);
pSocket.Send(CMD_CUT);
pSocket.Close();
#endregion
}
Please help me, whether i am doing it in correct way?
or is there any better way to do the same?
You can use libraries like SkiaSharp to make Image/PDF from your data in any language and print them properly using any printer.
I've created a sample to demonstrate how to print images properly with ESC\POS printers in C#: GitHub code repo

Zoom graphics processing 2.2.1

help please, at Arduino Uno I receive a signal from the sensor and build a graph using processing 2.2.1, but you need to scale up without losing proportions. My attempts failed, the proportion was crumbling(I tried to multiply the values) Code:
Serial myPort;
int xPos = 1;
int yPos = 100;
float yOld = 0;
float yNew = 0;
float inByte = 0;
int lastS = 0;
PFont f;
void setup () {
size(1200, 500);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil('\n');
background(0xff);
}
void draw () {
int s = second();
PFont f = createFont("Arial",9,false);
textFont(f,9);
fill(0);
if (s != lastS){
stroke(0xcc, 0xcc, 0xcc);
line(xPos, yPos+10, xPos, yPos+30);
text(s + " Sec.", xPos+5, yPos+30);
lastS = s;
}
}
void mousePressed(){
save(lastS + "-heart.jpg");
}
void serialEvent (Serial myPort) {
String inString = myPort.readStringUntil('\n');
if (inString != null) {
inString = trim(inString);
if (inString.equals("!")) {
stroke(0, 0, 0xff); // blue
inByte = 1023;
} else {
stroke(0xff, 0, 0); //Set stroke to red ( R, G, B)
inByte = float(inString);
}
inByte = map(inByte, 0, 1023, 0, height);
yNew = inByte;
line(xPos-1, yPos-yOld, xPos, yPos-yNew);
yOld = yNew;
if (xPos >= width) {
xPos = 1;
yPos+=200;
if (yPos > height-200){
xPos = 1;
yPos=100;
background(0xff);
}
} else {
xPos++;
}
}
}
There are multiple ways to scale graphics.
A simple method to try is to simply scale() the rendering (drawing coordinate system).
Bare in mind currently the buffer is only cleared when the xPos reaches the right hand side of the screen.
The value from Arduino is mapped to Processing here:
inByte = map(inByte, 0, 1023, 0, height);
yNew = inByte;
you should try to map change height to a different value as you see fit.
This however will scale only the Y value. The x value is incremented here:
xPos++;
you might want to change this increment to a different value that works with the proportion you are trying maintain between x and y.

Arduino PID DC Motor Position Control

I'm doing a control engineering project, implementing a PID motor position control for automatic antenna tracking system. The system contain a dc motor, absolute encoder, and a motor driver.
Everything work as expected, but one thing. The motor cannot stop at set point value near 0 degree (350 - 359, 0 - 10 degree). The used code:
#include <PID_v1.h>
int RPWM = 5;
int LPWM = 6;
int L_EN = 7;
int R_EN = 8;
boolean pin_state[10];
byte input_pin[] = {1, 2, 3, 4, 9, 10, 11, 12, 13};
int dec_position = 0;
int dc = 0;
double kp = 50, ki = 45, kd = 2;
double input = 0, output = 0, setpoint = 0;
volatile long encoderPos = 0;
PID myPID(&input, &output, &setpoint, kp, ki, kd, DIRECT);
void setup() {
pinMode(L_EN, OUTPUT);
pinMode(R_EN, OUTPUT);
pinMode(RPWM, OUTPUT);
pinMode(LPWM, OUTPUT);
for (byte i = 0; i < 9; i++) {
pinMode(input_pin[i], INPUT);
}
TCCR1B = TCCR1B & 0b11111000 | 1;
myPID.SetMode(AUTOMATIC);
myPID.SetSampleTime(1);
myPID.SetOutputLimits(-255, 255);
digitalWrite(L_EN, HIGH);
digitalWrite(R_EN, HIGH);
}
void loop() {
if (Serial.available() > 0) {
String baca = Serial.readString();
setpoint = baca.toInt();
}
ReadEncoder();
input = dc;
myPID.Compute();
pwmOut(output);
}
void pwmOut(int out) {
if (out > 0) {
analogWrite(RPWM, out);//Sets speed variable via PWM
}
else {
analogWrite(LPWM, abs(out));//Sets speed variable via PWM
}
}
void ReadEncoder() {
// FOR READING ENCODER POSITION, GIVING 0-359 OUTPUT CORRESPOND TO THE ENCODER POSITION
for (byte i = 0; i < 9; i++) {
pin_state[i] = !(digitalRead(input_pin[i]));
}
dec_position = (pin_state[8] * 256) + (pin_state[7] * 128) + (pin_state[6] * 64) + (pin_state[5] * 32) + (pin_state[4] * 16) + (pin_state[3] * 8) + (pin_state[2] * 4) + (pin_state[1] * 2) + pin_state[0];
dc = map(dec_position, 0, 500, 0, 360);
}
When the set point is a value between 10 - 350 the sytem worked well. But when it is not, the motor never stop rotating.
I know the problem is due to a little position overshoot cause the encoder to read a very large error.
For instance, if the setpoint is 0 degree, the motor rotate to reach it. Motor rotation is slowing down as its "now" position approaching 0 degree, but the system is not overshoot free. Therefore, even 1 degree overshoot cause the error value is -359 (set point - now position) and the motor rotate again to reach the desired position.
Need help how to overcome this problem. Sorry for bad english.
Here's the solution
double error;
if (SP>PV) {
if (abs(SP-PV) < abs(-360 + SP - PV)) error = SP - PV;
else error = -360 + SP - PV;
}
else{
if(abs(SP-PV)< abs(360 - PV + SP)) error = SP - PV;
else error = 360 - SP + PV;
}
Instead off simple present value minus the set point for error. The code above return shortest path from present value to set point.
i didn't read your code yet. However, to reach "set point" (SV) you should give for "present value" (PV) an error allowance (EA)
For example: EA = SV - PV.
If EA = (-2,+2)degree then it reach "now position"
And, you should not use degree for angle, you should convert it to position (calculate by pulse)
Hope that concept can help you.

Get 3 Values from processing to Arduino

I have a color tracking program in Processing, which works with a Kinect. When I click somewhere in the picture it saves this color and draws an ellipse around it. I just want to send 3 int values (one for red, green and blue) over myPort.write() to Arduino and save these 3 values in Arduino in 2 variables. My goal is to light a red LED if the red variable is the highest, and the green LED if green is the highest and so on.
I've tried several examples I found whiel googling, but nothing works. I don't know how Arduino should get the correct values in the variables!
EDIT: Here you have my Processing code. I glued it together from several other tutorials until I nearly cried..
import processing.serial.*;
Serial myPort;
import SimpleOpenNI.*;
SimpleOpenNI kinect;
// Frame
PImage currentFrame;
color trackColor;
int r1, g1, b1, r2, g2, b2;
void setup()
{
size(640, 480);
String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
kinect = new SimpleOpenNI(this);
kinect.enableRGB();
trackColor = color (255, 0, 0);
smooth ();
currentFrame = createImage (640, 480, RGB);
}
void draw()
{
kinect.update();
currentFrame = kinect.rgbImage ();
image(currentFrame, 0, 0);
currentFrame.loadPixels();
// Before we begin searching, the "world record" for closest color is set to a high number that is easy for the first pixel to beat.
float worldRecord = 500;
// XY coordinate of closest color
int closestX = 0;
int closestY = 0;
// Begin loop to walk through every pixel
for (int x = 0; x < currentFrame.width; x ++ ) {
for (int y = 0; y < currentFrame.height; y ++ ) {
int loc = x + y*currentFrame.width;
// What is current color
color currentColor = currentFrame.pixels[loc];
r1 = (int)red(currentColor);
g1 = (int)green(currentColor);
b1 = (int)blue(currentColor);
r2 = (int)red(trackColor);
g2 = (int)green(trackColor);
b2 = (int)blue(trackColor);
// Using euclidean distance to compare colors
float d = dist(r1, g1, b1, r2, g2, b2); // We are using the dist( ) function to compare the current color with the color we are tracking.
// If current color is more similar to tracked color than
// closest color, save current location and current difference
if (d < worldRecord) {
worldRecord = d;
closestX = x;
closestY = y;
}
}
}
// We only consider the color found if its color distance is less than 10.
// This threshold of 10 is arbitrary and you can adjust this number depending on how accurate you require the tracking to be.
if (worldRecord < 10) {
// Draw a circle at the tracked pixel
fill(trackColor);
strokeWeight(4.0);
stroke(0);
ellipse(closestX, closestY, 30, 30);
}
if (mousePressed == true) {
color c = get(mouseX, mouseY);
//println("r: " + red(c) + " g: " + green(c) + " b: " + blue(c));
// Save color where the mouse is clicked in trackColor variable
int loc = mouseX + mouseY*(currentFrame.width);
trackColor = currentFrame.pixels[loc];
println("red " + r2);
println("green " + g2);
println("blue " + b2);
int colors[] = {r2, g2, b2};
for(int i=0; i < 3; i++) {
myPort.write(colors[i]);
}
}
println("ClosestX " + closestX);
myPort.write(closestX);
}
And my Arduino Code, where I don't know how to get several values.
int val;
int ledPin = 13;
int freq;
int piezoPin = 9;
int redLED = 3;
int greenLED = 5;
int blueLED = 7;
int red, green, blue;
void setup() {
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
Serial.begin(9600); // Start serial communication at 9600 bps
digitalWrite(ledPin, LOW);
}
void loop() {
if (Serial.available() > 0)
{ // If data is available to read,
val = Serial.read(); // read it and store it in val
}
if(red > green && red > blue) {
digitalWrite(redLED, HIGH); //light Red LED
}
if(green > red && green > blue) {
digitalWrite(greenLED, HIGH); //light Red LED
}
if(blue > red && blue > green) {
digitalWrite(blueLED, HIGH); //light Red LED
}
//Piezo buzzing higher when X-Position of tracked color is higher.
if (val < 100) {
freq = 50;
}
else if (val < 200) {
freq = 200;
}
else if (val < 300) {
freq = 400;
}
else if (val < 400) {
freq = 600;
}
else if (val < 500) {
freq = 800;
}
else (freq = 1000);
tone(piezoPin, freq);
}
EDIT2: Yes, additionally to lighing the LEDs I also want to have a sound from a piezo buzzer, but that works pretty well, so no questions on that... yet.
Help, please!!
Serial communication to your arduino works with a single byte at a time.
As luck would have it, the three components of a Processing Color are also three bytes.
One for red(0-255)
One for green(0-255)
One for blue(0-255)
Now all we need is a little more info so we can keep them separate.
Because a byte's minimum and maximum values are 0-255, there's no safe character we can use to keep track of the three different bytes, so we need a way to figure out where the info we send begins and ends.
An easy way to do this, is to set up a header and a footer for your messages ; something like :
<color>[byte (red)][byte (green)][byte (blue)]</color>
If we are going to read and decipher messages formatted like this, we are going to need a little buffer that will store the values we receive from Processing, so we can read them back and see if we can match the message format.
So, on the Arduino side, we need this :
String buffer = "";
String messageBegin = "<color>";
String messageEnd = "</color>";
//we read our serial data in the SerialEvent() function
//this is called *after* a loop(), and only if there is serial data in the buffer.
void serialEvent()
{
while(Serial.available())
{
buffer += (char)Serial.read();
}
}
void loop()
{
//now, inside loop, we no longer need to worry about gathering data from serial.
//we do still need to figure out if our message is complete, and then parse it.
//if our buffer contains both the beginning and the end of a message
//in the right order.
int beginIndex = buffer.lastIndexOf(messageBegin);
int endIndex = buffer.lastIndexOf(messageEnd);
if(beginIndex != -1 && endIndex != -1 && beginIndex < endIndex)
{
//we have a complete message!
//our red color starts 7 characters after where the message begins,
//because our "messageBegin" is 7 characters long
string lastMessage = buffer.substring(beginIndex+7);
//this is arguably not the prettiest way to get our byte values back.
//see if you can do better for bonus points!
byte messageAsBytes[80];
lastMessage.getBytes(messageAsBytes, messageAsBytes.length());
//we can now finally reconstruct the value we had from processing!
byte r = (byte)messageAsBytes[0];
byte g = (byte)messageAsBytes[1];
byte b = (byte)messageAsBytes[2];
//if we get a complete message, we can clear our buffer. (don't forget to do this!)
buffer = "";
}
}
On the processing side, all we need to do is make sure our messagebegin and messageend are sent along for the ride :
myPort.write("<color">);
for(int i=0; i < 3; i++) {
myPort.write(colors[i]);
}
myPort.write("</color">);

Using a distance sensor in Processing to control the attributes of shapes

I'm trying to make a program that will use the readings it gets from a distance sensor to control the attributes of circles (size, xy and colour). To do this I'm trying to make it record the current value and apply that to the value when you press the relevant key (Eg. press 's' and it changes the size to whatever the distance was at that point). - Ideally I'd like the circle to change whatever field is next dynamically as you move your hand over the sensor, but that seems a bit beyond me.
I've tried to do as much as I can, but everything I'm not sure of I've commented out. Any tips or advice? I'm really not sure what I'm doing when it comes to classes and constructors.
EDIT: When I run the code, nothing happens.
import processing.serial.*;
int xpos, ypos, s, r, g, b;
Circle circle;
int shapeSize, distance;
String comPortString;
Serial myPort;
void setup(){
size(displayWidth,displayHeight); //Use entire screen size.
//Open the serial port for communication with the Arduino
myPort = new Serial(this, "/dev/cu.usbmodem1411", 9600);
myPort.bufferUntil('\n'); // Trigger a SerialEvent on new line
}
void draw(){
background(0);
delay(50); //Delay used to refresh screen
println(distance);
}
void serialEvent(Serial cPort){
comPortString = (new String(cPort.readBytesUntil('\n')));
if(comPortString != null) {
comPortString=trim(comPortString);
/* Use the distance received by the Arduino to modify the y position
of the first square (others will follow). Should match the
code settings on the Arduino. In this case 200 is the maximum
distance expected. The distance is then mapped to a value
between 1 and the height of your screen */
distance = int(map(Integer.parseInt(comPortString),1,200,1,height));
if(distance<0){
/*If computer receives a negative number (-1), then the
sensor is reporting an "out of range" error. Convert all
of these to a distance of 0. */
distance = 0;
}
}
}
void keyPressed()
{
// N for new circle (and keep old one)
if((key == 'N') || (key == 'n')) {
println("n");
circle = new Circle(1,1,1,1,1,1);
}
//r - change red
if((key == 'R') || (key == 'r')) {
float red = map(distance, 0, 700, 0, 255);
r = int(red);
println("r " + r);
}
//g - change green
if((key == 'G') || (key == 'g')) {
float green = map(distance, 0, 700, 0, 255);
g = int(green);
println("g " + g);
}
//b - change blue
if((key == 'B') || (key == 'b')) {
float blue = map(distance, 0, 700, 0, 255);
b = int(blue);
println("b " + b);
}
//S - change Size
if((key == 'S') || (key == 's')) {
s = distance;
println("s " + s);
}
//X - change x pos
if((key == 'X') || (key == 'x')) {
xpos = distance;
println("x " + xpos);
}
//y - change y pos
if((key == 'Y') || (key == 'y')) {
ypos = distance;
println("y " + ypos);
}
}
class Circle {
Circle(int xpos, int ypos, int s, int r, int g, int b){
ellipse(xpos, ypos, s, s);
color(r, g, b);
}
int getX(){
return xpos;
}
int getY(){
return ypos;
}
}
I would split this into steps/tasks:
Connecting to the Arduino
Reading values from Arduino
Mapping read values
Controlling mapping
You've got the Arduino part pretty much there, but things look messy when trying to map read values to the circle on screen.
For now, for simplicity reasons, let's ignore classes and focus on simply drawing a single ellipse with x,y,size,r,g,b properties.
To get read of jitter you should update the property ellipse continuously, not just when pressing a key. On the key event you should simply change what property gets updated.
You could use extra variables to keep track of what ellipse properties you're updating.
Here's a refactored version of the code based on the points above:
import processing.serial.*;
int xpos,ypos,s,r,g,b;
int distance;
int propertyID = 0;//keep track of what property should be updated on distance
int PROP_XPOS = 0;
int PROP_YPOS = 1;
int PROP_S = 2;
int PROP_R = 3;
int PROP_G = 4;
int PROP_B = 5;
void setup(){
size(400,400);
//setup some defaults to see something on screen
xpos = ypos = 200;
s = 20;
r = g = b = 127;
//initialize arduino - search for port based on OSX name
String[] portNames = Serial.list();
for(int i = 0 ; i < portNames.length; i++){
if(portNames[i].contains("usbmodem")){
try{
Serial arduino = new Serial(this,portNames[i],9600);
arduino.bufferUntil('\n');
return;
}catch(Exception e){
showSerialError();
}
}
}
showSerialError();
}
void showSerialError(){
System.err.println("Error connecting to Arduino!\nPlease check the USB port");
}
void draw(){
background(0);
fill(r,g,b);
ellipse(xpos,ypos,s,s);
}
void serialEvent(Serial arduino){
String rawString = arduino.readString();//fetch raw string
if(rawString != null){
String trimmedString = rawString.trim();//trim the raw string
int rawDistance = int(trimmedString);//convert to integer
distance = (int)map(rawDistance,1,200,1,height);
updatePropsOnDistance();//continously update circle properties
}
}
void updatePropsOnDistance(){
if(propertyID == PROP_XPOS) xpos = distance;
if(propertyID == PROP_YPOS) ypos = distance;
if(propertyID == PROP_S) s = distance;
if(propertyID == PROP_R) r = distance;
if(propertyID == PROP_G) g = distance;
if(propertyID == PROP_B) b = distance;
}
void keyReleased(){//only change what proprty changes on key press
if(key == 'x' || key == 'X') propertyID = PROP_XPOS;
if(key == 'y' || key == 'Y') propertyID = PROP_YPOS;
if(key == 's' || key == 'S') propertyID = PROP_S;
if(key == 'r' || key == 'R') propertyID = PROP_R;
if(key == 'g' || key == 'G') propertyID = PROP_G;
if(key == 'b' || key == 'B') propertyID = PROP_B;
}
//usually a good idea to test - in this case use mouseY instead of distance sensor
void mouseDragged(){
distance = mouseY;
updatePropsOnDistance();
}
If this makes sense, it can easily be encapsulated in a class.
We could use an array to store those properties, but if something like props[0] for x, props1 for y, etc. is harder to read, you could use an IntDict which allows you to index values based on a String instead of a value (so you can do props["x"] instead of props[0]).
Here's an encapsulated version of the code:
import processing.serial.*;
Circle circle = new Circle();
void setup(){
size(400,400);
//initialize arduino - search for port based on OSX name
String[] portNames = Serial.list();
for(int i = 0 ; i < portNames.length; i++){
if(portNames[i].contains("usbmodem")){
try{
Serial arduino = new Serial(this,portNames[i],9600);
arduino.bufferUntil('\n');
return;
}catch(Exception e){
showSerialError();
}
}
}
showSerialError();
}
void showSerialError(){
System.err.println("Error connecting to Arduino!\nPlease check the USB port");
}
void draw(){
background(0);
circle.draw();
}
void serialEvent(Serial arduino){
String rawString = arduino.readString();
if(rawString != null){
String trimmedString = rawString.trim();
int rawDistance = int(trimmedString);
int distance = (int)map(rawDistance,1,200,1,height);
circle.update(distance);
}
}
void keyReleased(){
circle.setUpdateProperty(key+"");//update the circle property based on what key gets pressed. the +"" is a quick way to make a String from the char
}
//usually a good idea to test - in this case use mouseY instead of distance sensor
void mouseDragged(){
circle.update(mouseY);
}
class Circle{
//an IntDict (integer dictionary) is an associative array where instead of accessing values by an integer index (e.g. array[0]
//you access them by a String index (e.g. array["name"])
IntDict properties = new IntDict();
String updateProperty = "x";//property to update
Circle(){
//defaults
properties.set("x",200);
properties.set("y",200);
properties.set("s",20);
properties.set("r",127);
properties.set("g",127);
properties.set("b",127);
}
void draw(){
fill(properties.get("r"),properties.get("g"),properties.get("b"));
ellipse(properties.get("x"),properties.get("y"),properties.get("s"),properties.get("s"));
}
void setUpdateProperty(String prop){
if(properties.hasKey(prop)) updateProperty = prop;
else{
println("circle does not contain property: " + prop+"\navailable properties:");
println(properties.keyArray());
}
}
void update(int value){
properties.set(updateProperty,value);
}
}
In both examples you can test the distance value by dragging your mouse on the Y axis.
Regarding the HC-SR04 sensor, you can find code on the Arduino Playground to get the distance in cm. I haven't used the sensor myself yet, but I notice other people has some issues with it, so it's worth checking this post as well. If you want to roll your own Arduino code, no problem, you can use the HC-SR04 datasheet(pdf link) to get the formula:
Formula: uS / 58 = centimeters or uS / 148 =inch; or: the range = high
level time * velocity (340M/S) / 2; we suggest to use over 60ms
measurement cycle, in order to prevent trigger signal to the echo
signal.
It's important to get accurate values (you'll avoid jitter when using these to draw in Processing). Additionally you can use easing or a moving average.
Here's a basic moving average example:
int historySize = 25;//remember a number of past values
int[] x = new int[historySize];
int[] y = new int[historySize];
void setup(){
size(400,400);
background(255);
noFill();
}
void draw(){
//draw original trails in red
stroke(192,0,0,127);
ellipse(mouseX,mouseY,10,10);
//compute moving average
float avgX = average(x,mouseX);
float avgY = average(y,mouseY);
//draw moving average in green
stroke(0,192,0,127);
ellipse(avgX,avgY,10,10);
}
void mouseReleased(){
background(255);
}
float average(int[] values,int newValue){
//shift elements by 1, from the last to the 2nd: count backwards
float total = 0;
int size = values.length;
for(int i = size-1; i > 0; i--){//count backwards
values[i] = values[i-1];//copy previous value into current
total += values[i];//add values to total
}
values[0] = newValue;//add the newest value at the start of the list
total += values[0];//add the latest value to the total
return (float)total/size;//return the average
}

Resources