#include "WaterPort.h"
// Hardware Unique Idenditfier
// Hash created from time and WWV
const String HWUID = "1234";
// Haardware Version
const String WWV = "0.01";
// Number of ports in the device
const byte WATER_PORTS = 2;
// Pins used
// OUTPUT
// DS Serial input
const byte DSP = 13;
// SHCP Serial clock
const byte SCHP = 11;
// STCP Storage (latch) pin
const byte STCP = 12;
// MR Reset (clear), active low. Connect to VCC if not used
const byte MRP = 10;
//Input
// Check water level
const byte WLP = 9;
// pulse width in microseconds
const int PULSE_WIDTH = 10;
// 500ms or HALF_SECOND trigger
bool HALF_SECOND = false;
// Millisecoends between changes to ports
const int FREQ_SET_PORTS = 500;
const int FREQ_CHK_PORTS = 1000;
WaterPort ports[WATER_PORTS];
// TODO Delete when real code in t loop no longer requies it
int pattern = 10101010;
int timerCount = 1;
unsigned long ac = 0xFFFFFF00;
unsigned long check = 5;
void setup() {
Serial.begin( 115200);
pinMode(DSP, OUTPUT); // clock signal, idle LOW
pinMode(SCHP, OUTPUT); // latch (copy input into registers), idle HIGH
pinMode(STCP, OUTPUT);
pinMode(MRP, OUTPUT); // High to send power to level pins
pinMode(WLP, INPUT);
digitalWrite(MRP, HIGH);
// Timer 0A Setup for 1 ms
// https://deepbluembedded.com/arduino-timer-calculator-code-generator/
TCCR0A = 0; // Init Timer0A
TCCR0B = 0; // Init Timer0B
TCCR0B |= B00000011; // Prescaler = 64
OCR0A = 250; // Timer Compare0A Register
TIMSK0 |= B00000010; // Enable Timer COMPA Interrupt
}
void loop() {
//
// Check for server action - Then may be better as an inturupt i2c
//
// Check for next object actions
long curTime = millis();
for (int port = 0; port < WATER_PORTS; port++ ) {
Serial.println((String) "Chek and set port " + port);
ports[port].check_set_port()
}
Serial.println("Break While");
//
// Send actions to Server
//
// Do next object action
//
// Check time between interatins of loop. To see if they are resonable.
if (HALF_SECOND == true){
Serial.print("*");
HALF_SECOND = false;
}
if (time_to_set_ports()){
pattern = ~pattern; // Invert the pattern
set_ports(pattern);
}
}
ISR(TIMER0_COMPA_vect)
{
OCR0A += 250; // Advance The COMPA Register
// Handle The Timer Interrupt
timerCount++;
if (timerCount >= 500) {
HALF_SECOND = true;
timerCount = 1;
}
}
//###########################
boolean time_to_set_ports(){
static long future;
if (millis() > future){
future = millis() + FREQ_SET_PORTS;
return true;
} else {
return false;
}
}
void set_ports(int ports){
digitalWrite(STCP, LOW);
shiftOut(DSP, SCHP, LSBFIRST, ports);
digitalWrite(STCP, HIGH);
}
//#############################
//###########################
// boolean time_to_check_ports(){
// static long future;
// if (millis() > future){
// future = millis() + FREQ_CHK_PORTS;
// return true;
// } else {
// return false;
// }
// }
// void check_ports(){
// for(int i = 0; i < 0; i++){
// ports[i].check_port()
// }
//}
//#############################
// unsigned long future_time(unsigned long timeDelay){
// return millis() + timeDelay;
// }