Arduino Mega 2560 - sending serial data from Processing

Hi,

I am trying to run 22 servos from an Arduino Mega 2560 using input from Processing, and I can’t seem to send any information from Processing to the Arduino. I can send Serial data the other way (Arduino > processing), and my Uno and Nano work both ways with this code, but the Mega doesn’t seem to want to pull from the Serial port.

I have read a lot of forums talking about hooking up 10uf capacitors to the GND & RST on the mega or putting delays after starting the Serial port in the processing code and I have tried every “solution” I can find but none seem to work so I’m thinking there might be a better way to send the serial data that I’m missing.

Here is the test program I m using:

Arduino Code (ignore all the servo attaching & detaching)

#include <Servo.h>

int sD1 = 60;

Servo servoM1;
Servo servoP1;
Servo servoK4;
Servo servoD3;
Servo servoM4;
Servo servoP4;
Servo servoD5;
Servo servoD1;
Servo servoM5;
Servo servoP2;
Servo servoMe5;
Servo servoP5;
Servo servoD4;
Servo servoP3;
Servo servoK3;
Servo servoK5;
Servo servoK2;
Servo servoM3;
Servo servoM2;
Servo servoK1;
Servo servoMe4;
Servo servoD2;

void setup()
{
  Serial.begin(9600);
  
  servoM1.attach(22);
  servoP1.attach(23);
  servoK4.attach(24);
  servoD3.attach(25);
  servoM4.attach(26);
  servoP4.attach(27);
  servoD5.attach(28);
  servoD1.attach(4); //29
  servoM5.attach(30);
  servoP2.attach(31);
  servoMe5.attach(32);
  servoP5.attach(33);
  servoD4.attach(50);
  servoP3.attach(35);
  servoK3.attach(36);
  servoK5.attach(37);
  servoK2.attach(38);
  servoM3.attach(39);
  servoM2.attach(40);
  servoK1.attach(41);
  servoMe4.attach(42);
  servoD2.attach(43);

  //servoD1.detach();
  servoD2.detach();
  servoD3.detach();
  servoD4.detach();
  servoD5.detach();

  servoM1.detach();
  servoM2.detach();
  servoM3.detach();
  servoM4.detach();
  servoM5.detach();

  servoP1.detach();
  servoP2.detach();
  servoP3.detach();
  servoP4.detach();
  servoP5.detach();
  
  servoK1.detach();
  servoK2.detach();
  servoK3.detach();
  servoK4.detach();
  servoK5.detach();

  servoMe4.detach();
  servoMe5.detach();

}

void loop()
{
  if (Serial.available()>0)
  {
    sD1 = Serial.read();
  }
  servoD1.write(sD1); //85-15
}

Processing code (I’m using the controlP5 library to create sliders):

import controlP5.*;

Serial myPort;
ControlP5 cp5;

int D1;

void setup(){
  size (800, 800);
  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 9600);
  delay(1000);
  
  cp5 = new ControlP5(this);
 
  cp5.addSlider("D1")
    .setPosition(190,20) //x, y
    .setSize(50, 250)  //width, height
    .setRange(15, 85)  //slider range, low - high
    .setValue(50)  //start val
    .setColorBackground(color(0, 0, 255)) //top of slider colour
    .setColorForeground(color(0, 255, 0))  //bottom colour
    .setColorValue(color(255, 255, 255))  //wall colour
    .setColorActive(color(255, 0, 0))  //mouse over colour
    ;
}

void draw() {
  background(0,0,0);
  myPort.write(6);
}

Thank you!

EDIT: The whole system just started working after leaving it off for about an hour, unfortunately I didn’t change anything, so i don’t have a solution to the problem.

It sounds like slack wiring or the like. Serial input to Mega is the same in principle as for Uno and Nano - it just has more ports which, if anything, makes life easier.

Based on the edit to the Original Post I agree with the comment in Reply #1

Separately …

When you find yourself writing code like this

servoM1.attach(22);
  servoP1.attach(23);
  servoK4.attach(24);
  servoD3.attach(25);

it’s time to learn about arrays. All that could be done with

for (n = 0; n < numServos) {
    myServo[n].attach(servoPin[n]);
}

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

You could send the data for all the servos in a single message. The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

…R