Arduino and Processing Communication
in
Integration and Hardware
•
1 year ago
I currently have Arduino code that is either writing 0's or 1's to Serial.println based on the position of a switch. I have confirmed in the Serial Monitor that either a 0 or 1 is being outputted.
I am trying to use Processing to write back to the Arduino based on whether a 0 or 1 is received. The only issue is that Processing is receiving three apparently random numbers repeatably: 10,13, and 49. I have posted my code for both programs below. I realize that the Arduino code is not actually doing anything if it does receive the 'S' at this point. My main issue at this moment is trying to get Processing to read either the 0 or 1. Thank you for your help!
- int incomingByte;
- const int analogPin = A0;
- const int threshold = 1;
- void setup() {
- Serial.begin(9600);
- }
- void loop() {
- int analogValue = analogRead(analogPin);
- if (analogValue < threshold) {
- Serial.println('0');
- }
- else {
- Serial.println('1');
- }
- }
- import processing.serial.*;
- int incomingByte;
- Serial port;
- void setup(){
- println(Serial.list());
- port = new Serial(this, Serial.list()[0], 9600);
- port.bufferUntil('\n');
- }
- void draw() {
- }
- void serialEvent(Serial port) {
- incomingByte = port.read();
- println(incomingByte);
- if (incomingByte == 0) {
- port.write('S');
- }
- }
1