I have the hardware figured out and breadboarded, next thing to flesh out is the FW. It’s a VERY simple program. Here’s 4 main basic states.
- Measure Battery Voltage
- Poll water gauge
- To save on battery, I’m going to “poll” the float switch (no pullups!)
- Send signal on COM and look for signal on either of the two remaining wires
- Set Solenoid
- Short 30ms pulse
- Polarity of pulse dictates whether solenoid opens or closes
- Sleep & Repeat
- Go to deep deep sleep
- If voltage is too low we might decide to shut off the solenoid as a preventative measure… we don’t want to risk losing power while the solenoid is open!
- We could also monitor battery drain to gauge faults in the system
I wanted the main data structure to be circular. So, I made a linked list that sort of feeds into itself… this way I don’t have to know the exact location of any data node, and I navigate the structure using node->next or node->last.
This way I (1) don’t have to keep track of which element I’m on and (2) I get to retain some history of events that I’ve logged. I suspect there’s a less obtuse way of storing this data, but for now this will work (I’m just a HW guy after all!).
Here’s the main data structure.
struct DataNode{
//Debug Data
bool Init_FLAG = false; // If initizlized is false, data is potentially NULL
uint8_t ArrayElem; // Defines which element of the array this is
// Data
uint16_t BatteryLevel; // Measured battery voltage [RAW]
SOLENOID SolenoidState = UNDEFINED; // State that was written to the solenoid
// Pointers
struct DataNode* last; // Pointer to the lastelement
struct DataNode* next; // Pointer to the nextelement
};
I have to do some initialization leg work to link the “last” and “next” pointers as shown.
// ============ Variables ============
static struct DataNode HistoricData[8]; //MUST BE SIZE=8
struct DataNode* DataPtr = &HistoricData[0];
uint8_t i = 0;
// Initialize HistoricData[]
for(i = 1; i<8; i++){
HistoricData[i].last = &HistoricData[i-1];
HistoricData[i].ArrayElem = i;
}for(i = 0; i<7; i++){
HistoricData[i].next = &HistoricData[i+1];
}
HistoricData[0].last = &HistoricData[7];
HistoricData[7].next = &HistoricData[0];
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.