Help me optimize my code please (Arduino, Serial & Firmata)
in
Integration and Hardware
•
4 months ago
Could somebody please glance at my code and tell me if I am making any obvious mistakes? Somehow it is introducing a significant amount of lag, which I cannot account for.
I am using two Arduinos. Arduino1 reads 8 capacitive sensors and sends them to my computer via serial connection. The other Arduino2 is running Firmata and controlls 8 motors.
If I am running the serial consule of Arduino, the readings from Arduino1 appear snappy and fast. In my own software however, it lags behind by ~500ms
Here is the code:
- //handles the touch issues.
- // incoming touch is a serial connection, outgoing touch runs via firmata
- //array for storing incoming values, arranged according to evernote pins??
- int[] incomingValues = new int[8];
- //threshholds for deciding whether there is contact or not
- int[] thresholds = new int[8];
- //array for storing motor connections
- int[] motorPins = new int[8];
- import processing.serial.*; //import the Serial library
- import cc.arduino.*;
- Arduino arduino;
- char end = '\n'; // /nl is the end of the message
- String incomingString; // the string which is read
- Serial port; // The serial port
- String[] a; //string for temporarily storing the incoming values
- void setup() {
- println(Arduino.list()); //list all arduinos connected
- arduino = new Arduino(this, Arduino.list()[3], 57600);
- port = new Serial(this, Serial.list()[2], 9600);
- incomingString = port.readStringUntil(end);
- port.clear(); // function from serial library that throws out the first reading, in case we started reading in the middle of a string from Arduino
- incomingString = null; // initially, the string will be null (empty)
- //set all pins as output and set then low
- for (int i = 0; i <= 13; i++) {
- arduino.pinMode(i, Arduino.OUTPUT);
- arduino.digitalWrite(i, Arduino.LOW);
- }
- //assign which motor is connected to wich pin (i.e. motor 0 is connected to pin 11)
- motorPins[0] = 11;
- motorPins[1] = 10;
- motorPins[2] = 9;
- motorPins[3] = 8;
- motorPins[4] = 7;
- motorPins[5] = 6;
- motorPins[6] = 5;
- motorPins[7] = 3;
- //assign the thresholds for each sensor (i.e. if sensor 2 has a reading higher then 3 its a contact)
- thresholds[0] = 1;
- thresholds[1] = 2;
- thresholds[2] = 3;
- thresholds[3] = 2;
- thresholds[4] = 2;
- thresholds[5] = 2;
- thresholds[6] = 2;
- thresholds[7] = 2;
- }
- void draw() {
- while (port.available () > 0) { //as long as there is data coming from serial port, read it and store it
- incomingString = port.readStringUntil(end);
- }
- if (incomingString != null) { //if the string is not empty
- a = split(incomingString, ", ");
- //this is done in order to remap the incoming values so they are in the apropreate order
- incomingValues[0] =int(a[6]);
- incomingValues[1] =int(a[7]);
- incomingValues[2] =int(a[0]);
- incomingValues[3] =int(a[1]);
- incomingValues[4] =int(a[2]);
- incomingValues[5] =int(a[4]);
- incomingValues[6] =int(a[3]);
- incomingValues[7] =int(a[5]);
- //print the values for debugging
- for (int i = 0; i<8; i++) {
- print(incomingValues[i]);
- print(", ");
- }
- println();
- }
- //set the motors high or low, depending on if there is a touch or not
- for (int i = 0; i<8; i++) {
- if (incomingValues[i] - thresholds[i] > 0) {
- arduino.digitalWrite(motorPins[i], Arduino.HIGH);
- }
- else {
- arduino.digitalWrite(motorPins[i], Arduino.LOW);
- }
- }
- }
Hope you can help me
Cheers
p.
1