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.

6
  • 2
    How do you know that it doesn't work? I don't see you reading anything from the serial interface in the processing sketch. What are you actually seeing?
    – chrisl
    Commented Dec 11, 2021 at 14:04
  • I read the variable in the arduino IDE serial monitor, after waiting a few seconds and stopping the processing app. If the processing app successfully writes to the arduino, then var = 1, however, it's = 0. Commented Dec 11, 2021 at 17:43
  • how would you ever know the value of var?
    – jsotola
    Commented Dec 11, 2021 at 17:54
  • I print it on the serial monitor in arduino IDE. Am I missing sth fundamental? Commented Dec 11, 2021 at 18:26
  • 1
    I'm not sure how it is with the Nano Every, but other Arduinos reset every time that you reopen the serial port. And that happens when you close the processing program and open the Serial Monitor. If that is also the case with the Nano Every, then you cannot every get anything other than zero, because the value send by the processing sketch is already forgotten due to the reset of the Arduino. You would need to read and print from serial inside your processing sketch
    – chrisl
    Commented Dec 11, 2021 at 19:10

2 Answers 2

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.

2
  • Well this is something I would have never thought to consider. Thank you so much, Chris. I was completely stuck here! Commented Dec 12, 2021 at 14:58
  • I suppose this reset only happens if you open/close the serial connection from the PC. Since repeatedly opening and closing the serial monitor from the IDE doesn't cause the same reset? Commented Dec 12, 2021 at 15:09
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.

2
  • 1
    @Zhelyazko Grudov - Another way to open 2 windows: gist.github.com/atduskgreg/666e46c8408e2a33b09a Just copy and paste both sketches into ONE sketch and try it :)
    – VE7JRO
    Commented Dec 12, 2021 at 1:30
  • Thanks @ve7JRO for the answer. I think the issue was what Chris mentioned below with the reset. However, this is very useful information for debugging, so thank you for sharing. Commented Dec 12, 2021 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.