communicating 2 arduinos with PWM *video* and *code* provided. y so laggy?

So i have a 1 "master" arduino recieving MIDI signals and then outputing different PWM signals to trigger "slave" arduinos that are connected to a set of relays.

but when i output a PWM signal. the relay lags. it somestimes is really responsive while other times the relay stays on for a few seconds even tho im not pressing a key.

"Master" arduino code

#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();


#define LED 11                  // LED pin on Arduino Uno

void setup()
{
    pinMode(LED, OUTPUT);
    MIDI.begin();           // Launch MIDI, by default listening to channel 1.
    Serial.begin(115200);
}

void loop()
{
    if (MIDI.read())                // Is there a MIDI message incoming ?
    {                    
        switch(MIDI.getType())      // Get the type of the message we caught
        {        
            case midi::NoteOn:       // If it is a note on message,
                if( MIDI.getData1() == 48) // note off by zero velocity
                analogWrite(LED,127);
               else if
                 ( MIDI.getData1() == 50) // note off by zero velocity
                analogWrite(LED,100);
                break;
         
                
            case midi::NoteOff:
               digitalWrite(LED,LOW); 
               break;
            // See the online reference for other message types
            default:
                break;
        }
    }
}

"slave" arduino code

int digitalPinin = 10;
int in1 = 2;
float pulseWidth = 0;

void setup() {
  Serial.begin(9600);
  pinMode(in1, OUTPUT);
  digitalWrite(in1, HIGH);
  

}

void loop() {
 pulseWidth = pulseIn (digitalPinin,HIGH) / 100;
 Serial.println(pulseWidth);

 if(pulseWidth == 10)digitalWrite(in1, LOW);

Why would you use analogWrite() for communication?

Just send the value using Serial or SPI if the distance is very short.

...R

Th code of the slave contains Serial.prints… What values do they give You?
Why use rather slow stuff like relays and not things like fast MOSFET transistors?

Robin2:
Why would you use analogWrite() for communication?

Just send the value using Serial or SPI if the distance is very short.

...R

i dunno how. can you post a tutorial link or any reference?

Why use rather slow stuff like relays and not things like fast MOSFET transistors?
[/quote]

when i analogWrite(pin, 127); it returns 10 in the serial monitor.

i am using relays because i am trying to control long lengths of 12v LED Strips. im just using small amounts of led strips to get the coding working and to my standard.

when i analogWrite(pin, 127); it returns 10 in the serial monitor.

50% duty cycle at 490Hz should be around 10 microseconds.
What's the problem?
Edit: whoops - decimal point misplace. Sorry.
Double edit: I missed your divide by 100. Original comment stands.

AWOL:
50% duty cycle at 490Hz should be around 10 microseconds.
What's the problem?
Edit: whoops - decimal point misplace. Sorry.
Double edit: I missed your divide by 100. Original comment stands.

why is it laggy? why does the light act as if the button is sticky?

What if pulseIn returns 11?
Or 9?

You turn the relay on if the pulse width is equal to 10.

Don't you need another line to turn the relay off when the pulse width is less than 10?

JohnLincoln:
You turn the relay on if the pulse width is equal to 10.

Don't you need another line to turn the relay off when the pulse width is less than 10?

Good point - which begs the question "what is turning the relays off?"

Attaching the entire code, especially for the slave, had been good.
Use <= 10 for one action and >10 for the reverse action.

i think im gunna try i2c or spi i dunno which one yet.... reading and researching.....

beensolo:
i dunno how. can you post a tutorial link or any reference?

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.

The technique in the 3rd example will be the most reliable. It's what I use for communication between Arduinos.

You can send data in a compatible format with code like this

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

...R

A master Arduino to drive slave Arduinos to operate relays is not how it's normally done.
Only beginners might think that's the only way to have more I/O.

One 74HC595 shift register per 8-channel relay board is all you need.
That only uses three Aruino pins from the master, for as many 74HC595/relay boards as you like.
Leo..