GerryS wrote:Might be interesting to see if you could use Zigby and an Arduino shield.
GerryS wrote:Watch your parasitic load.
GerryS wrote:I did a quick search, but don't see any low voltage switches .
// define pins
//inputs
int porchLightSwitch1 = 23; //cabin right
int porchLightSwitch2 = 25; //cabin left
int porchLightSwitch3 = 27; //galley
//pwm outputs
int porchLight = 2;
// system variables
//saved sensor states
int porchLightSwitch1State_old = 0; //cabin right
int porchLightSwitch2State_old = 0; //cabin left
int porchLightSwitch3State_old = 0; //galley
// fresh sensor states
int porchLightSwitch1State = 0; //cabin right
int porchLightSwitch2State = 0; //cabin left
int porchLightSwitch3State = 0; //galley
//percent Brightness = 0 - 100
int porchLightBrightness = 0;
// begin time of ON state (milliseconds). Rolls over to zero in 55 days
unsigned long porchLightTimeOn = 0;
// elapsed time in ON state (seconds)
unsigned long porchLightElapsedTimeOn = 0;
// other
int batteryChargePercent = 100;
const int batteryAmpHourCapacity = 100;
void setup() {
Serial.begin(9600);
// init pins
pinMode(porchLightSwitch1, INPUT);
pinMode(porchLightSwitch2, INPUT);
pinMode(porchLightSwitch3, INPUT);
pinMode(porchLight, OUTPUT);
// get actual switch states
porchLightSwitch1State_old = digitalRead(porchLightSwitch1); //cabin right
porchLightSwitch2State_old = digitalRead(porchLightSwitch2); //cabin left
porchLightSwitch3State_old = digitalRead(porchLightSwitch3); //galley
Serial.print("!");
}
void loop() {
//Serial.print(".");
//get battery info
//getBatteryInfo();
//service porch Light
servicePorchLight();
}
void servicePorchLight() {
// get sensor switch(es) status
//Serial.print("+");
porchLightSwitch1State = digitalRead(porchLightSwitch1); //cabin right
porchLightSwitch2State = digitalRead(porchLightSwitch2); //cabin left
porchLightSwitch3State = digitalRead(porchLightSwitch3); //galley
//Serial.println(porchLightSwitch1State);
// determine if any switch status has changed
// if (porchLightSwitch1State == porchLightSwitch1State_old )
if (porchLightSwitch1State == porchLightSwitch1State_old &&
porchLightSwitch2State == porchLightSwitch2State_old &&
porchLightSwitch3State == porchLightSwitch3State_old)
{
// nothing changed
//Serial.println("nothing changed");
if (porchLightBrightness > 0)
{
// porch Light was on
// update elapsed Time On
porchLightElapsedTimeOn = millis() - porchLightTimeOn;
}
}
else
{
// switch state changed. Which? Don't care.
// update switchState_old
// Serial.println("CHANGED");
porchLightSwitch1State_old = porchLightSwitch1State;
porchLightSwitch2State_old = porchLightSwitch2State;
porchLightSwitch3State_old = porchLightSwitch3State;
if (porchLightBrightness == 0)
{
// porch Light was off, turn on
Serial.println("On");
porchLightBrightness = 100;
digitalWrite(porchLight, HIGH);
porchLightTimeOn = millis();
porchLightElapsedTimeOn = 0;
}
else
{
// porch Light was on, turn off
Serial.println("off");
porchLightBrightness = 0;
digitalWrite(porchLight, LOW);
porchLightTimeOn = 0;
porchLightElapsedTimeOn = 0;
}
}
// deal with battery SOC issues
}
Breytie wrote:How about a "All secure and ready to travel" check and a "something just opened while in motion" alarm using the door, window and hatch switches?
Breytie wrote:Activate the first test when charge power from the TV is applied (engine started, ready to roll) and the alarm if anything changes thereafter.
WoodSmith wrote:It seems like using momentary switches would be easier.
Breytie wrote:... discrete components built my own APPLE II rip-off from scratch.
// define pins
/* switched loads
* 0 = porch light
* 1 = cabin light
* 2 = galley work light
* 3 = galley red light
* 4 = running lights
* 5 = backup lights
* 6 = water pump
****** 7 = USB charging port
*/
/* switch assignmenbts
* 0 = porch cabin left
* 1 = porch cabin right
* 2 = porch galley
* 3 = cabin left
* 4 = cabin right
* 5 = galley work
* 6 = galley red
* 7 = backup lights
* 8 = running lights
* 9 = water pump
*/
#define DEFswitchCount 10
#define DEFswitchedLoadCount 7
int switchCount = DEFswitchCount;
int switchPin[DEFswitchCount] = {23, 25, 27, 29, 31, 33, 35, 37, 39, 41};
int switchCountByLoad[DEFswitchedLoadCount] = {3, 2, 1, 1, 1, 1, 1};
// multiplr switches per load
int switch1PinIndex[DEFswitchedLoadCount] = {1, 4, 6, 7, 8, 9, 10};
int switch2PinIndex[DEFswitchedLoadCount] = {2, 5, 0, 0, 0, 0, 0};
int switch3PinIndex[DEFswitchedLoadCount] = {3, 0, 0, 0, 0, 0, 0};
// Switch properties
// New/old switch on-off states
int switchNewState[DEFswitchCount] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int switchOldState[DEFswitchCount] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
// ***** loads *****
int switchedLoadCount = DEFswitchedLoadCount;
int loadPin[DEFswitchedLoadCount] = {2, 3, 4, 5, 6, 7, 8};
// PercentOn at load shedding level
int loadPercentOn[DEFswitchedLoadCount] = {0, 0, 0, 0, 0, 0, 0}; // percent on now
// power level to run at by shedding level, 255 = 100%
int loadPercentOn1[DEFswitchedLoadCount] = {255, 255, 255, 127, 255, 255, 255}; // percent on to use at shed level 1.
int loadPercentOn2[DEFswitchedLoadCount] = {127, 127, 127, 127, 127, 127, 0}; // shed level 2
int loadPercentOn3[DEFswitchedLoadCount] = {32, 63, 63, 63, 63, 63, 0}; // shed level 3
unsigned long loadTimeOn[DEFswitchedLoadCount] = {0, 0, 0, 0, 0, 0, 0}; // device turned on at millis()
unsigned long loadElapsedTimeOn[DEFswitchedLoadCount] = {0, 0, 0, 0, 0, 0}; //calculated ET
int loadShedLevel[DEFswitchedLoadCount] = {1, 1, 1, 1, 1, 1, 1}; // current load shed level by load
// time at shedding level, millis
unsigned long loadShedTimeLevel1[DEFswitchedLoadCount] = {5000, 5000, 5000, 5000, 5000, 5000, 300000};
unsigned long loadShedTimeLevel2[DEFswitchedLoadCount] = {5000, 5000, 5000, 5000, 5000, 5000, 0};
unsigned long loadShedTimeLevel3[DEFswitchedLoadCount] = {5000, 5000, 5000, 5000, 5000, 5000, 0};
//======================================================================
void setup() {
Serial.begin(9600);
// init sensor switch assignments
for (int i = 0; i < switchCount; i++) {
pinMode( switchPin[i], INPUT);
}
// init load output pin assignments
for (int i = 0; i < switchedLoadCount; i++) {
pinMode( loadPin[i], OUTPUT);
}
// init switch states
// this allows any switch configuration at startup and everything's off
for ( int i = 0; i < switchCount; i++) {
switchOldState[i] = digitalRead(switchPin[i]);
switchNewState[i] = switchOldState[i];
}
// init battery info
// init real time clock
}
//=====================================================================
void loop() {
//Serial.write("!");
// read status of all switches
for ( int i = 0; i < switchCount; i++) {
switchNewState[i] = digitalRead(switchPin[i]);
}
// service Loads with physical switches
serviceSwitchedLoads();
}
//=====================================================================
void serviceSwitchedLoads() {
//Serial.print(".");
int switchIndex1;
int switchIndex2;
int switchIndex3;
for ( int i = 0; i < switchedLoadCount; i++) {
switch (switchCountByLoad[i]) {
case 3:
// load has 3 switches, get them
//Serial.println("Case 3");
switchIndex1 = switch1PinIndex[i];
switchIndex2 = switch2PinIndex[i];
switchIndex3 = switch3PinIndex[i];
// Serial.print(switchIndex1);
// Serial.print(switchIndex2);
// Serial.print(switchIndex3);
//compare olds & news
if (switchOldState[switchIndex1] == switchNewState[switchIndex1] &&
switchOldState[switchIndex2] == switchNewState[switchIndex2] &&
switchOldState[switchIndex3] == switchNewState[switchIndex3])
{
// no change, continue
//Serial.println("no change");
updateElapsedTime(i);
}
else
{
// switch state changed. Which? Don't care.
// update switchState_old
//Serial.println("change");
switchOldState[switchIndex1] = switchNewState[switchIndex1];
switchOldState[switchIndex2] = switchNewState[switchIndex2];
switchOldState[switchIndex3] = switchNewState[switchIndex3];
//----------------------
if (loadPercentOn[i] > 0)
{
// load was on, turn off
turnOffLoad(i);
}
else
{
// was off, turn on
turnOnLoad(i);
}
}
//Serial.print(switchOldState[switchIndex1]);
//Serial.print(switchOldState[switchIndex2]);
//Serial.println(switchOldState[switchIndex3]);
//===============================
break;
case 2:
// load has 2 switches
//Serial.println("Case 2");
break;
case 1:
// load has only one switch
//Serial.println("Case 1");
break;
default:
// error
Serial.println("***ERROR*** Switch Case #1 defaulted in serviceSwitchedLoads.");
break;
}
//delay(1000);
}
}
void updateElapsedTime(int j)
{ if (loadPercentOn[j] > 0) {
// load is on
loadElapsedTimeOn[j] = millis() - loadTimeOn[j];
Serial.print("ET: ");
Serial.println(loadElapsedTimeOn[j]);
setLoadShedLevel(j);
}
}
void setLoadShedLevel(int k)
{
if (loadShedLevel[k] == 3 && loadShedTimeLevel1[k] + loadShedTimeLevel2[k] + loadShedTimeLevel3[k] < loadElapsedTimeOn[k])
{
Serial.print("level 3 exhausted, time to turn off");
turnOffLoad(k);
}
else if (loadShedLevel[k] == 2 && loadShedTimeLevel1[k] + loadShedTimeLevel2[k] < loadElapsedTimeOn[k])
{
Serial.println("level 2 exhausted, go to L3");
loadShedLevel[k] = 3;
analogWrite(loadPin[k], loadPercentOn3[k]);
}
else if (loadShedLevel[k] == 1 && loadShedTimeLevel1[k] < loadElapsedTimeOn[k])
{
Serial.println("level 1 exhausted, go to L2");
loadShedLevel[k] = 2;
analogWrite(loadPin[k], loadPercentOn2[k]);
}
else
{
// nothing to do
}
}
void turnOffLoad(int j) {
//Serial.println("turn off ");
//delay(2000);
loadPercentOn[j] = 0;
analogWrite(loadPin[j], 0);
loadTimeOn[j] = 0;
loadElapsedTimeOn[j] = 0;
loadShedLevel[j] = 1;
}
void turnOnLoad(int j) {
//Serial.println("was off");
loadPercentOn[j] = loadPercentOn1[j];
digitalWrite(loadPin[j], loadPercentOn[j]);
loadTimeOn[j] = millis();
loadElapsedTimeOn[j] = 0;
}
Users browsing this forum: No registered users and 2 guests