Arduino Mega 2560 - sending serial data from Processing


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()
  servoD1.attach(4); //29






void loop()
  if (Serial.available()>0)
    sD1 =;
  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);
  cp5 = new ControlP5(this);
    .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() {

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


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

for (n = 0; n < numServos) {

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(','); // comma separator
Serial.println('>'); // end marker