connecting two arduinos


My name is Daniel. I'm new here.

Need some help with my code.

I have two Arduinos, Uno and Mega.

I want to connect both of them. Now I connected LEDs for testing, ones the code will run I'll connect instead of the LEDs other components (IR sensor and step motors). Of course I'll make the adjustments for the components...

Here is my code for the Mega and the Uno:

-------------------Mega---------------- int a; int b;

void setup() { pinMode(30,INPUT);//connected to pin4 on the Uno pinMode(31, INPUT);//connected to pin5 on the Uno pinMode(32, OUTPUT);//connected to LED 1 pinMode(33, OUTPUT);//connected to LED 2


void loop() { a=digitalRead(30); b=digitalRead(31);

if(a=HIGH) {zzz();} if(a=LOW) {vvv();} if(b=HIGH) {yyy();} if(b=LOW) {www();} }

void zzz() { if(a=HIGH) { digitalWrite(32, HIGH); digitalWrite(33, LOW); } else {Else();} }

void vvv() { if(a=LOW) { digitalWrite(32, LOW); } else {Else();} }

void yyy() { if(b=HIGH) { digitalWrite(33, HIGH); digitalWrite(32,LOW); } else {Else();} }

void www() { if(b=LOW) { digitalWrite(33, LOW); } else {Else();} }

void Else() { if(a=HIGH) {zzz();} if(a=LOW) {vvv();} if(b=HIGH) {yyy();} if(b=LOW) {www();} }


int a; int b;

void setup() { pinMode(2,INPUT);//connected to microswitch1 pinMode(3, INPUT);//connected to microswitch2 pinMode(4, OUTPUT);//connected to pin30 on the Mega pinMode(5, OUTPUT);//connected to pin31 on the Mega


void loop() { a=digitalRead(2); b=digitalRead(3); if(a=HIGH) {zzz();} if(a=LOW) {vvv();} if(b=HIGH) {yyy();} if(b=LOW) {www();} }

void zzz() { if(a=HIGH) { digitalWrite(4, HIGH); digitalWrite(5, LOW); } else {Else();} } void vvv() { if(a=LOW) { digitalWrite(4, LOW);} else {Else();} } void yyy() { if(b=HIGH) { digitalWrite(5, HIGH); } else {Else();} } void www() { if(b=LOW) { digitalWrite(5, LOW);} else {Else();} } void Else() { if(a=HIGH) {zzz();} if(a=LOW) {vvv();} if(b=HIGH) {yyy();} if(b=LOW) {www();} }

Can you tell me please where is the problem with the code?


So far, what i see is a repetition of Ifs inside those functions of your called zzz and vvv and stuff. Remember they were called from an IF statement already, why evaluate the same condition again?

Second, get rid of the useless elses. If you plan on using them in the future, either comment them out or remember to add in the future.

Also I've noticed you use a Reserved word to name a function: void Else()... If I remembre my lessons well it could be a big No-No. Just get rid of those elses, what do you want them to do?

If you aim at creating a loop with them, notice they lay inside "void loop()". Everything in there will be automatically repeated over and over and over and rolli'n and cooki'n.

Percebes esta frase?

Do you have a common GND connection between the two boards ?

Using names like zzz is silly. Using meaningful names makes it much easier to develop and maintain code.

If you want to send serial data between the two boards the examples (3rd one would be best) in Serial Input Basics are simple reliable ways to receive data. With minor modifications you can use a copy on the Mega and on the Uno.

If you want to use Infra Red for communication have a look at this IR Thread.


thank you all for the answers.

I'll start from the beginning :)

I want to control a step motor by encoder. the step motor is turning a pulley on which rolled a rope. Before reaching the pulley the rope is going thru the encoder. That allows me to know at any given moment how much rope is on the pulley. As well, I can stop the motor every time that a certain amount of rope got thru the encoder.

I connected the encoder and the motor to the Mega. Every time that both the encoder an the motor were running together, there was a glitch. The motor and the encoder are not running smoothly, as like it was to much for the Mega to read the pulses from the encoder, print the read from the encoder (serial read ln) and run the step motor, all in the same time.

So I decided to split the tasks by connect the encoder to the Mega and the motor to the Uno. This is why I need that to Mega and The Uno will communicate (only HIGH and LOW, no need the send any data)

The motor and the encoder are not running smoothly, as like it was to much for the Mega to read the pulses from the encoder, print the read from the encoder (serial read ln) and run the step motor, all in the same time.

The Mega alone should easily be able to do all of that. Please post the code that gave you problems.

#include <Stepper.h>
int val;
int encoder0PinA = 21;
int encoder0PinB = 20;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;

void setup() {
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
Serial.begin (9600);

void loop() {



n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
} else {
Serial.println (encoder0Pos);

encoder0PinALast = n;


Please read the two posts by Nick Gammon at the top of the Forum for guidelines on posting here, especially the use of code tags when posting source code. It will help us help you.

How about using interrupt to read the encoder and allowing the loop() function to run freely and drive the stepper ?

As well as what @UKHeliBob has said you have huge delay()s in your code. Look at how the stepper timing is managed without blocking in the second example in this Simple Stepper Program


UK HeliBob I thought to use "interrupt" but did found the right logical way to use it. I'm new in programming (as you can see from the code that I posted).

Robin2 How can I combine the code from the example you sent posted, with the code for the encoder? Is there a light code that can run with the step motor? I have a feeling that the code I posted here, for the encoder, is not so elegant.

I think to replace the in the "void actOnButtons() {" the reading from the buttons for the reading from the encoder. The problem is that I'm not sure how to integrate it using the sub functions ( void readButtons(),void singleStep() Etc.)

thank you very much for your time and patience thanks to you people are not giving up on programming :)

byte directionPin = 9;
byte stepPin = 8;

byte buttonCWpin = 10;
byte buttonCCWpin = 11;

boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;

byte ledPin = 13;

unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 25; // milliseconds

void setup() { 

  Serial.println("Starting Stepper Demo with millis()");

  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonCWpin, INPUT_PULLUP);
  pinMode(buttonCCWpin, INPUT_PULLUP);

void loop() { 

    curMillis = millis();


void readButtons() {

    buttonCCWpressed = false;
    buttonCWpressed = false;

    if (digitalRead(buttonCWpin) == LOW) {
        buttonCWpressed = true;
    if (digitalRead(buttonCCWpin) == LOW) {
        buttonCCWpressed = true;

void actOnButtons() {
    if (buttonCWpressed == true) {
        digitalWrite(directionPin, LOW);
    if (buttonCCWpressed == true) {
        digitalWrite(directionPin, HIGH);

void singleStep() {
    if (curMillis - prevStepMillis >= millisBetweenSteps) {
        prevStepMillis += millisBetweenSteps;
        digitalWrite(stepPin, HIGH);
        digitalWrite(stepPin, LOW);

I'm not clear about what relationship you want between the encoder pulses and the stepper steps. Describe exactly what should happen using ordinary English rather than code.

It would also be useful to know the maximum rate of encoder pulses (pulses per second) as that may affect how to deal with them.


I thought to use "interrupt" but did found the right logical way to use it.

What you do is to use loop() to run the code that happens all the time such as controlling the stepper. You connect the encoder to one or more of the interrupt pins and when triggered an interrupt service routine (ISR) runs. At its simplest the ISR flags that it has been triggered by setting or incrementing a variable which is acted upon in loop().

Doing things this way allows the foreground tasks, such as controlling the stepper, to run continuously whilst events that could happen at any time, such as a pulse from the encoder are only acted upon when they happen.

I'll try to explain what I was trying to do:

Turn on the encoder => turn on the motor=> when the number of the pulses from the encoder >= 1000 => stop the motor => turn off encoder => next sensor.

From what I understood the interrupt is turning on only when there is a chang in the pin (low, change, rising,falling). So, to use the interrupt for my program I'll have to do something like that?:

{digitalWrite(interrupt Pin, HIGH);}

The "if" function will not slow down the code?

The “if” function will not slow down the code?

You have to face facts. The Arduino cannot do more than one thing at a time but what it can do is do things sequentially very fast, at least in human terms.

What using an interrupt does is to allow the Arduino to do its main task, in your case controlling the stepper, for the majority of time and only do something else such as reading the encoder and acting on it when strictly necessary.

Incrementing a variable and checking its value, or maybe better still decrementing a variable and checking whether it is zero, are very quick to execute compared to reading and writing to ports so will not slow the code appreciably if written correctly.

shats: ISo, to use the interrupt for my program I'll have to do something like that?:

{digitalWrite(interrupt Pin, HIGH);}

It seems to me you have the concept completely back to front. YOU don't set the interrupt pin HIGH - the encoder does. The code in your ISR could be like this

void myEncoderISR() {
   pulseCount ++;
   newPulse = true;

and the corresponding code in loop() would be

if (newPulse == true) {
   prevPulseCount = currentPulseCount;
      currentPulseCount = pulseCount;
   newPulse = false;
if (currentPulseCount - prevPulseCount >= 1000) {
   // stop motor