I want to control 4 shutters independently, so I extended the code (thx to @ludodoucet for pointing me in the right direction). But there is something strange happening and I don't know where to look.
Here's what I did: I multiplied most of the code by creating multiple shutter instances. "Shutters" is the original instance, shutters1 etc is the multiplied part. So for the moment I have 5 shutters ;-)
When I run the sketch, "shutters" stays at 0%. All other instances works fine.
When I comment out all parts involved "shutters", the problem moves to shutters1. All remaining three work just fine.
So, there is always a problem with the first instance that is not responding as it should. It seems as if whatever first instance is not responding on setLevel, or at least no more than 1%. I can't get my head around it...
#include <Shutters.h>
#include <EEPROM.h>
const byte eepromOffset = 0;
const byte eepromOffset1 = 1;
const byte eepromOffset2 = 2;
const byte eepromOffset3 = 3;
const byte eepromOffset4 = 4;
const unsigned long upCourseTime = 5000;
const unsigned long downCourseTime = 5000;
const unsigned long upCourseTime1 = 5000;
const unsigned long downCourseTime1 = 5000;
const unsigned long upCourseTime2 = 5000;
const unsigned long downCourseTime2 = 5000;
const unsigned long upCourseTime3 = 5000;
const unsigned long downCourseTime3 = 5000;
const unsigned long upCourseTime4 = 5000;
const unsigned long downCourseTime4 = 5000;
const float calibrationRatio = 0.1;
int relay1 = 35;
int relay2 = 37;
int relay3 = 39;
int relay4 = 41;
int schakelaar1 = 43;
int schakelaar2 = 45;
int schakelaar3 = 42;
int schakelaar4 = 44;
int schakelaar1State;
int schakelaar2State;
int schakelaar3State;
int schakelaar4State;
//0 default
void shuttersOperationHandler(Shutters* s, ShuttersOperation operation) {
switch (operation) {
case ShuttersOperation::UP:
Serial.println("shutters going up.");
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
break;
case ShuttersOperation::DOWN:
Serial.println("shutters going down.");
digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);
break;
case ShuttersOperation::HALT:
Serial.println("Shutter halted.");
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
break;
}
}
void readInEeprom(char* dest, byte length) {
for (byte i = 0; i < length; i++) {
dest[i] = EEPROM.read(eepromOffset + i);
}
}
void shuttersWriteStateHandler(Shutters* shutters, const char* state, byte length) {
for (byte i = 0; i < length; i++) {
EEPROM.write(eepromOffset + i, state[i]);
}
}
void onShuttersLevelReached(Shutters* shutters, byte level) {
Serial.print("shutters at ");
Serial.print(level);
Serial.println("%");
}
Shutters shutters;
//1
void shuttersOperationHandler1(Shutters* s, ShuttersOperation operation) {
switch (operation) {
case ShuttersOperation::UP:
Serial.println("shutters1 going up.");
break;
case ShuttersOperation::DOWN:
Serial.println("shutters1 going down.");
break;
case ShuttersOperation::HALT:
Serial.println("shutters1 halted.");
break;
}
}
void readInEeprom1(char* dest, byte length) {
for (byte i = 0; i < length; i++) {
dest[i] = EEPROM.read(eepromOffset1 + i);
}
}
void shuttersWriteStateHandler1(Shutters* shutters, const char* state, byte length) {
for (byte i = 0; i < length; i++) {
EEPROM.write(eepromOffset1 + i, state[i]);
}
}
void onShuttersLevelReached1(Shutters* shutters, byte level) {
Serial.print("shutters1 at ");
Serial.print(level);
Serial.println("%");
}
Shutters shutters1;
//2
void shuttersOperationHandler2(Shutters* s, ShuttersOperation operation) {
switch (operation) {
case ShuttersOperation::UP:
Serial.println("shutters2 going up.");
break;
case ShuttersOperation::DOWN:
Serial.println("shutters2 going down.");
break;
case ShuttersOperation::HALT:
Serial.println("shutters2 halted.");
break;
}
}
void readInEeprom2(char* dest, byte length) {
for (byte i = 0; i < length; i++) {
dest[i] = EEPROM.read(eepromOffset2 + i);
}
}
void shuttersWriteStateHandler2(Shutters* shutters, const char* state, byte length) {
for (byte i = 0; i < length; i++) {
EEPROM.write(eepromOffset2 + i, state[i]);
}
}
void onShuttersLevelReached2(Shutters* shutters, byte level) {
Serial.print("shutters2 at ");
Serial.print(level);
Serial.println("%");
}
Shutters shutters2;
//3
void shuttersOperationHandler3(Shutters* s, ShuttersOperation operation) {
switch (operation) {
case ShuttersOperation::UP:
Serial.println("shutters3 going up.");
break;
case ShuttersOperation::DOWN:
Serial.println("shutters3 going down.");
break;
case ShuttersOperation::HALT:
Serial.println("shutters3 halted.");
break;
}
}
void readInEeprom3(char* dest, byte length) {
for (byte i = 0; i < length; i++) {
dest[i] = EEPROM.read(eepromOffset3 + i);
}
}
void shuttersWriteStateHandler3(Shutters* shutters, const char* state, byte length) {
for (byte i = 0; i < length; i++) {
EEPROM.write(eepromOffset3 + i, state[i]);
}
}
void onShuttersLevelReached3(Shutters* shutters, byte level) {
Serial.print("shutters3 at ");
Serial.print(level);
Serial.println("%");
}
Shutters shutters3;
//4
void shuttersOperationHandler4(Shutters* s, ShuttersOperation operation) {
switch (operation) {
case ShuttersOperation::UP:
Serial.println("shutters4 going up.");
break;
case ShuttersOperation::DOWN:
Serial.println("shutters4 going down.");
break;
case ShuttersOperation::HALT:
Serial.println("shutters4 halted.");
break;
}
}
void readInEeprom4(char* dest, byte length) {
for (byte i = 0; i < length; i++) {
dest[i] = EEPROM.read(eepromOffset4 + i);
}
}
void shuttersWriteStateHandler4(Shutters* shutters, const char* state, byte length) {
for (byte i = 0; i < length; i++) {
EEPROM.write(eepromOffset4 + i, state[i]);
}
}
void onShuttersLevelReached4(Shutters* shutters, byte level) {
Serial.print("shutters4 at ");
Serial.print(level);
Serial.println("%");
}
Shutters shutters4;
void setup() {
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(schakelaar1, INPUT_PULLUP);
pinMode(schakelaar2, INPUT_PULLUP);
pinMode(schakelaar3, INPUT_PULLUP);
pinMode(schakelaar4, INPUT_PULLUP);
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
Serial.begin(9600);
delay(100);
Serial.println();
Serial.println("*** start shutters ***");
//0 original code
char storedShuttersState[shutters.getStateLength()];
readInEeprom(storedShuttersState, shutters.getStateLength());
shutters
.setOperationHandler(shuttersOperationHandler)
.setWriteStateHandler(shuttersWriteStateHandler)
.restoreState(storedShuttersState)
.setCourseTime(upCourseTime, downCourseTime)
.onLevelReached(onShuttersLevelReached)
.begin()
.setLevel(10);
//shutter 1
char storedShuttersState1[shutters1.getStateLength()];
readInEeprom1(storedShuttersState1, shutters1.getStateLength());
shutters1
.setOperationHandler(shuttersOperationHandler1)
.setWriteStateHandler(shuttersWriteStateHandler1)
.restoreState(storedShuttersState1)
.setCourseTime(upCourseTime1, downCourseTime1)
.onLevelReached(onShuttersLevelReached1)
.begin()
.setLevel(10);
//shutter 2
char storedShuttersState2[shutters2.getStateLength()];
readInEeprom2(storedShuttersState2, shutters2.getStateLength());
shutters2
.setOperationHandler(shuttersOperationHandler2)
.setWriteStateHandler(shuttersWriteStateHandler2)
.restoreState(storedShuttersState2)
.setCourseTime(upCourseTime2, downCourseTime2)
.onLevelReached(onShuttersLevelReached2)
.begin()
.setLevel(10);
//shutter 3
char storedShuttersState3[shutters3.getStateLength()];
readInEeprom3(storedShuttersState3, shutters3.getStateLength());
shutters3
.setOperationHandler(shuttersOperationHandler3)
.setWriteStateHandler(shuttersWriteStateHandler3)
.restoreState(storedShuttersState3)
.setCourseTime(upCourseTime3, downCourseTime3)
.onLevelReached(onShuttersLevelReached3)
.begin()
.setLevel(10);
//shutter 4
char storedShuttersState4[shutters4.getStateLength()];
readInEeprom4(storedShuttersState4, shutters4.getStateLength());
shutters4
.setOperationHandler(shuttersOperationHandler4)
.setWriteStateHandler(shuttersWriteStateHandler4)
.restoreState(storedShuttersState4)
.setCourseTime(upCourseTime4, downCourseTime4)
.onLevelReached(onShuttersLevelReached4)
.begin()
.setLevel(10);
}
void loop() {
shutters.loop();
shutters1.loop();
shutters2.loop();
shutters3.loop();
shutters4.loop();
if (Serial.available() > 0) {
int level = Serial.parseInt();
Serial.print("Goes to level ");
Serial.print(level);
Serial.println(" %");
shutters.setLevel(level);
shutters1.setLevel(level);
shutters2.setLevel(level);
shutters3.setLevel(level);
shutters4.setLevel(level);
}
//on-OFF-on type switch
schakelaar1State = digitalRead(schakelaar1);
schakelaar2State = digitalRead(schakelaar2);
schakelaar3State = digitalRead(schakelaar3);
if (schakelaar1State == LOW) {
shutters.setLevel(0); //MOVE UP
}
else if (schakelaar2State == LOW) {
shutters.setLevel(80); //MOVE to 80%
}
else if (schakelaar3State == LOW) {
shutters.setLevel(100); //MOVE to 100%
}
else {
shutters.stop();
}
} //loop ends here
I want to control 4 shutters independently, so I extended the code (thx to @ludodoucet for pointing me in the right direction). But there is something strange happening and I don't know where to look. Here's what I did: I multiplied most of the code by creating multiple shutter instances. "Shutters" is the original instance, shutters1 etc is the multiplied part. So for the moment I have 5 shutters ;-) When I run the sketch, "shutters" stays at 0%. All other instances works fine. When I comment out all parts involved "shutters", the problem moves to shutters1. All remaining three work just fine. So, there is always a problem with the first instance that is not responding as it should. It seems as if whatever first instance is not responding on setLevel, or at least no more than 1%. I can't get my head around it...