Thanks for your response so far. Please can you help me check the codes below.
This is for generating random numbers into the Arduino and sending it out:
- void setup()
- {
- Serial.begin(9600);
-
- }
-
- int buffer[20];
-
- void loop()
- {
-
- int rpm;
- int torque;
- int power;
-
- //Generating data that will be plotted
- rpm = random(1, 40);
- torque = random(1, 25);
- power = random(1, 50);
-
-
- output(rpm, torque, power);
-
- delay(10); //Need some delay else the program gets swamped with data
-
- }
-
- void output(int rpm, int torque, int power)
- {
- int pktSize;
-
- buffer[0] = 0xCDAB; //SimPlot packet header. Indicates start of data packet
- buffer[1] = 3*sizeof(int); //Size of data in bytes. Does not include the header and size fields
- buffer[2] = rpm;
- buffer[3] = torque;
- buffer[4] = power;
-
- pktSize = 2 + 2 + (3*sizeof(int)); //Header bytes + size field bytes + data
-
- //IMPORTANT: Change to serial port that is connected to PC
- Serial.write((uint8_t * )buffer, pktSize);
-
-
- }
And this other one is on the processing side to get the data and store into a csv file:
- import processing.serial.*;
- PrintWriter output;
- Serial port;
- int[] val=new int[20];
- void setup() {
- size(400, 400);
- noStroke();
- frameRate(10);
-
- output = createWriter("Results.csv");
- println(Serial.list());
- // print a list of all available ports
-
- port = new Serial(this, Serial.list()[0], 9600);
- // choose the port to which the Arduino is connected
- // on the PC this is usually COM1, on the Macintosh
- // this is usually tty.usbserial-XXX
-
-
-
- }
- void draw() {
- if (0 < port.available()) {
- output.print("x");
- output.print(",");
- output.print("y");
- output.print(",");
- output.print("z");
- output.println();
-
- int j=0;
- while (j<20){
-
- val[j] = port.read();
- output.print(val[j] + ",");
- j++;
- val[j] = port.read();
- output.print(val[j] + "," );
- j++;
- val[j] = port.read();
- output.print(val[j]);
- output.println();
- j++;
- }
-
- output.flush();
- output.close();
- }
When I compile and upload the code on the arduino, it seems okay but when I run that of processing it gives something like ArrayIndexOutOfBoundsException:20.
Please can you point out where am getting it wrong? Thanks.