Close

Skeleton Code

A project log for Water Lvl Monitor w/ Latching Solenoid Controller

Hacky project, that monitors the water level in a well as it fills.

jesse-farrellJesse Farrell 07/16/2024 at 05:000 Comments

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.

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