Nick Lee
Nick Lee

Reputation: 11

Using two Arduino Mega boards to control 64 LEDs

I am a beginner in Arduino. I would like to control 64 LEDs using 2 Arduino Mega boards.

The logic is:

  1. Arduino 1 blinks the LED in serial ===>
  2. Arduino 1 finished blinking and sends the signal (HIGH) to Arduino 2 ===>
  3. Arduino 2 blinks the LED in serial ===>
  4. Arduino 2 finishes blinking and sends a signal (HIGH) to Arduino 1, and both Arduinos reset using asm volatile("jmp 0").

I am using pin 52 as TX and pin 53 as RX for both.

And now the problem is that after Arduino 1 finishes blinking and sends out the signal (HIGH) to Arduino 2, it doesn't wait for the signal from Arduino 2 but resets itself.

Can anyone have a look of my code to see whether it is a logical mistake or a coding error?

digitalWrite(TX, HIGH);   
delay(1000);    
if(digitalRead(RX)==HIGH) {
  asm volatile("jmp 0");
}

Upvotes: 1

Views: 72

Answers (2)

Michaël Roy
Michaël Roy

Reputation: 6481

You need to interlock the shutdown sequence:

// Arduino 1:
digitalWrite(TX, HIGH);    // set high for 1 second
delay(1000);
while (digitalRead(RX))    // wait for a low pulse from # 2
 ;

digitalWrite(TX, LOW);     // #2 is latched.
while (!digitalRead(RX))  
 ;
// RX is high again, #2 is ready to reset as well...
asm volatile("jmp 0");


// arduino # 2, assuming you have already detected a pulse longer than 500ms on RX, 
// sequence:
digitalWrite(TX, LOW);  // indicate we're latched
while(digitalRead(RX))  // wait for end of pulse from #1
  ; 
digitalWrite(TX, HIGH);  // indicate we're ready
delay(2);                // make sure #1 gets it.
asm volatile("jmp 0");   // reset at approx same time as #1

Arduino #2 should keep its TX line HIGH while it's busy and wants to delay the reset.

Upvotes: 0

dda
dda

Reputation: 6213

digitalWrite(TX, HIGH);   
delay(1000);    
if(digitalRead(RX)==HIGH) {
  asm volatile("jmp 0");
}

When you do this, you have to make sure that Arduino 2 sets its TX pin to LOW first, before playing with the LEDs. Only when it is finished, will should it set its TX pin to HIGH.

Upvotes: 0

Related Questions