Very Slow Arduino Connection

edited June 2016 in Arduino

Hi there,

I'm trying to send a string of 16 sensor values from Arduino to Processing unfortunately, the connection is extremely slow. It also reads fine from the Arduino serial monitor.

What I'm sending, the max for any of them is 255:

<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>

Can anyone see what I'm doing wrong?

Thanks in advance! Charles

ps: There actually seems to be a 15 second delay on the connection

To Processing:

import processing.serial.*;
Serial arduino;

boolean newData = false;

void setup() {
  size(700, 400);
  frameRate(60);
  arduino = new Serial(this, Serial.list()[5], 9600);
}

void draw() {
  background(255);
  char startMarker = '<';
  char endMarker = '>';
  char read;

  String out = " ";

  while (arduino.available() > 0 && newData == false) {
    read = char(arduino.read());
    if (read != ',' && read != '<' & read != '>') {
      out = out + read;
    } else if (read == '>') {
      newData = true;
    } else if (read == ',') {
      out = out + " " ;
    }
  }

  if (newData) {
    fill(0);
    textSize(30);
    text(out, width*0.1, width*0.2);
    newData = false;
  }
}

From Arduino:

/Mux control pins
const int s0 = 12;
const int s1 = 11;
const int s2 = 9;
const int s3 = 8;

//Mux in "SIG" pin
const int SIG_pin = 2;

const int sample_rate = 40;
unsigned long last = 0;



void setup() {
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);

  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);

  Serial.begin(9600);
}


void loop() {

  unsigned long current = millis();

  if ( current - last > sample_rate) {
    Serial.print('<');
    for (int i = 0; i < 16; i++) {
      Serial.print(readMux(i) / 4);
      if (i < 15) Serial.print(',');
    }
    Serial.println('>');
  }

}


int readMux(int channel) {
  int controlPin[] = {s0, s1, s2, s3};

  int muxChannel[16][4] = {
    {0, 0, 0, 0}, //channel 0
    {1, 0, 0, 0}, //channel 1
    {0, 1, 0, 0}, //channel 2
    {1, 1, 0, 0}, //channel 3
    {0, 0, 1, 0}, //channel 4
    {1, 0, 1, 0}, //channel 5
    {0, 1, 1, 0}, //channel 6
    {1, 1, 1, 0}, //channel 7
    {0, 0, 0, 1}, //channel 8
    {1, 0, 0, 1}, //channel 9
    {0, 1, 0, 1}, //channel 10
    {1, 1, 0, 1}, //channel 11
    {0, 0, 1, 1}, //channel 12
    {1, 0, 1, 1}, //channel 13
    {0, 1, 1, 1}, //channel 14
    {1, 1, 1, 1} //channel 15
  };

  //loop through the 4 sig
  for (int i = 0; i < 4; i ++) {
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }

  //read the value at the SIG pin
  int val = analogRead(SIG_pin);

  //return the value
  return val;
}

Answers

  • Took me far too long to work this one out but a arduino.clear() at the end of every cycle solved the problem. :)

Sign In or Register to comment.