0

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

Code for Arduino Mega slave:

//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
 public:
 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:
 Serial.begin(9600);
 Wire.begin(SLAVE_ADDRESS);
 Wire.onReceive(receiveData);
 Wire.onRequest(slavesRespond);
 pinMode(PWR_LED, OUTPUT);
}
void loop() {
 delay(1000);
 Serial.print("LED_T_ON=");
 Serial.println(LED_T_ON);
 Serial.print("LED_T_OFF=");
 Serial.println(LED_T_OFF);
 Serial.println();
 relay3.Update();
 switch (Mval) {
 case 1 ... 25:
 LED_TinH_ON = Mval;
 Serial.print("LED time on: ");
 Serial.print(LED_TinH_ON);
 Serial.println("h");
 LED_T_ON = LED_TinH_ON*1000;//*60*60;
 LED_T_OFF = (24-LED_TinH_ON)*1000;//*60*60;
 break;
 }
}
void receiveData(int byteCount) {
 LastMasterCommand = Wire.read();
 while (Wire.available()) {
 int msg = Wire.read();
 if (isDigit(msg)) {
 inString += (char)msg;
 }
 }
 Mval = inString.toInt();
 //Serial.println(LastMasterCommand);
 inString = "";
 //Serial.println(Mval);
}
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?

asked May 2, 2018 at 22:38
4
  • 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? Commented May 2, 2018 at 22:44
  • Same. Also LED_T_ON is not changed anywhere (nor is OnTime). Commented May 2, 2018 at 22:45
  • My bad, edited to display the right code Commented May 2, 2018 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) Commented May 2, 2018 at 22:52

1 Answer 1

2

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

answered May 2, 2018 at 22:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.