1

I'm trying to connect an Arduino Nano Every and Processing via serial. I'm getting no errors on either side, and the arduino serial functionality works otherwise on the arduino IDE.

I'm connecting on the right port, and the port is busy while the processing code is running (as you would expect), however, the variable inside the arduino is not updating. In fact, the whole if serial.available statement is not executing.

Arduino:

int var;

void setup() { Serial.begin(9600); }

void loop() { if (Serial.available()) { var = Serial.read(); } Serial.println(var); delay(100); }

Processing:

import processing.serial.*;

// The serial port Serial myPort;

void setup() { // List all the available serial ports: printArray(Serial.list()); // open port myPort = new Serial(this, Serial.list()[2], 9600); }

void draw() { try { myPort.write(1); } catch(Exception e) { // Print detailed error information to the console. System.err.println(e); e.printStackTrace(); } delay(50); }

PS. Asked moderator to close previous post on topic.

Zhelyazko Grudov
  • 367
  • 2
  • 15

2 Answers2

1

Most Arduinos (and the Nano Every is one of them) will reset when the serial connection is reopened (You close it by closing the processing sketch and open it again with the Serial Monitor). Variables are stored in RAM, which gets wiped and reinitialized after a reset or power loss.

So this basically happens: You send the data from processing over serial to the Arduino. The Arduino puts the received value into the variable in its RAM. Then you close the processing sketch, effectively closing the serial connection. Then you open the Serial Monitor, which opens the serial connection again. This triggers the reset of the Arduino and wipes the previous variable value from the RAM. You will only ever see the initial variable value (zero) this way.

You can prevent a reset over serial if you connect a capacitor between the Reset pin and ground.

Though I would say this is probably not what you really should do. Instead just read the data from serial inside your processing sketch. After you have read it you can print it.


As a side note: The reset-on-serial-reconnect functionality is used for programming the Arduino over serial (through the USB cable). The new program is written into the program memory (flash) by the bootloader. The bootloader runs directly after a reset/power-on. So to upload a new program to the Arduino the Arduino IDE has to first reset the Arduino to talk to the bootloader.

chrisl
  • 16,622
  • 2
  • 18
  • 27
1

If you open your Processing sketch and send the data to the Arduino without opening the serial monitor in the Arduino IDE, the data gets through. Here is your Arduino sketch which is slightly modified. Disclaimer - tested on an Uno.

There are other options for serial monitors which may work (varies by OS), as well as other options to interface between Processing and Arduino, such as Firmata

int var;

void setup() { Serial.begin(9600); pinMode(2, OUTPUT); digitalWrite(2, LOW); }

void loop() { if (Serial.available()) { var = Serial.read(); digitalWrite(2, HIGH); delay(50); digitalWrite(2, LOW); } Serial.println(var); delay(100); }

EDIT

Arduino IDE version 1.0.6.2 with Processing 2.2-ish would let you open the serial monitor in the Arduino IDE and "monitor" the data. Since you can't do this anymore, what about making your Processing app open 2 windows. One window could be a home brewed serial monitor, and the other the sketch you are working on. Using Processing version 3.5.3, it is only 15 lines of code to open up 2 windows. I'm sure the actual implementation of such a system would be a good challenge.

VE7JRO
  • 2,515
  • 19
  • 27
  • 29