#include "max6675.h"// this file is part of the library. See video for details
#include <LCD_I2C.h>
#include <Wire.h>
#define pwmPin 9
#define controlPin A3
const int SW = 2; //pin2並電阻10K在按鈕同側對側電容1n接G the number of the pushbutton pin
const int compin = 3;
const int timeupin = 12;
const int oilLosePin = 7;
const int oilHeaterPin =11;
const int refVolts=5; //使用 5V 參考電壓的 Arduino 開發板
//int oilLosePin=7; //LED 連接數位接腳 7
int val; //數位值
float volts;
//const int oilLoseVol = 7;
const unsigned long Debounce = 20;
const unsigned long LongPress = 800;
unsigned long ButtonStateChangeTime = 0; // Debounce timer
boolean ButtonWasPressed = false;
boolean Effect = false;
boolean EffectState = false;
//unsigned long oilsensorValue=0;
int Time_second;
int Time_minute;
int Time_hour;
const int baudRate = 9600;
long Time_hour_setting=3600000;
long Time_minute_setting=60000;
long Time_second_setting=1000;
LCD_I2C lcd(0x27, 16, 2);
unsigned long start_time;
int page_counter=1 ; //To move beetwen pages
unsigned long previousMillis = 0;
unsigned long interval = 5000; //Desired wait time 10 seconds
int up = 8; //Up button
int down = 10; //Down button
//---------Storage debounce function-----//
boolean current_up = LOW;
boolean last_up=LOW;
boolean last_down = LOW;
boolean current_down = LOW;
const int pressureInput = A0; //select the analog input pin for the pressure transducer
const int pressureZero = 160; //analog reading of pressure transducer at 0psi
const int pressureMax = 921.6; //analog reading of pressure transducer at 100psi
const int pressuretransducermaxPSI = 174; //psi value of transducer being used
const int pressureInput2 = A1; //select the analog input pin for the pressure transducer
const int pressureZero2 = 102.4; //analog reading of pressure transducer at 0psi
const int pressureMax2 = 921.6; //analog reading of pressure transducer at 100psi
const int pressuretransducermaxPSI2 = 435; //psi value of transducer being used
//const int baudRate = 9600; //constant integer to set the baud rate for serial monitor
const int sensorreadDelay = 250; //constant integer to set the sensor read delay in milliseconds
float pressureValue = 0; //variable to store the value coming from the pressure transducer
float pressureValue2 = 0;
int soPin = 6;// SO=Serial Out
int csPin = 5;// CS = chip select CS pin
int sckPin = 4;// SCK = Serial Clock pin
MAX6675 thermocouple(sckPin, csPin, soPin); //PIN4.5.6
void setup(){
pinMode(SW, INPUT);//PIN2
pinMode(compin, OUTPUT); //PIN3
pinMode(timeupin,INPUT_PULLUP);//PIN12
pinMode(pwmPin,OUTPUT);//PIN9
pinMode(oilLosePin ,OUTPUT);//PIN7
pinMode(oilHeaterPin, OUTPUT); //PIN11
//pinMode(oilLoseVol,INPUT); //PIN7
int timeupin =0;
//float voltage = 0;//int oilLosePin =0;
lcd.begin();
lcd.backlight();
lcd.print("GOOD LUCK"); //|
lcd.setCursor(0,1); //|
lcd.print(" Jung Bill ");
delay( 6000);
digitalWrite(SW,HIGH);
Serial.println("Start");
digitalWrite(compin, LOW);
Serial.begin(baudRate);
digitalWrite(oilLosePin,LOW);
digitalWrite(oilHeaterPin,LOW);
}
//---- De-bouncing function for all buttons----//
boolean debounce(boolean last, int pin)
{
boolean current = digitalRead(pin);
if (last != current)
{
delay(5);
current = digitalRead(pin);
}
return current;
}
void loop() {
int sensorValue = analogRead(A5);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
unsigned long currentTime = millis();
boolean buttonIsPressed = digitalRead(SW) == LOW; // Active LOW
// Check for button state change debounce.
if (buttonIsPressed != ButtonWasPressed &&
currentTime - ButtonStateChangeTime > Debounce) {
// Button state has changed.
ButtonWasPressed = buttonIsPressed;
if (ButtonWasPressed) {
Serial.println("Press");
// digitalWrite(compin, HIGH);
//delay(1);
// digitalWrite(compin, LOW);
}
else {
//Released after 500ms has passed:
if (currentTime - ButtonStateChangeTime >= LongPress) {
Serial.println("Momentary");
// digitalWrite (LED, LOW);
}
if (currentTime - ButtonStateChangeTime < LongPress && Effect == false) {
Serial.println("Start Recycling");
Effect = true;
digitalWrite(compin, HIGH);
}
else {
if (Effect == true) {
Serial.println("Recycling End");
Effect = false;
digitalWrite(compin, LOW);
}
}
}
//Time reset.
ButtonStateChangeTime = currentTime;
}
current_up = debounce(last_up, up); //Debounce for Up button
current_down = debounce(last_down, down); //Debounce for Down button
//Page Up
if (last_up== LOW && current_up == HIGH){
lcd.clear(); //When page is changed, lcd clear to print new page
if(page_counter <6){ //Page counter never higher than 3(total of pages)
page_counter= page_counter +1; //Page up
//delay(500);
}
else{
page_counter= 1; //return to page 1
}
}
last_up = current_up;
//Page Down
if (last_down== LOW && current_down == HIGH){
lcd.clear(); //When page is changed, lcd clear to print new page
if(page_counter >1){ //Page counter never lower than 1 (total of pages)
page_counter= page_counter -1; //Page down
// delay(500);
}
else{
page_counter= 6; //return to page 3
}
}
last_down = current_down;
//------ Switch function to write and show what you want---//
switch (page_counter) {
case 1:{ //Design of home page 1
int timeup= digitalRead(12);
if (timeup == HIGH){
start_time= currentTime ;
// start_time=millis();
Time_hour = start_time / Time_hour_setting; //小時
Time_minute = (start_time % Time_hour_setting) / Time_minute_setting; //分鐘
Time_second = ((start_time % Time_hour_setting) % Time_minute_setting) / Time_second_setting; //秒
// put your main code here, to run repeatedly:
lcd.setCursor(0,0);
lcd.print("Recycling TIME");
lcd.setCursor(3,1);
lcd.print(Time_hour);
lcd.print("Hr ");
lcd.print(Time_minute);
lcd.print("Min");
lcd.print(Time_second);
lcd.print("Sec");
delay(600);
}
}
break;
case 2: { //Design of page 2
pressureValue = analogRead(pressureInput); //reads value from input pin and assigns to variable
pressureValue = ((pressureValue-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero); //conversion equation to convert analog reading to psi
lcd.setCursor(0,0);
lcd.print("LOW ");
lcd .print( pressureValue); //prints value from previous line to serial
lcd.print("Psig"); //prints label to serial
// delay(6000); //delay in milliseconds between read values
pressureValue2 = analogRead(pressureInput2); //reads value from input pin and assigns to variable
pressureValue2 = ((pressureValue2-pressureZero2)pressuretransducermaxPSI2)/(pressureMax2-pressureZero2); //conversion equation to convert analog reading to psi
lcd.setCursor(0,1);
lcd.print("HIGH "); //prints label to serial/
lcd .print( pressureValue2); //prints value from previous line to serial
lcd.print("Psig");
delay(600); //delay in milliseconds between read values
}
break;
case 3: { //Design of page 3
Serial.print("C = ");
Serial.println(thermocouple.readCelsius());
Serial.print("F = ");
Serial.println(thermocouple.readFahrenheit());
//lcd.clear();// clear previous values from screen
lcd.setCursor(1,0);// set cursor at character 0, line 0
lcd.print("");
lcd.setCursor(0,1);// set cursor at character 0, line 1
lcd.print(thermocouple.readCelsius());
lcd.setCursor(5,1);// set cursor at character 9, line 1
lcd.print((char)223);
lcd.setCursor(6,1);// set cursor at character 9, line 1
lcd.print("C");
lcd.setCursor(7,1);// set cursor at character 9, line 1
lcd.print(" ");
lcd.setCursor(8,1);// set cursor at character 9, line 1
lcd.print(thermocouple.readFahrenheit()); // print temperature in ahrenheit
lcd.setCursor(14,1);// set cursor at character 9, line 1
lcd.print((char)223);
lcd.setCursor(15,1);// set cursor at character 9, line 1
lcd.print("F");
// Robojax.com MAX6675 video with LCD1602 20181124
delay(600);
}
break;
case 4: { //Design of page 3
int potValue = analogRead(controlPin);
int pwm =map(potValue, 0,1023, 0, 255);
if (thermocouple.readCelsius()<=10){
pwm = toPWM(35); }
else if (thermocouple.readCelsius()==10.2){
pwm = toPWM(36);
}
else if (thermocouple.readCelsius()==10.5){
pwm = toPWM(37);
}
else if (thermocouple.readCelsius()==10.7){
pwm = toPWM(38);
}
else if (thermocouple.readCelsius()==11){
pwm = toPWM(40);
}
else if (thermocouple.readCelsius()==11.3){
pwm = toPWM(41);
}
else if (thermocouple.readCelsius()==11.5){
pwm = toPWM(42);
}
else if (thermocouple.readCelsius()==11.7){
pwm = toPWM(43);
}
else if (thermocouple.readCelsius()==11.9){
pwm = toPWM(44);
}
else if (thermocouple.readCelsius()==12){
pwm = toPWM(45);
}
else if (thermocouple.readCelsius()==12.1){
pwm = toPWM(46);
}
else if (thermocouple.readCelsius()==12.3){
pwm = toPWM(47);
}
else if (thermocouple.readCelsius()==12.7){
pwm = toPWM(48);
}
else if (thermocouple.readCelsius()==12.6){
pwm = toPWM(49);
}
else if (thermocouple.readCelsius()==12.9){
pwm = toPWM(50);
}
else if (thermocouple.readCelsius()==13 ){
pwm = toPWM(51);
}
else if (thermocouple.readCelsius()==13.1 ){
pwm = toPWM(52);
}
else if (thermocouple.readCelsius()==13.3 ){
pwm = toPWM(53);
}
else if (thermocouple.readCelsius()==13.5 ){
pwm = toPWM(54);
}
else if (thermocouple.readCelsius()==13.7 ){
pwm = toPWM(55);
}
else if (thermocouple.readCelsius()==14.1 ){
pwm = toPWM(56);
}
else if (thermocouple.readCelsius()==14.3){
pwm = toPWM(57);
}
else if (thermocouple.readCelsius()==14.4 ){
pwm = toPWM(58);
}
else if (thermocouple.readCelsius()==14.7 ){
pwm = toPWM(59);
}
else if (thermocouple.readCelsius()==14.9 ){
pwm = toPWM(60);
}
else if (thermocouple.readCelsius()==15.1 ){
pwm = toPWM(61);
}
else if(thermocouple.readCelsius()==15.3){
pwm = toPWM(62);
}
else if (thermocouple.readCelsius()==15.5){
pwm = toPWM(63);
}
else if (thermocouple.readCelsius()==15.7 ){
pwm = toPWM(64);
}else if (thermocouple.readCelsius()==15.9 ){
pwm = toPWM(65);
}else if (thermocouple.readCelsius()==16.1 ){
pwm = toPWM(66);
}else if (thermocouple.readCelsius()==16.3 ){
pwm = toPWM(67);
}else if (thermocouple.readCelsius()==16.7 ){
pwm = toPWM(68);
}else if (thermocouple.readCelsius()==16.8 ){
pwm = toPWM(69);
}
else if (thermocouple.readCelsius()==17.1){
pwm = toPWM(70);
}else if (thermocouple.readCelsius()==17.3){
pwm = toPWM(71);
}else if (thermocouple.readCelsius()==17.7){
pwm = toPWM(72);
}else if (thermocouple.readCelsius()==17.9){
pwm = toPWM(73);
}else if (thermocouple.readCelsius()==17.6){
pwm = toPWM(74);
}
else if(thermocouple.readCelsius()==18.1){
pwm = toPWM(75);
} else if(thermocouple.readCelsius()==18.3){
pwm = toPWM(76);
} else if(thermocouple.readCelsius()==18.7){
pwm = toPWM(77);
} else if(thermocouple.readCelsius()==18.9){
pwm = toPWM(78);
} else if(thermocouple.readCelsius()==18.2){
pwm = toPWM(79);
}
else if (thermocouple.readCelsius()==19.1){
pwm = toPWM(80);
}else if (thermocouple.readCelsius()==19.3){
pwm = toPWM(81);
}else if (thermocouple.readCelsius()==19.7){
pwm = toPWM(82);
}else if (thermocouple.readCelsius()==19.9){
pwm = toPWM(85);
}else if (thermocouple.readCelsius()==19.8){
pwm = toPWM(88);
}
else if (thermocouple.readCelsius()==20){
pwm = toPWM(90);
}else if (thermocouple.readCelsius()==20.1){
pwm = toPWM(91);
}else if (thermocouple.readCelsius()==20.3){
pwm = toPWM(94);
}else if (thermocouple.readCelsius()==20.7){
pwm = toPWM(95);
}else if (thermocouple.readCelsius()==20.9){
pwm = toPWM(98);
}
else if (thermocouple.readCelsius()>=21){
pwm = toPWM(100);
}
analogWrite(pwmPin,pwm);
lcd.setCursor(1,0);
lcd.print("*** FUN PWM ***");
lcd.setCursor(7,1);
// lcd.print(pwm);
lcd.print(pwmToPercent(pwm));
lcd.print("%");
delay(600);
}
break;
case 5: {
if (voltage>=2) {digitalWrite(oilLosePin, HIGH);
Serial.println("oilLosePin NO");
lcd.print(voltage);
lcd.setCursor(0,0);
lcd.print("*** Oil Lose ");
lcd.setCursor(0,1);
lcd.print(" oilLosePin ON ");
delay(600);
} else {
digitalWrite(oilLosePin, LOW);
lcd.setCursor(0,0);
lcd.print(" Oil Lose *** ");
lcd.setCursor(0,1);
lcd.print(" oilLosePin OFF ");
Serial.println("oilLosePin OFF");} //Design of page 3
delay(1000);
}
break;
case 6: {if (thermocouple.readCelsius()<=15.9){
digitalWrite(oilHeaterPin, HIGH);
lcd.setCursor(0,0);
lcd.print("*** Oil Heater ");
lcd.setCursor(7,1);
lcd.print("ON ");
delay(500); } else {
digitalWrite(oilHeaterPin, LOW);
lcd.setCursor(0,0);
lcd.print(" Oil Heater *** ");
lcd.setCursor(7,1);
lcd.print("OFF");
}
}
break;
} //switch end
//-----------Auto scroll function---------------//
unsigned long currentMillis = millis(); //call current millis
// lcd.setCursor(0,1); // Show millis counter status
// Serial.println((currentMillis-previousMillis)/1000);
if (currentMillis - previousMillis > interval) { //If interval is reached, scroll page
previousMillis = currentMillis; //replace previous millis with current millis as new start point
lcd.clear(); //lcd clear if page is changed.
if (page_counter <6){ //Page counter never higher than 3 (total of pages)
page_counter = page_counter +1; //Go to next page
}
else{
page_counter=1; //if counter higher than 3 (last page) return to page 1
}
}
if (digitalRead(8) == HIGH || digitalRead(10)==HIGH){ // Reset millis counter If any button is pressed
previousMillis = currentMillis;
}
}
int toPWM(int v){
return map(v, 0,100,0,255);
}// .
int pwmToPercent(int p)
{
return map(p, 0,255,0,100);
}