Slave Sketch for mlink 4x4 keypad

Forum for posting topics and questions about anything.
Post Reply
philliph
Posts: 7
Joined: Sat Apr 05, 2025 5:24 pm

Slave Sketch for mlink 4x4 keypad

Post by philliph » Tue Apr 15, 2025 4:29 pm

Further to my entry dated sun 13 Apr on the mlink 4x4 keypad product specific forum, I have amended the sketch as suggested. I trust I have put the byte = 255 line in the right place and have added the last break statement.

The problem still remains. The selected relay cycles through On and Off continually until the other relay is selected, when it in it's turn cycles in the same way.

Do you have any further thoughts?

Many thanks in advance,

PhillipH

/*
4x4 I2C Slave Relay Demo
using Arduino Mega
*/
// Include Arduino Wire library for I2C
#include <Wire.h>

// Define Slave I2C Address
#define SLAVE_ADDR 9

//define Relay States
#define N_CLOSED 1
#define N_OPEN 0

#define Relay_1 53
#define Relay_2 52

// Variable for received data
int rd;
byte value = 255;

void setup() {

pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);

digitalWrite(Relay_1, N_CLOSED);
digitalWrite(Relay_2, N_CLOSED);

// Initialize I2C communications as Slave
Wire.begin(SLAVE_ADDR);

// Function to run when data received from master
Wire.onReceive(receiveEvent);

// Setup Serial Monitor
Serial.begin(9600);
Serial.println("Waiting valid route selection");
}

void receiveEvent() {
// read one character from the I2C
rd = Wire.read();
// Print value of incoming data
Serial.println(rd);

}
void loop() {
delay(50);

if (rd != 255)
{
switch(rd)
{
case 1:
delay(500);
digitalWrite(Relay_1, N_OPEN);
delay(500);
digitalWrite(Relay_1, N_CLOSED);
break;

case 2:
delay(500);
digitalWrite(Relay_2, N_OPEN);
delay(500);
digitalWrite(Relay_2, N_CLOSED);
break;


}
}
}

andrew
Site Admin
Posts: 1464
Joined: Sun Aug 05, 2012 4:15 pm

Re: Slave Sketch for mlink 4x4 keypad

Post by andrew » Wed Apr 16, 2025 9:48 am

Hi Phillip, your sketch is still missing the line that sets the value (rd) variable back to 255:

  1. void loop()
  2. {
  3.   delay(50);
  4.  
  5.   if (rd != 255)
  6.   {
  7.     switch(rd)
  8.     {
  9.       case 1:
  10.         delay(500);
  11.         digitalWrite(Relay_1, N_OPEN);
  12.         delay(500);
  13.         digitalWrite(Relay_1, N_CLOSED);
  14.         break;
  15.      
  16.       case 2:
  17.         delay(500);
  18.         digitalWrite(Relay_2, N_OPEN);
  19.         delay(500);
  20.         digitalWrite(Relay_2, N_CLOSED);
  21.         break;
  22.     }
  23.     rd = 255;  //<---- Missing this line
  24.   }
  25. }
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

philliph
Posts: 7
Joined: Sat Apr 05, 2025 5:24 pm

Re: Slave Sketch for mlink 4x4 keypad

Post by philliph » Wed Apr 16, 2025 10:36 pm

Sorry, missed that. Thank you again PhillipH

Post Reply

Return to “General Discussion”