2 Arduinos + Processing serial communication

Hi folks!
I have this project here with motorised sliders, I will try to explain it fast:

  1. From Processing user sets the position of motorised slider and sends it to Arduino MEGA.
  2. Arduino UNO is getting analog read from the motorised sliders’ potentiometer and sends data to Arduino MEGA via serial port (TX, RX).
  3. Arduino MEGA checks the position and moves the slider via motor if necessary.

I need two Arduinos because one cannot measure analog pin (position) and send HIGH to the motor at the same time (Also I don’t have enough pins for all the sliders).

Problem: How to communicate with Processing to Arduino MEGA, when it’s already communicating with UNO? At first it seemed no problem, since MEGA has 3 ports, but I just won’t send or get any data from MEGA when UNO is attached.

I hope some of you will have a clue what I’m doing wrong:) Cheers!

Here I have the code.

For UNO:

void setup() {
  Serial.begin(9600);
}

void loop() {
  int value = analogRead(A0); //this would be much more exciting if it was a sensor value
  byte data1 = value / 256;
  byte data2 = value % 256;

  Serial.write(data1);
  Serial.write(data2);
  delay(25);
}

MEGA:

//Arduino Pin Assignments
const int motorDown    = 2;   //H-Bridge control to make the motor go down
const int motorUp      = 3;   //H-Bridge control to make the motor go up
int Ack_tx = 2; //Acknowledgement tx.

//Inputs
const int wiper        = 0;   //Position of fader relative to GND (Analog 0)

//Variables
double faderMax        = 0;   //Value read by fader's maximum position (0-1023)
double faderMin        = 0;   //Value read by fader's minimum position (0-1023)
int state;
int board;   // 1 = Uno. 2 = Mega.
int on_off;  // 1 = On. 0 = Off.
int rec;


void setup() {
  Serial.begin(9600);      // start serial communication at 9600bps
  Serial1.begin(9600);     // start serial1 communication at 9600bps

  pinMode (motorUp, OUTPUT);
  pinMode (motorDown, OUTPUT);

  calibrateFader();
}

void loop() {

  if (Serial1.available())
  {
    byte b1=Serial1.read();  //Read Upper byte
    byte b2=Serial1.read();  //Read Lower byte
    rec=(b1*256)+b2;

      int wiperVal = rec;

    state = Serial.read();
    state = state * 4;

    if (state < analogRead(wiper) - 25 && state > faderMin) {
      digitalWrite(motorDown, HIGH);

      while (state < analogRead(wiper) - 25) {};  //Loops until motor is done moving
      digitalWrite(motorDown, LOW);

    }
    else if (state > analogRead(wiper) + 25 && state < faderMax) {
      digitalWrite(motorUp, HIGH);

      while (state > analogRead(wiper) + 25) {}; //Loops until motor is done moving
      digitalWrite(motorUp, LOW);

      Serial.println(wiperVal);

    }
  }


}

//Calibrates the min and max position of the fader
void calibrateFader() {
  //Send fader to the top and read max position
  digitalWrite(motorUp, HIGH);

  delay(250);
  digitalWrite(motorUp, LOW);

  faderMax = analogRead(wiper);


  //Send fader to the bottom and read max position
  digitalWrite(motorDown, HIGH);

  delay(250);
  digitalWrite(motorDown, LOW);

  faderMin = analogRead(wiper);

}

Processing:

import controlP5.*;
import processing.serial.*;
ControlP5 controlP5;

int lf = 10;    // Linefeed in ASCII
int value;
String myString = null;
Serial myPort;  // The serial port
int x = 0;
int value2;

void setup() {
  size(500, 300);
  background(0);
  // List all the available serial ports
  println(Serial.list());
  // I know that the first port in the serial list on my mac
  // is always my  Keyspan adaptor, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[1], 9600);
  myPort.clear();
  // Throw out the first reading, in case we started reading 
  // in the middle of a string from the sender.
  myString = myPort.readStringUntil(lf);
  myString = null;
}

void draw() {
  background(0);

  controlP5 = new ControlP5(this);
  controlP5.addSlider("value", 0, 255, 0, 10, 125, 300, 30);
  controlP5.addSlider("value2", 0, 255, 0, 10, 225, 300, 30);


  while (myPort.available() > 0) {
    myString = myPort.readStringUntil(lf);
    if (myString != null) {
      myString = trim(myString);
      value = int(myString);
      myPort.write(value2);
      println(value2);
    }
  }
}

I need two Arduinos because one cannot measure analog pin (position) and send HIGH to the motor at the same time

Not at the same time maybe, but so fast that it will seem as though they occurred at the same time.

I don't have enough pins for all the sliders

How many sliders do you have ?

It would be extremely strange if you need two Arduinos for your project.

Try writing a program so that one of them does everything.

...R

If the UNO is talking to the Mega using the Serial pins, that is going to confuse the hell out of Processing. Get the UNO off of the Mega's TX0 and RX0 pins.

UKHeliBob:
Not at the same time maybe, but so fast that it will seem as though they occurred at the same time.
How many sliders do you have ?

In this project both action should occur at the same time - I want to have force feedback from the sliders when using them. The project is more complicated than I have explained - it will be a shape-changing surface.

I will have 25 sliders. So 2 digital and 1 analog pin needed per slider. Mega has 15 analog ins.

PaulS:
If the UNO is talking to the Mega using the Serial pins, that is going to confuse the hell out of Processing. Get the UNO off of the Mega's TX0 and RX0 pins.

I understand that it would use the same serial port, that's why UNO is connected to TX1 and RX1 of MEGA. Processing is not receiving anything.

Mega has 15 analog ins.

Actually it has 16 analogue inputs. That does not solve your problem, but it is better get the facts right.

Now that we know more about your project ...

Problem: How to communicate with Processing to Arduino MEGA, when it's already communicating with UNO? At first it seemed no problem, since MEGA has 3 ports, but I just won't send or get any data from MEGA when UNO is attached.

I am not familiar with Processing but I presume it can communicate on more than one serial connection so you should be able to open one connection for USB communication with the Uno and another connection for USB communication with the Mega.

The Uno and Mega could possibly communicate by sending data via the Processing program OR you could use SoftwareSerial on the Uno to make an extra serial connection to one of the spare serial ports on the Mega.

...R