//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <LiquidCrystal_I2C.h>
#include <TimeLib.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
time_t tim=now(); time_t cyctime=now();int settime=30;
void setup()
{
lcd.init();
// initialize the lcd
// Print a message to the LCD.
lcd.backlight();
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);pinMode(4, INPUT_PULLUP);pinMode(5, INPUT_PULLUP);
}
int edge1=0;
int process=0, membit[9], count=0;
void loop()
{
int up=digitalRead(1), down=digitalRead(0);
int start=digitalRead(4), stop=digitalRead(5);
//menu
if(up==false && membit[0]==0)
{
count=count+1;
lcd.print(count);
membit[0]=1;
}
if(up==true)
{
membit[0]=0;
}
if(down==false && membit[1]==0)
{
count=count-1;
lcd.print(count);
membit[1]=1;
}
if(down==true)
{
membit[1]=0;
}
//menu
//process
if(count==0)
{
lcd.setCursor(7,0);
lcd.print("CYCLE");
lcd.setCursor(0,1);
lcd.print("SET TIME:");
lcd.setCursor(14,1);
lcd.print(settime);
lcd.setCursor(0,2);
lcd.print("ELAP TIME:");
lcd.setCursor(14,2);
lcd.print(now()-cyctime);
lcd.setCursor(0,3);
if(process==true)
{
lcd.print("RUNNING");
}
else
{
lcd.print("STOP");
}
if(start==false)
{
process=true;
tim=now();
cyctime=now();
}
if(stop==false)
{
process=false;
}
if (process==true)
{
digitalWrite(8, HIGH);
cycle(5,2,3);
}
else
{
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
}
}
lcd.setCursor(0,0);
//process
//set time
if(count==2)
{
if(start==false && membit[2]==0)
{
settime=settime+1;
membit[2]=1;
}
if(start==true)
{
membit[2]=0;
}
if(stop==false && membit[3]==0)
{
settime=settime-1;
lcd.print(count);
membit[3]=1;
}
if(stop==true)
{
membit[3]=0;
}
lcd.setCursor(0,1);
lcd.print(" ");
lcd.print(settime);
lcd.print(" ");
}
}
void cycle(int fwd, int pause, int rev)
{
int total1, total2, total3;
total1=fwd+pause;
total2=fwd+pause+rev;
total3=fwd+pause*2+rev;
now()<fwd+tim?digitalWrite(6, HIGH):digitalWrite(6, LOW);
(now()>=total1+tim && now()<total2+tim)?digitalWrite(7, HIGH):digitalWrite(7, LOW);
now()>=total3+tim?tim=now():0;
now()>=cyctime+settime?process=0:0;
}