Processing Code "Error, disabling_serialEvent() in Arduino - macos

I receive this error when trying to run my arduino and Processing
Error, disabling serialEvent() for /dev/cu.usbmodem1451 null
I am running process 2 and Arduino 1.6.5 on a MAC OSX 10.9.5
I am super new to processing and arduino . I am just trying to use three potentiometers to control the RGB values of the background color.
Arduino code:
int potPin = 0;
//int potPinB = 1;
//int potPinC = 2;
void setup()
{
Serial.begin(9600);
}
void loop()
{
int val = map(analogRead(potPin), 0, 1023, 0, 255);
Serial.println(val);
delay(500);
//int valB = map(analogRead(potPinB), 0, 1023, 0, 255);
//Serial.println(valB);
//delay(50);
//int valC = map(analogRead(potPinA), 0, 1023, 0, 255);
//Serial.println(valC);
//delay(50);
}
Processing code:
import processing.serial.*;
Serial port;
float val = 0;
//float valB = 1; // another analog input
//float valC = 2; // another analog input
void setup()
{
size(500, 500);
port = new Serial(this, "/dev/cu.usbmodem1451", 9600);
port.bufferUntil('\n');
if (frame != null);
frame.setResizable(true);
}
void draw ()
{
background(val,255,150);
}
void serialEvent (Serial port)
{
val = float(port.readStringUntil('\n'));
}

You might be getting an error in serialEvent which you should be handling (perhaps the float conversion). Also, if you're using bufferUntil(), you shouldn't need readStringUntil().
Try something like this:
import processing.serial.*;
Serial port;
float val = 0;
//float valB = 1; // another analog input
//float valC = 2; // another analog input
void setup()
{
size(500, 500);
port = new Serial(this, "/dev/cu.usbmodem1451", 9600);
port.bufferUntil('\n');
if (frame != null);
frame.setResizable(true);
}
void draw ()
{
background(val,255,150);
}
void serialEvent (Serial port)
{
try{
val = float(port.readString());
}catch(Exception e){
e.printStackTrace();
}
}
When simply reading a single value, readChar() or simply read() should work.
If the above works. You should be able to send multiple values.
Initially you can send a csv like formatter line:
arduinoR,arduinoG,arduinoB\n
which you can then read as a string, split() it into an array of 3 values, then convert each array element from String to int.
Update
Here an example:
import processing.serial.*;
int r, g, b;
void setup() {
try {
new Serial(this, "/dev/tty.usbmodemfa141", 9600).bufferUntil('\n');
}
catch(Exception e) {
println("check settings above and usb cable, something went wrong:");
e.printStackTrace();
}
}
void draw() {
background(r, g, b);
}
void serialEvent(Serial s) {
String data = s.readString();
try {
String[] rgb = data.trim().split(",");
print(rgb);
if(rgb.length == 3){
r = int(rgb[0]);
g = int(rgb[1]);
b = int(rgb[2]);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
Later on you can look at sending data as binary: exactly 3 bytes (r,g,b).
Have fun :)

Related

Why can't redraw SecondApplet with processing?

I am working with processing 3 and I am actually trying to work with 2 windows.
I would like redraw the second window but redraw do nothing (In final project data incoming from serial port will make the redraw). I don't know why.
Here is my code :
/*
* 17.09.16 : Logan Gallois
* future imporvement :
* Windows print graph of PWMs values from RGB LED
* Add autoDetect position to move windows at the good location
*/
import processing.serial.*;
Serial myPort;
SecondApplet sa;
int[] numbers = new int[600];
boolean flag = false;
float var1;
float var2;
float var3;
float var4;
void setup() {
size(1024, 576); //Affiche en plein écran
surface.setResizable(false);
noStroke();
println(Serial.list()); /* Verifier dans la liste ou se trouve l'arduino */
/* Puis changer en dessous la valeur en fonction */
/* En general 0 pour windows et 1 pour un mac */
String os=System.getProperty("os.name"); /* Detection automatique de l'OS */
if(os != "Mac OS X") {
if(Serial.list().length > 0) {
myPort = new Serial(this, Serial.list()[0], 115200);
}
} else {
if(Serial.list().length > 1) { /* Module BLE en position 0 */
myPort = new Serial(this, Serial.list()[1], 115200);
}
}
//Important : se câler sur la valeur en baud du prog Arduino
myPort.bufferUntil('\n');
String[] args = {"TwoFrameTest"};
sa = new SecondApplet();
PApplet.runSketch(args, sa);
delay(100);
}
void draw() {
if(!flag) {
surface.setLocation(displayWidth/2-width/2-400,displayHeight/2-height/2);
sa.frame.setTitle("Monitoring RGB Values");
flag = true;
}
background(color(int(var1), int(var2), int(var3)));
//sa.background(0, 255, 0);
/*for(int i = 0 ;i<=width/10;i++){
sa.stroke(200);
sa.line((-frameCount%10)+i*10,0,(-frameCount%10)+i*10,height);
sa.line(0,i*10,width,i*10);
}
sa.noFill();
sa.stroke(0);
sa.beginShape();
for(int i = 0; i<numbers.length;i++){
sa.vertex(i,350-numbers[i]);
}
sa.endShape();
for(int i = 1; i<numbers.length;i++){
numbers[i-1] = numbers[i];
}
numbers[numbers.length-1]=mouseY;*/
}
void mouseClicked() {
print("clicked");
sa.background(0, 0, 255);
sa.fill(100);
sa.redraw(); //This line do not redraw my 'sa' window
background(0, 0, 255);
redraw();
}
void serialEvent (Serial myPort) {
String inString = myPort.readStringUntil('\n');
//println(inString);
if (inString != null) {
inString = trim(inString);
int inputs[] = int(split(inString,',')); // on élude les virgules
// on affecte nos 4 valeurs
if(inputs.length == 4) {
var1 = inputs[0];
var2 = inputs[1];
var3 = inputs[2];
var4 = inputs[3];
// On ré-échelonne la valeur analogique en valeur RGB
var1 = map(var1, 0, 1023, 0, 255);
var2 = map(var2, 0, 1023, 0, 255);
var3 = map(var3, 0, 1023, 0, 255);
var4 = map(var3, 0, 1023, 0, 255);
print(int(var1));
print(" ,");
print(int(var2));
print(" ,");
print(int(var3));
print(" ,");
println(var4);
}
}
}
public class SecondApplet extends PApplet {
public void settings() {
size(600, 600);
noLoop(); //I tried with and without that
}
public void draw() {
}
public void mouseClicked() {
print("clicked2");
background(0, 255, 0);
redraw(); //This line works great
}
}
Any Ideas ?
When posting, please try to narrow your problem down to an MCVE. That means taking out all the Serial stuff and just using basic mouse events.
Your second sketch's draw() function is empty. What do you expect calling redraw() to actually do?
Here's an example MCVE:
SecondApplet sa;
void setup() {
size(500, 500); //Affiche en plein écran
String[] args = {"TwoFrameTest"};
sa = new SecondApplet();
PApplet.runSketch(args, sa);
delay(100);
}
void draw() {
background(255, 0, 0);
}
void mouseClicked() {
sa.redraw();
}
public class SecondApplet extends PApplet {
public void settings() {
size(100, 100);
noLoop();
}
public void draw() {
background(0);
ellipse(random(width), random(height), 25, 25);
}
public void mouseClicked() {
redraw();
}
}
This MCVE works perfectly, and correctly triggers the second sketch's draw() function from both the first and second sketch windows.
I'd recommend working from this MCVE. If you still can't get it working, feel free to post a follow-up question, preferably without the serial stuff (which makes your sketch impossible to run since we don't have access to your hardware). Good luck.

Arduino Serial.write to Processing returning 0?

I've currently got an Arduino program communicating accelerometer values via a Serial event to processing working perfectly. I'm trying to add a thermometer to my setup however processing is only receiving 0 from reading the pin. If I Serial.print the reading in setup it does print to the serial monitor just fine, however I can't get it to send proper values alongside my accelerometer readings.
Arduino code:
int inByte = 0;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
establishContact(); // send a byte to establish contact until receiver responds
}
void loop() {
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
// send sensor values:
Serial.write(analogRead(A3)); // X AXIS
Serial.write(analogRead(A2)); // Y AXIS
Serial.write(analogRead(A1)); // Z AXIS
Serial.write(analogRead(A0)); // TEMPERATURE
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A'); // send a capital A
delay(300);
}
}
Processing code:
import processing.serial.*;
Serial myPort;
int[] serialInArray = new int[4];
int serialCount = 0;
int xInput, yInput, zInput;
float temperature;
boolean firstContact = false;
void setup() {
size(600, 600, P3D);
pixelDensity(2);
noStroke();
background(0);
printArray(Serial.list());
String portName = Serial.list()[4];
myPort = new Serial(this, portName, 9600);
}
void draw() {
}
void serialEvent(Serial myPort) {
// read a byte from the serial port:
int inByte = myPort.read();
// if this is the first byte received, and it's an A,
// clear the serial buffer and note that you've
// had first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if (firstContact == false) {
if (inByte == 'A') {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
}
} else {
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;
// If we have 3 bytes:
if (serialCount > 2 ) {
zInput = serialInArray[0]-80;
yInput = serialInArray[1]-80+69;
xInput = serialInArray[2]-77;
temperature = serialInArray[3]; // should return voltage reading (i.e 16ºc = 130);
//println("x = " + xInput + ", y = " + yInput + ", z = " + zInput + ", Temp = " + serialInArray[3]);
// Send a capital A to request new sensor readings:
myPort.write('A');
// Reset serialCount:
serialCount = 0;
}
}
}
The accelerometer values print perfectly, but temperature just returns a 0. Serial.print(analogRead(A0)) in the serial monitor gives me the correct values, so the thermometer is definitely working.
Any help would be greatly appreciated, thank you!
In this line ,
if (serialCount > 2 ) {
change for
if (serialCount >= 4 ) {
OR try to use typecasting or change the temperature for integer !!!
int temperature;

Printing the wrong text and getting results randomly

Here's my code, where lastLine checks the last of the file:
import java.util.Random;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public String lastLine( File file ) {
RandomAccessFile fileHandler = null;
try {
fileHandler = new RandomAccessFile( file, "r" );
long fileLength = fileHandler.length() - 1;
StringBuilder sb = new StringBuilder();
for(long filePointer = fileLength; filePointer != -1; filePointer--){
fileHandler.seek( filePointer );
int readByte = fileHandler.readByte();
if( readByte == 0xA ) {
if( filePointer == fileLength ) {
continue;
}
break;
} else if( readByte == 0xD ) {
if( filePointer == fileLength - 1 ) {
continue;
}
break;
}
sb.append( ( char ) readByte );
}
String lastLine = sb.reverse().toString();
return lastLine;
} catch( java.io.FileNotFoundException e ) {
e.printStackTrace();
return null;
} catch( java.io.IOException e ) {
e.printStackTrace();
return null;
} finally {
if (fileHandler != null )
try {
fileHandler.close();
} catch (IOException e) {
/* ignore */
}
}
}
File file = new File("lines.txt");
String last = lastLine(file);
String previous = null;
float r = random(0,255);
float g = random(0, 255);
float b = random(0, 255);
public void settings() {
size(500, 500);
}
public void setup() {
frameRate(60);
stroke(155, 0, 0);
textAlign(CENTER, CENTER);
textSize(30);
background(r+(random(0,100)), g+(random(0,100)), b+(random(0,100)));
}
void draw() {
previous = lastLine(file);
if (last.equals(previous)) {
last = lastLine(file);
}
if (!last.equals(previous)) {
if (last.indexOf("w") != -1) {
text("there is a w", 255, 255);
fill(r, g, b);
}
else {
text("there is not a w", 255, 255);
}
fill(50);
last = lastLine(file);
}
}
So whenever the last line of the file has been changed, the text printed on screen should change to "there is a w" or "there is not a w" depending on whether or not there is a w in the last line. However, I seem to be getting results randomly. For example, when the last string of the file contains a "w," sometimes I get "there is not a w" printed on the screen. I've also gotten "there is a w" for when there is no w in the string.
What did I do wrong?
Your code is doing some strange things: why are you reading in the file 3 separate times in the draw() function? Why are you reading in the file one byte at a time?
Instead of trying to go through your code, here is an example that seems to do what you want:
String previous;
void draw() {
String current = lastLine();
if (!current.equals(previous)) {
background(random(255), random(255), random(255));
if (current.contains("w")) {
text("YES W", 0, 50);
} else {
text("NO W", 0, 50);
}
}
previous = current;
}
public String lastLine() {
String lines[] = loadStrings("list.txt");
if(lines.length == 0){
return "";
}
return lines[lines.length-1];
}
Also note that if you're not going to be deploying as JavaScript, you might want to use Java's file watch service instead of reading in the file every frame.

Video Delay/Buffer in Processing 2.0

I'm having a ton of trouble making a simple video delay in processing. I looked around on the internet and I keep finding the same bit of code and I can't get it to work at all. When I first tried it, it did nothing (at all). Here's my modified version (which at least seems to load frames into the buffer), I really have no idea why it doesn't work and I'm getting really tired of pulling out my hair. Please... please, for the love of god, please somebody point out the stupid mistake I'm making here.
And now, without further delay (hah, get it?), the code:
import processing.video.*;
VideoBuffer vb;
Movie myMovie;
Capture cam;
float seconds = 1;
void setup() {
size(320,240, P3D);
frameRate(30);
String[] cameras = Capture.list();
if (cameras.length == 0) {
println("There are no cameras available for capture.");
exit();
} else {
println("Available cameras:");
for (int i = 0; i < cameras.length; i++) {
println(cameras[i]);
}
cam = new Capture(this, cameras[3]);
cam.start();
}
vb = new VideoBuffer(90, width, height);
}
void draw() {
if (cam.available() == true) {
cam.read();
vb.addFrame(cam);
}
image(cam, 0, 0);
image( vb.getFrame(), 150, 0 );
}
class VideoBuffer
{
PImage[] buffer;
int inputFrame = 0;
int outputFrame = 0;
int frameWidth = 0;
int frameHeight = 0;
VideoBuffer( int frames, int vWidth, int vHeight )
{
buffer = new PImage[frames];
for(int i = 0; i < frames; i++)
{
this.buffer[i] = new PImage(vWidth, vHeight);
}
this.inputFrame = 0;
this.outputFrame = 1;
this.frameWidth = vWidth;
this.frameHeight = vHeight;
}
// return the current "playback" frame.
PImage getFrame()
{
return this.buffer[this.outputFrame];
}
// Add a new frame to the buffer.
void addFrame( PImage frame )
{
// copy the new frame into the buffer.
this.buffer[this.inputFrame] = frame;
// advance the input and output indexes
this.inputFrame++;
this.outputFrame++;
println(this.inputFrame + " " + this.outputFrame);
// wrap the values..
if(this.inputFrame >= this.buffer.length)
{
this.inputFrame = 0;
}
if(this.outputFrame >= this.buffer.length)
{
this.outputFrame = 0;
}
}
}
This works in Processing 2.0.1.
import processing.video.*;
Capture cam;
PImage[] buffer;
int w = 640;
int h = 360;
int nFrames = 60;
int iWrite = 0, iRead = 1;
void setup(){
size(w, h);
cam = new Capture(this, w, h);
cam.start();
buffer = new PImage[nFrames];
}
void draw() {
if(cam.available()) {
cam.read();
buffer[iWrite] = cam.get();
if(buffer[iRead] != null){
image(buffer[iRead], 0, 0);
}
iWrite++;
iRead++;
if(iRead >= nFrames-1){
iRead = 0;
}
if(iWrite >= nFrames-1){
iWrite = 0;
}
}
}
There is a problem inside your addFrame-Method. You just store a reference to the PImage object, whose pixels get overwritten all the time. You have to use buffer[inputFrame] = frame.get() instead of buffer[inputFrame] = frame. The get() method returns a copy of the image.

Processing: I need counter inside draw function

I'm using controlP5 processing GUI, and I need counter inside draw() function.
I know that draw() function run continuously and every for loop is shown in its last step.
I need to see every step. I have two inputs, and I can input only once, because of draw(). I need draw() function to wait for input or something like that.
import controlP5.*;
Textarea myTextareaMI;
Textarea myTextareaVI;
Textarea my;
String textValueBODOVI = "";
String textValueZVANJE = "";
boolean mi=false, vi=false;
int i=1;
ControlP5 cp5;
int myColor = color(255);
int c1, c2;
float n, n1;
void setup() {
size(320, 480);
noStroke();
PFont font = createFont("arial", 20);
cp5 = new ControlP5(this);
cp5.addButton("NOVA PARTIJA")
.setValue(0)
.setPosition(0, 0)
.setSize(480, 19)
;
cp5.addButton("PONISTI ZADNJI UNOS")
.setValue(100)
.setPosition(0, 20)
.setSize(480, 19)
;
cp5.addBang("MI")
.setPosition(60, 80)
.setSize(60, 20)
.getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
;
cp5.addBang("VI")
.setPosition(123, 80)
.setSize(60, 20)
.getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
;
cp5.addTextfield("BODOVI")
.setPosition(60, 41)
.setSize(60, 20)
.setFont(font)
.setColor(color(255, 0, 0))
;
cp5.addTextfield("ZVANJE")
.setPosition(123, 41)
.setSize(60, 20)
.setFont(font)
.setColor(color(255, 0, 0))
;
;
int l=0;
for (int i=1;i<11;i++,l=l+22) {
my = cp5.addTextarea("MIVI"+i).setPosition(60, 101+l).setSize(123, 20).setFont(createFont("arial", 14))
.setLineHeight(14)
.setColor(color(128))
.setColorBackground(color(255, 100))
.setColorForeground(color(255, 100))
;
}
}
public void bang() {
}
void draw() {
delay(15);
background(myColor);
myColor = lerpColor(c1, c2, n);
n += (1-n)* 0.1;
funkcija();
}
void funkcija(){
int suma= 0;
for( i=1;i<=11;i++){
int brojmi=0;
textValueZVANJE = cp5.get(Textfield.class, "ZVANJE").getText();
textValueBODOVI = cp5.get(Textfield.class, "BODOVI").getText();
int bodovi = int(textValueBODOVI);
int zvanje = int(textValueZVANJE);
int mii, vii;
if(bodovi>0){
if (mi) {
println("mi"+brojmi+i);
int ukupno = 162 + zvanje;
vii = ukupno - bodovi;
cp5.get(Textarea.class, "MIVI"+i).setText(textValueBODOVI+" "+vii);
mi=false;
vi=false;
cp5.get(Textfield.class, "BODOVI").clear();
cp5.get(Textfield.class, "ZVANJE").clear();
loop();
}
if (vi) {
println("vi"+i);
int ukupno = 162 + zvanje;
mii = ukupno - bodovi;
cp5.get(Textarea.class, "MIVI"+i).setText(mii+" "+textValueBODOVI);
mi=false;
vi=false;
cp5.get(Textfield.class, "BODOVI").clear();
cp5.get(Textfield.class, "ZVANJE").clear();
}
}
brojmi++; }
}
public void controlEvent(ControlEvent theEvent) {
mi = theEvent.getController().getName().equals("MI");
vi = theEvent.getController().getName().equals("VI");
}
public void clear() {
cp5.get(Textfield.class, "BODOVI").clear();
cp5.get(Textfield.class, "ZVANJE").clear();
}
Mmmmm, I don't know if I got your question.
If your question is: "i need draw() function to wait for input or something like that.", then use a flag (boolean value) to decide if draw or not. When user input something, assign true to that value.
So your code should be:
void draw () {
// put the code you want to run anyways here...
// code
// code
if (hasUserInput) {
// here write the code you should wait to execute
}
}

Resources