I am trying to send and receive 10 bit integers (the range of analogRead()) first from processing to Arduino and then arduino will reply back the same number to processing.
I am aware that Serial.write and Serial.read both send and receive 8 bit (1 Byte) values and I have that working. Now want to add 2 more bits to my Byte. I am sure I am not the first person to ask this.
The idea of bit-shifting is very interesting and I think it will solve my problem, however I need help on doing this; preferably an example code would be nice.
Or maybe help me build on top of the code I already have.
void setup(){ println(Serial.list()); String portName = Serial.list()[0]; // change [0] for the Arduino serial port myPort = new Serial(this, "COM5", 57600); myPort.clear();
}
void draw() { // first contact while (!proceed) { if ( myPort.available() > 0) { inByte = myPort.read(); if (inByte == 'A') { println("Received " + inByte); myPort.clear(); proceed = true; } } } // send and receive test while (proceed) { if (!sending) { if ( myPort.available() > 0) { inByte = myPort.read(); println("Received " + inByte); myPort.clear(); sending = true; } } if (sending){ myPort.write(255); sending = false; } } }
Arduino
int inByte = 0;
void setup() { // setup Serial comm with computer Serial.begin(57600); Serial.print('A'); // send letter "A" }
import processing.serial.*; PrintWriter output; Serial myPort; String myData = null; String[] Data = new String[5000]; int numData = 0; int n = 0; int m = 0;
void setup() { println(Serial.list()); String portName = Serial.list()[0]; // change [0] for the Arduino serial port myPort = new Serial(this, "COM5", 57600); myPort.bufferUntil('\n'); // SerialEvent for new line myPort.clear(); String Name = "Data "+str(month())+"-"+str(day())+"-"+str(year()) +"_"+str(hour())+"."+str(minute())+".txt"; // Create file to save the captured data output = createWriter(Name); }
void keyPressed() { output.flush(); // Writes the remaining data to the file output.close(); // Finishes the file exit(); // Stops the program }
The Arduino sends data when a push button is activated and it sends the data described above for 3.1 seconds. The data in the arrays was captured every 5 ms. (200Hz). The total number of data sequence is 620 rows and in each row there are 5 data.
Here is the problem I have:
First I run the processing program, then I press the push button. The TX light is on for a short time (~3.1 seconds).
Now my code says, if I press any key on keyboard, flush/close the file, and terminate the program, right?... This does not work, and I have to stop the program by clicking the Stop Button in the processing window.
Here is the weird thing: After I close the program, I only have 2.8 seconds of data in the output file. where did the other 300 ms go?
I really appreciate it if you can help me understand this, or you have some idea on how to fix this.
Thanks