I'd like to alter the "LED_T_ON" values using an I2C enabled device (i.e. RPi)

Code for Arduino Mega slave:


#include <Wire.h>

#define SLAVE_ADDRESS 0x08

const int PWR_LED = 43;
int LED_TinH_ON = 18;
int LED_T_ON = 5000;
int LED_T_OFF = 5000;

String inString = "";    // string to hold input
byte LastMasterCommand = 0;
int Mval;

char user_input;

class Relay {
  int relayPin;
  long OnTime;     // milliseconds of on-time
  long OffTime;    // milliseconds of off-time

  // These maintain the current state
  int relayState;                 // pumpState used to set the PUMP
  unsigned long previousMillis;   // will store last time PUMP was updated

  // Constructor - creates a Timer and initializes the member variables and state
  Relay(int pin, long on, long off)
    relayPin = pin;
    pinMode(relayPin, OUTPUT);     

    OnTime = on;
    OffTime = off;

    relayState = LOW; 
    previousMillis = 0;  //ORIGINALLY 0

  void Update()
    // check to see if it's time to change the state of the TIMER
    unsigned long currentMillis = millis();

    if((relayState == HIGH) && (currentMillis - previousMillis >= OnTime))
      relayState = LOW;  // Turn it off
      previousMillis = currentMillis;  // Remember the time
      digitalWrite(relayPin, relayState);  // Update the actual LED
    else if ((relayState == LOW) && (currentMillis - previousMillis >= OffTime))
      relayState = HIGH;  // turn it on
      previousMillis = currentMillis;   // Remember the time
      digitalWrite(relayPin, relayState);   // Update the actual RELAY

Relay relay3(PWR_LED, LED_T_OFF, LED_T_ON);

void setup() {
 // initialize serial communication with computer:



  pinMode(PWR_LED, OUTPUT);

void loop() {



  switch (Mval) {
    case 1 ... 25:
      LED_TinH_ON = Mval;
      Serial.print("LED time on: ");
      LED_T_ON = LED_TinH_ON*1000;//*60*60;
      LED_T_OFF = (24-LED_TinH_ON)*1000;//*60*60;

void receiveData(int byteCount) {
  LastMasterCommand = Wire.read();
  while (Wire.available()) {
    int msg = Wire.read();

    if (isDigit(msg)) {
      inString += (char)msg;
  Mval = inString.toInt();
  inString = "";

void slavesRespond() {

When I change the value on the RPi the Arduino receives and displays the correct value but the timed relays don't actually use the updated values.

Am I missing something?

  • I see nothing there relating to I2C. What there can be "displaying the correct value" when there is neither anything receiving a value nor anything displaying anything anywhere? – Majenko May 2 '18 at 22:44
  • Same. Also LED_T_ON is not changed anywhere (nor is OnTime). – Nick Gammon May 2 '18 at 22:45
  • My bad, edited to display the right code – Yousef Abdelsalam May 2 '18 at 22:52
  • assuming maybe you have other code that is doing the I2C bit, if that code is changing the value of LED_T_ON that will have no effect on that relay object as that value is passed in during the construction. You would need to update relay3.OnTime ( your order/naming is also switched, relay3(PWR_LED, LED_T_OFF, LED_T_ON); calls Relay(int pin, long on, long off) – Chad G May 2 '18 at 22:52

Your updating LED_T_ON and LED_T_OFF but those values were already passed into the constructor and changing them will not change the object. you need to update relay3.OnTime and relay3.OffTime

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.