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;
}
}
}
Slave Sketch for mlink 4x4 keypad
Re: Slave Sketch for mlink 4x4 keypad
Hi Phillip, your sketch is still missing the line that sets the value (rd) variable back to 255:
- 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;
- }
- rd = 255; //<---- Missing this line
- }
- }
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.
Re: Slave Sketch for mlink 4x4 keypad
Sorry, missed that. Thank you again PhillipH