//#include <TM1637Display.h>
#include <TM1637.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//#include <SPI.h>
//#include <shiftOut.h>
//#include <Wire.h>
//#include <Adafruit_MCP23017.h>
#include "time.h"
//#include <pqxx>
//Uncomment on hardware
//#include <Wifi.h>
const int numDisplays = 4;
const int numSlots = 12;
const uint8_t clkdp[numDisplays] = {2, 0, 10}; //18,21
const uint8_t diodp[numDisplays] = {3, 1, 19}; //4,20
TM1637 displays[numDisplays];
// Define Sensor PINs for 74hc165
#define SPL 7
#define SCP 9
//#define CE SET TO GND
#define SQ7 6
//Define Pins for 784hc165 for BUTTONS
#define BPL 8
#define BCP 5
//#define CE SET TO GND
#define BQ7 4
// Define Temp Sensor data pin
#define TEMP 18
// Initialize Temp Sensor
OneWire oneWire(TEMP);
//Pass onewire instance to dallas temp library
DallasTemperature tempSensors(&oneWire);
int Temperature;
//int numberOfTS; // Number of temperature devices found
//DeviceAddress tempDeviceAddress;
// Create an instance of the MCP23017 library
//Adafruit_MCP23017 mcp;
struct Data {
//int displayNum; // Equivalent with index of data array PRIMARY KEYs; // Display State Key: Temp= T; Sitting Time = S; Ideal time = I; Default = S
bool edit; // Used to maintain consistency when editing times on the display
char state; //Maintains state of display
float temp; // Temperature nearest to slot, updates frequently
bool on; //Shows if display is currently on
bool hasB; //Shows if slot is full
int time0; // Time that bottle was placed
int time1; //Sitting, updates frequently
int time2; //ideal
Data() : edit(false), state('S'), temp(60), on(false), hasB(false), time1(1111), time2(9999) {}
};
// Note; Display and display previous state variables will not be pushed to DB
Data ww[numSlots];
/** Display/Button Key order
[9,10,11,12]
[5, 6, 7, 8]
[1, 2, 3, 4]
Binary is read in this order: 12-1
EX: 11 is on, 1 is on, rest off: 010000000001
Note: Using TM1637 Library in wokwi, might use TM1637Display.h on hardware
Modes: View: Temp, Sitting Time, Ideal
Display State Key: Temp= T; Sitting Time = S; Ideal time = I;
Data Storage: DB Schema has two tables:
Known Bugs: Buttons and sensors can get stuck in the on position, for buttons this will cause the display to switch modes constantly.
*/
// TODO:
// CLEAN UP CODE
// Setup initialize function that inits and checks db and checks for each slot.
// DB supabase?
void setup() {
//Initializes the console
Serial.begin(115200);
time();
// Initialize each display pins and Data
for(int i=0; i<numDisplays;i++){
// Initalizes Data
// Initialize each output display pin
pinMode(clkdp[i], OUTPUT);
pinMode(diodp[i], OUTPUT);
displays[i].begin(clkdp[i], diodp[i], 4); //displays[i] = TM1637Display(clkPins[i], dioPins[i]);
//displays[i].setBrightness(0x0f);
}
//Serial.println("Console INIT");
// Initialize the sensor shift register pins
pinMode(SPL, OUTPUT);
pinMode(SCP, OUTPUT);
//pinMode(CE, OUTPUT); GND
pinMode(SQ7, INPUT);
//Initialize the button shift register
pinMode(BPL, OUTPUT);
pinMode(BCP, OUTPUT);
//pinMode(CE, OUTPUT); GND
pinMode(BQ7, INPUT);
//pinMode(TEMP,INPUT);
// Start up the library
tempSensors.begin();
// Grab a count of devices on the wire
//numberOfTS = tempSensors.getDeviceCount();
//Serial.print(numberOfTS, DEC);
Serial.println("Setup Done");
}
void loop() {
//byte data[] = {0x06, 0x3F, 0x2D, 0x7D};
// Reads Jan 01 1970 :/
//Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
// Read temperature
tempSensors.requestTemperatures();
float temp = tempSensors.getTempCByIndex(0);
// Update Local Data
Temperature = temp;
// Print the device count for TEMP SENSOR(Not reading properly in wokwi)
// We can use this to read data from both/ multiple sensors on hardware to give more accurate readings per slot
/**
Serial.print("Number of devices found: ");
Serial.println(tempSensors.getDeviceCount());
for(int i=0;i<tempSensors.getDeviceCount(); i++) {
Serial.print("Temperature for device ");
Serial.print(i);
Serial.print(": ");
Serial.println(tempSensors.getTempCByIndex(i));
}
**/
uint64_t sensorStates = readShiftRegisters(SPL, SCP, SQ7, 16);
uint64_t buttonStates = readShiftRegisters(BPL, BCP, BQ7, 40);
//print_binary(sensorStates,16);
//print_binary(buttonStates, 40);
buttonInputCheck(buttonStates, 40);
sensorCheck(sensorStates);
//displays[2].displayInt(1010);
delay(100);
}
/**
void setupMCP23017(int sdaPin, int sclPin, int resetPin) {
// Set up the reset pin
pinMode(resetPin, OUTPUT);
digitalWrite(resetPin, HIGH);
// Begin the I2C interface
Wire.begin(sdaPin, sclPin);
// Begin communication with the MCP23017
mcp.begin();
}
**/
// Prints data from shift register to console, returns a binary array of numbers
void print_binary(uint64_t data, int size) {
uint64_t States[size];
for (int i = (size - 1); i >= 0; i--) {
int bit = bitRead(data, i);
Serial.print(bit);
States[i] = bit;
}
Serial.println();
}
// Reads data bit by bit from shift register
uint64_t readShiftRegisters(int rPL, int rCP, int rQ7, int size) {
uint64_t data = 0;
// Set load pin LOW to load data from parallel inputs
digitalWrite(rPL, LOW);
delayMicroseconds(5);
digitalWrite(rPL, HIGH);
// Shift in data from both shift registers
for (int i = 0; i < size; i++) {
data |= (uint64_t)digitalRead(rQ7) << ((size - 1) - i);
pulseClock(rCP);
}
return data;
}
// Pulses clock in between bit reads
void pulseClock(int rCP) {
digitalWrite(rCP, HIGH);
delayMicroseconds(5);
digitalWrite(rCP, LOW);
}
void printAddress(DeviceAddress deviceAddress) {
for (uint8_t i = 0; i < 8; i++) {
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1
Loading
ds18b20
ds18b20
Loading
ds18b20
ds18b20