#include "TM1637.h"
#define SEG_A 1 // -- A --
#define SEG_B 2 // | |
#define SEG_C 4 // F B
#define SEG_D 8 // | |
#define SEG_E 16 // -- G --
#define SEG_F 32 // | |
#define SEG_G 64 // E C
// | |
// -- D --
// washing time
int w_time = 40 * 60; // washing time in minutes
int w_temp = 60; // washing temperature
int r_time = 10 * 60; // rinsing time in minutes
// pins
const int pinPumpDir = 12; // relay - pump direction : wash/flush
const int pinPumpPow = 11; // relay - pump power
const int pinHeater = 10; // relay - heater
const int pinWatInlet = 9; // relay - water inlet valve
const int pinDoor = 8; // switch on door
const int pinModeSel = 7; // switch for mode select: temp/time setting
const int WaterSensorHig = 6; // switch on floater
const int WaterSensorLow = 5; // switch on floater
const int pinLedDio = 4; // I2C pin for TM1637 LCD
const int pinLedClk = 3; // I2C pin for TM1637 LCD
// analog pins
int PinThermistor = 6;
int PinPotMeter = 5;
// variables
int temp;
int potMeter;
int WaterLow = 0;
int WaterHig = 0;
int doorState = 0; // variable for reading the pushbutton status
unsigned long time_curr; // current timestamp
unsigned long seconds;
unsigned long time_cstart; // cycle start timestamp
// motor modes
#define mstop 0
#define mwash 1
#define mflush 2
TM1637 tm1637(pinLedClk, pinLedDio);
// washing state
int w_cycle = 0; // washing cycle varibale walking on below values
#define w_start 0 // just started: wait for door to be closed, flush all remaining water for at least 10 sec
#define w_fill 1 // fill for washing
#define w_wash 2 // wash the dishes + heat to 40 Celsuis
#define w_wflush 3 // flush washing water
#define r_fill 4 // fill for rinsing
#define r_rinse 5 // rinsing + heat to 30 Celsuis
#define r_flush 6 // flush rinsing water
#define r_finshed 7 // flush rinsing water
// ========================================================================================================================
void showLed(uint8_t BitAddr, int8_t DispData)
{
tm1637.start(); //start signal sent to TM1637 from MCU
tm1637.writeByte(ADDR_FIXED);//
tm1637.stop(); //
tm1637.start(); //
tm1637.writeByte(BitAddr | 0xc0); //
tm1637.writeByte(DispData);//
tm1637.stop(); //
}
// the setup function runs once when you press reset or power the board ===================================================
void setup() {
Serial.begin(9600);
Serial.println();
Serial.println("start=============");
pinMode(pinPumpPow, OUTPUT);
pinMode(pinPumpDir, OUTPUT);
pinMode(pinWatInlet, OUTPUT);
pinMode(pinHeater, OUTPUT);
pinMode(WaterSensorLow, INPUT); // init water low sensor
pinMode(WaterSensorHig, INPUT); // init water high sensor
pinMode(pinDoor, INPUT); // init door sensor
tm1637.init(); // init lcd
tm1637.set(6); // set brightness
tm1637.display(0, 8); // show 'booted'
tm1637.display(1, 8);
tm1637.display(2, 8);
tm1637.display(3, 8);
/*
for(int i=0;i<=8;i++)
{
showLed(0x02,1|2);delay(1000);
showLed(0x02,2|64);delay(1000);
showLed(0x02,64|32);delay(1000);
showLed(0x02,32|1);delay(1000);
}
*/
digitalWrite(pinPumpPow, HIGH); // stop flush
digitalWrite(pinPumpDir, HIGH); // stop pump
digitalWrite(pinWatInlet, HIGH); // stop water valve
digitalWrite(pinHeater, HIGH); // stop heater
}
// ========================================================================================================================
void setTemp(int level) {
if (getTemp() < level) {
digitalWrite(pinHeater, LOW); Serial.print(", pinHeater:LOW");
} else {
digitalWrite(pinHeater, HIGH); Serial.print(", pinHeater:HIGH");
}
}
// ========================================================================================================================
int getTemp() {
int Vo;
float R1 = 10000;
float logR2, R2, T, Tc;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
Vo = analogRead(PinThermistor);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
Tc = T - 273.15;
return (int)Tc;
}
// ========================================================================================================================
void manage()
{
while (WaterHig == 1) {
tm1637.display(0, 10); // A
pump(mflush);
delay(100);
WaterHig = digitalRead(WaterSensorHig);
}
while (doorState == 0) {
tm1637.display(0, 11); // B
pump(mstop);
delay(100);
doorState = digitalRead(pinDoor);
}
}
// ========================================================================================================================
void readInput() {
temp = getTemp();
WaterLow = digitalRead(WaterSensorLow);
WaterHig = digitalRead(WaterSensorHig);
doorState = digitalRead(pinDoor);
}
#define w_out 0 //
#define w_normal 1 //
// ========================================================================================================================
void setWaterLevel(int level)
{
switch (level) {
case w_out:
digitalWrite(pinWatInlet, HIGH); // close water inlet valve
while (digitalRead(WaterSensorLow) == 0) {
if (digitalRead(pinDoor) == 0)
pump(mstop);
else
pump(mflush);
showLed(0, SEG_C | SEG_D | SEG_E | SEG_G);
showLed(1, SEG_C | SEG_D | SEG_E);
showLed(2, SEG_D | SEG_E | SEG_F | SEG_G);
showLed(3, 0);
delay(100);
}
delay(10000);
pump(mstop);
break;
case w_normal:
while (digitalRead(WaterSensorLow) == 1) {
if (digitalRead(pinDoor) == 0)
pump(mstop);
digitalWrite(pinWatInlet, LOW); // open water inlet valve
showLed(0, SEG_C);
showLed(1, SEG_E | SEG_G | SEG_C);
showLed(2, 0);
showLed(3, 0);
delay(100);
}
digitalWrite(pinWatInlet, HIGH); // close water inlet valve
break;
}
}
// ========================================================================================================================
void witeLCD()
{
// Show the state of pushbutton on serial monitor
Serial.print(", WaterLow:");
Serial.print(WaterLow);
Serial.print(", WaterHig:");
Serial.print(WaterHig);
Serial.print(", doorState:");
Serial.print(doorState);
Serial.print(", w_cycle:");
Serial.print(w_cycle);
witeLCDTemp(temp);
}
void witeLCDTime(int data)
{
Serial.print("LCDTime:");
Serial.print(data / 60);
Serial.print(":");
Serial.println(data % 60);
if (data > 60) {
tm1637.display(0, data / 60 / 10);
tm1637.display(1, data / 60 % 10);
}
else {
tm1637.display(0, data / 10);
tm1637.display(1, data % 10);
}
}
void witeLCDTemp(int data)
{
Serial.print("LCDTemp:");
Serial.println(data);
tm1637.display(2, data / 10);
tm1637.display(3, data % 10);
}
// the loop function runs over and over again forever =====================================================================
void loop() {
while (digitalRead(WaterSensorHig) == 1) {
tm1637.display(0, 10); // A
pump(mflush);
delay(100);
}
readInput();
witeLCD();
switch (w_cycle) {
case w_start: // 0 - start
setTemp(0); // stop heater
// started with door opened - let user change the washing parameters: time/temperature
if (digitalRead(pinDoor) == 0) {
while (digitalRead(pinDoor) == 0) {
potMeter = analogRead(PinPotMeter) / 10 - 33; //545 - 1020
if (digitalRead(pinModeSel)) {
w_temp = potMeter; // 24..69 celsius
witeLCDTemp(w_temp);
}
else {
w_time = (potMeter * 90) - 1600; // 6..76 minutes
witeLCDTime(w_time);
}
delay(100);
}
} else {
if (digitalRead(pinModeSel)) {
w_temp = 60; // 24..69 celsius
w_time = 50 * 60; // 6..76 minutes
}
else {
w_temp = 40; // 24..69 celsius
w_time = 30 * 60; // 6..76 minutes
}
witeLCDTemp(w_temp);
witeLCDTime(w_time);
delay(5000);
}
Serial.print("temp:");
Serial.print(w_temp);
Serial.print(", time:");
Serial.println(w_time);
setWaterLevel(w_out); // flush all remaning water
w_cycle++;
time_cstart = millis();
break;
case r_fill: // 4 - fill for rinsing
case w_fill: // 1 - fill for washing
setWaterLevel(w_normal);
time_cstart = millis();
w_cycle++;
break;
case w_wash: // 2 - wash
setTemp(w_temp);
pump(mwash);
setWaterLevel(w_normal); // check again water level
seconds = (millis() - time_cstart) / 1000;
witeLCDTime(w_time - seconds);
if (seconds >= w_time)
w_cycle++;
break;
case r_rinse: // 5 - rinsing
setTemp(30);
pump(mwash);
setWaterLevel(w_normal);
seconds = (millis() - time_cstart) / 1000;
witeLCDTime(r_time - seconds);
if (seconds >= r_time)
w_cycle++;
break;
case w_wflush: // 3 - flush washing water
case r_flush: // 6 - flush rinsing water
setTemp(0); // stop the heater
pump(mstop); // stop the pump
delay(1000); // wait for pump to stop
setWaterLevel(w_out);
w_cycle++;
break;
}
Serial.println();
delay(1000); // wait for a second
}
void pump(int mode)
{
if (doorState == 0) { // door is open
Serial.println("pump:door");
digitalWrite(pinPumpPow, HIGH); // stop pump
} else {
switch (mode) {
case 0:
digitalWrite(pinPumpPow, HIGH); // stop pump
break;
case 1: // wash
digitalWrite(pinPumpDir, LOW); // set rotation to wash the dishes
digitalWrite(pinPumpPow, LOW); // start pump
break;
case 2: // flush to drain
digitalWrite(pinPumpDir, HIGH); // set rotation to flush into drain
digitalWrite(pinPumpPow, LOW); // start pump
break;
}
}
}
Comments