I2C Connects but wont flash LEDs or stay on when a button is pressed

Hey All, I've recently got into I2C Between a Nano (Master) and Uno (Slave) and its working for the most part. All the connected LEDs turn on Slave side but wont flash or at the very least flicker when I drop the delay to a lower value.

and secondly, How would I be able to keep these ON when their buttons (Master side) are triggered and then turn them OFF when I press it again?

MASTER

#include <Wire.h>
const int slaveAddress = 1;
int BTN1 = 2;
int BTN2 = 3;
int BTN3 = 4;
int BTN4 = 5;
int BTN5 = 6;


int BUTTON1 = 0;
int BUTTON2 = 0;
int BUTTON3 = 0;
int BUTTON4 = 0;
int BUTTON5 = 0;

void setup()
{
  Wire.begin();
  pinMode(BTN1, INPUT_PULLUP);
  pinMode(BTN2, INPUT_PULLUP);
  pinMode(BTN3, INPUT_PULLUP);
  pinMode(BTN4, INPUT_PULLUP);
  pinMode(BTN5, INPUT_PULLUP);

  Serial.begin(9600); 
}

void loop()
{
   Wire.beginTransmission(slaveAddress);
   
   BUTTON1 = digitalRead(BTN1);
   BUTTON2 = digitalRead(BTN2);
   BUTTON3 = digitalRead(BTN3);
   BUTTON4 = digitalRead(BTN4);
   BUTTON5 = digitalRead(BTN5);
   
   Wire.write(BUTTON1);
   Wire.write(BUTTON2);
   Wire.write(BUTTON3);
   Wire.write(BUTTON4);
   Wire.write(BUTTON5);
      
   Wire.endTransmission();
//   delay(500);
}

SLAVE

#include <Wire.h>
int ledPin = A0;       // the pin that the LED is attached to
int ledPin1 = A1;       // the pin that the LED is attached to
int ledPin2 = 2;       // the pin that the LED is attached to
int ledPin3 = 3;       // the pin that the LED is attached to
int ledPin4 = 4;       // the pin that the LED is attached to
int ledPin5 = 5;       // the pin that the LED is attached to
int ledPin6 = 6;       // the pin that the LED is attached to
int ledPin7 = 7;       // the pin that the LED is attached to
int ledPin8 = 8;       // the pin that the LED is attached to
int ledPin9 = 9;       // the pin that the LED is attached to
int ledPin10 = 10;       // the pin that the LED is attached to
int ledPin11 = 11;       // the pin that the LED is attached to
int ledPin12 = 12;       // the pin that the LED is attached to
int ledPin13 = 13;       // the pin that the LED is attached to

int BUTTON1 = LOW;
int BUTTON2 = LOW;
int BUTTON3 = LOW;
int BUTTON4 = LOW;
int BUTTON5 = LOW;

void setup()
{
  Wire.begin(1);
  
  Wire.onReceive(receiveEvent);
   
   pinMode(ledPin, OUTPUT);
   pinMode(ledPin1, OUTPUT);
   pinMode(ledPin2, OUTPUT);
   pinMode(ledPin3, OUTPUT);
   pinMode(ledPin4, OUTPUT);
   pinMode(ledPin5, OUTPUT);
   pinMode(ledPin6, OUTPUT);
   pinMode(ledPin7, OUTPUT);
   pinMode(ledPin8, OUTPUT);
   pinMode(ledPin9, OUTPUT);
   pinMode(ledPin10, OUTPUT);
   pinMode(ledPin11, OUTPUT);
   pinMode(ledPin12, OUTPUT);
   pinMode(ledPin13, OUTPUT);
}

void loop()
{
//Nothing
}

void receiveEvent(int howMany){

BUTTON1 = Wire.read();
BUTTON2 = Wire.read();
BUTTON3 = Wire.read();
BUTTON4 = Wire.read();
BUTTON5 = Wire.read();

  if (BUTTON1 == LOW){
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin12, HIGH);
delay(500);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin12, LOW);  

digitalWrite(ledPin8, HIGH);
digitalWrite(ledPin3, HIGH);
delay(500);
digitalWrite(ledPin8, LOW);
digitalWrite(ledPin3, LOW);
delay(500);
  }

    if (BUTTON2 == LOW){
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin6, HIGH);
digitalWrite(ledPin7, HIGH);
digitalWrite(ledPin10, HIGH);
  }

    if (BUTTON3 == LOW){
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin5, HIGH);
digitalWrite(ledPin6, HIGH);
digitalWrite(ledPin8, HIGH);
digitalWrite(ledPin9, HIGH);
digitalWrite(ledPin10, HIGH);
digitalWrite(ledPin11, HIGH);
digitalWrite(ledPin12, HIGH);

  }

    if (BUTTON4 == LOW){
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin6, HIGH);
digitalWrite(ledPin8, HIGH);
digitalWrite(ledPin9, HIGH);
digitalWrite(ledPin10, HIGH);
digitalWrite(ledPin11, HIGH);
digitalWrite(ledPin12, HIGH);

  }

    if (BUTTON5 == LOW){
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin5, HIGH);
digitalWrite(ledPin6, HIGH);
digitalWrite(ledPin7, HIGH);
digitalWrite(ledPin10, HIGH);

  }
  
  if (BUTTON1 == HIGH && BUTTON2 == HIGH && BUTTON3 == HIGH && BUTTON4 == HIGH && BUTTON5 == HIGH) {
digitalWrite(ledPin, LOW);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, LOW);
digitalWrite(ledPin6, LOW);
digitalWrite(ledPin7, LOW);
digitalWrite(ledPin8, LOW);
digitalWrite(ledPin9, LOW);
digitalWrite(ledPin10, LOW);
digitalWrite(ledPin11, LOW);
digitalWrite(ledPin12, LOW);
digitalWrite(ledPin13, LOW);
  }
}

Only slave addresses above 7 are allowed.

I'd only set the variables in receiveEvent() and do the flashing in loop().

I've tried everything from 1-9 and nothing seems to make a difference, same responses, same delays, etc

I did try something like it but I don't suppose you could give me an example?

Nvm the last part I've realized what I got wrong, thanks for that.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.