#define clockPin 6 // Clock pin of 74HC595 is connected to Digital pin 10
#define dataPin 8 // Data pin of 74HC595 is connected to Digital pin 9
#define latchPin 4 // Latch pin of 74HC595 is connected to Digital pin 8
#define OPEN_PIN 2
#define CLOSE_PIN 3
#include <math.h>
#include "Condition.h"
//Condition array sizes
const int openConditionSize = 14;
const int closeConditionSize = 14;
volatile Condition openConditions[openConditionSize];
volatile Condition closeConditions[closeConditionSize];
volatile bool button_open_pressed = false;
volatile bool button_close_pressed = false;
volatile bool button_stop_pressed = true;
enum Operation {OPEN, CLOSE, STOP};
Operation op = STOP;
const int idataPin = 9; // Q7 Data 74HC165
const int iclockPin = 10; // CP Clock pin of 74HC165 is connected to Digital
const int ilatchPin = 11; // PL Latch 74HC165
const int numBits = 16; /* Set to 8 * number of shift registers */
//Interrup Open function
void openAction() {
op = OPEN;
}
//Interrup Close function
void closeAction() {
op = CLOSE;
}
//Setup Open Conditions
void setOpenConditions(Condition arr[openConditionSize])
{
// Copied from Close !!
arr[0] = Condition(0B0000001111101000, 0B0000000100000000, false, "OB");
arr[1] = Condition(0B0000001111101001, 0B0000000110001000, false, "OC");
arr[2] = Condition(0B0000001111101001, 0B0000010110001000, false, "OD");
arr[3] = Condition(0B0000001011101001, 0B0000010110000100, false, "OE");
arr[4] = Condition(0B0000001010101101, 0B0000010110000100, false, "OF");
arr[5] = Condition(0B0000001010101101, 0B0000010110000110, false, "OG");
arr[6] = Condition(0B0000001000101101, 0B0000010110010110, false, "OH");
arr[7] = Condition(0B0000001000001111, 0B0000010110010110, false, "OI");
arr[8] = Condition(0B0000001000001111, 0B0001010110010110, false, "OJ");
arr[9] = Condition(0B0000000000001111, 0B0001010110000110, false, "OK");
arr[10] = Condition(0B0000000000010111, 0B0001010110000100, false, "OL");
arr[11] = Condition(0B0000000000110111, 0B0001010110000000, false, "OM");
arr[12] = Condition(0B0000000010110111, 0B0010000100100001, false, "ON");
arr[13] = Condition(0B0000000010110110, 0B0000000000000000, true, "OP");
}
//Setup Close Conditions
void setCloseConditions(Condition arr[closeConditionSize])
{
arr[0] = Condition(0B0000001111101000, 0B0000000100000000, false, "CB");
arr[1] = Condition(0B0000001111101001, 0B0000000110001000, false, "CC");
arr[2] = Condition(0B0000001111101001, 0B0000010110001000, false, "CD");
arr[3] = Condition(0B0000001011101001, 0B0000010110000100, false, "CE");
arr[4] = Condition(0B0000001010101101, 0B0000010110000100, false, "CF");
arr[5] = Condition(0B0000001010101101, 0B0000010110000110, false, "CG");
arr[6] = Condition(0B0000001000101101, 0B0000010110010110, false, "CH");
arr[7] = Condition(0B0000001000001111, 0B0000010110010110, false, "CI");
arr[8] = Condition(0B0000001000001111, 0B0001010110010110, false, "CJ");
arr[9] = Condition(0B0000000000001111, 0B0001010110000110, false, "CK");
arr[10] = Condition(0B0000000000010111, 0B0001010110000100, false, "CL");
arr[11] = Condition(0B0000000000110111, 0B0001010110000000, false, "CM");
arr[12] = Condition(0B0000000010110111, 0B0010000100100001, false, "CN");
arr[13] = Condition(0B0000000010110110, 0B0000000000000000, true, "CP");
}
void setup() { //this function runs once when Arduino turns on
//populate conditions
setOpenConditions(openConditions);
setCloseConditions(closeConditions);
//setup input shift register
pinMode(idataPin, INPUT);
pinMode(iclockPin, OUTPUT); //Clock pin of 74HC595 is connected to Digital
pinMode(ilatchPin, OUTPUT);
//interrupt
pinMode(OPEN_PIN, INPUT);
pinMode(CLOSE_PIN, INPUT);
//attach the Open and Close interrupt
//For Arduino Nano 33 BLE add interrupt for STOP
attachInterrupt(digitalPinToInterrupt(OPEN_PIN), openAction, RISING);
attachInterrupt(digitalPinToInterrupt(CLOSE_PIN), closeAction, RISING);
//set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
Serial.begin(115200); //setup
}
void lightup(uint16_t inPins) {
Serial.print("lightup: ");
Serial.println(inPins, BIN);
digitalWrite(latchPin, LOW); //Begin session
shiftOut(dataPin, clockPin, MSBFIRST, inPins >> 8);
shiftOut(dataPin, clockPin, MSBFIRST, (inPins));
digitalWrite(latchPin, HIGH); //Begin session
}
void upRoutine(Condition Cndtn[]) {
for (int i = 0; i < closeConditionSize; i++)
{
Serial.println(Cndtn[i].inpins, BIN);
delay(100);
}
// Serial.print("BINARY >>>>>>>>>>>> ");
// Serial.println(Cndtn[12].inpins, BIN);
// delay(100);
return;
}
//https://forum.arduino.cc/t/how-to-create-an-array-of-objects-solved/474487/2
void loop() { //loop() - this function runs over and over again
//action setting
switch (op)
{
case OPEN: Serial.println("UP");
upRoutine(openConditions);
break;
case CLOSE: Serial.println("DOWN"); break;
case STOP : Serial.println("STOP"); break;
}
uint16_t inValue = 0x0000;
int booleanCount = 1;
int total = 0;
// Step 1: Sample
digitalWrite(ilatchPin, LOW);
digitalWrite(ilatchPin, HIGH);
// Step 2: Shift
Serial.print("Bits: ");
for (int i = 0; i < numBits; i++) {
int bit = digitalRead(idataPin);
if (bit == HIGH) {
inValue = inValue + round(pow(2, i));
Serial.print("1");
} else {
Serial.print("0");
}
digitalWrite(iclockPin, HIGH); // Shift out the next bit
digitalWrite(iclockPin, LOW);
}
Serial.println();
Serial.print("SerialInPins: ");
Serial.println(inValue, HEX);
//digitalWrite(ilatchPin, HIGH);
Serial.println();
lightup(inValue);
delay(100);
}