#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int m_dly,m_rot,mode,m,sm,m_speed;
long sys_dly,rot;
bool st,m_st,m_dir,dis;
void setup() {
lcd.init();
lcd.backlight();
////////////////// tampilan awal LCD ////////////////////
lcd.setCursor(0, 0);
lcd.print(" MESIN SABLON ");
lcd.setCursor(0, 1);
lcd.print(" Semi Otomatis ");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" By: Dudung ");
lcd.setCursor(0, 1);
lcd.print(" Elecrtonic ");
delay(3000);
/////// pemetaan PIN arduino //////////
pinMode(2, INPUT_PULLUP); // tombol OK/MENU
pinMode(3, INPUT_PULLUP); // tombol Arah putaran /UP
pinMode(4, INPUT_PULLUP); // tombol Start /DOWN
//pinMode(5, INPUT_PULLUP); // Limit Switch 1
//pinMode(6, INPUT_PULLUP); // Limit Switch 2
pinMode(8,OUTPUT); // DIR
pinMode(9,OUTPUT); // STEP
///////////// eeprom,penyimpan data ///////////
mode=EEPROM.read(0);
if(mode>2)mode=0; // Jika MODE lebih dr 2 maka mode kembali ke 0
m_speed =EEPROM.read(1); // mode Speed membaca epprom 1
m_speed =m_speed+EEPROM.read(2)*100; // mode speed d tamah membaca epprom 2 di x 100
m_speed =m_speed+EEPROM.read(3)*10000; // mode speed d tamah membaca epprom 3 di x 1000
if(m_speed>10000)m_speed=10; // Jika mode speed lebih 10000 maka mode kembali ke speed 10
m_rot =EEPROM.read(4); // membaca epprom 4 mode rotasi
m_rot =m_rot+EEPROM.read(5)*100; // mode rotasi tambah baca epprom 5 di x 100
m_rot =m_rot+EEPROM.read(6)*10000; // mode rotasi tambah baca epprom 6 di x 10000
if(m_rot>10000)m_rot=0; //jika mode rotasi lebih dr 10000 maka kembali ke rotasi 0
}
void loop() {
rot=m_rot*200;
if (digitalRead(2)&digitalRead(3)&digitalRead(4))st=m_dly = 0;
if (!digitalRead(2)&!st){
if(m>0&m<4)sm++;
m_dly++;
if(m_dly>50){
m_dly=0;
m=1;
st=1;
}
if(m>0)st=1;
if(m==4)m=0;
if(sm>1&m==1){sm=0;
EEPROM.update(0, mode);
}
if(sm>3&m==2){sm=0;
EEPROM.update(1, m_speed%100);
EEPROM.update(2, (m_speed/100)%100);
EEPROM.update(3, (m_speed/10000)%100);
}
if(sm>3&m==3){sm=0;
EEPROM.update(4, m_rot%100);
EEPROM.update(5, (m_rot/100)%100);
EEPROM.update(6, (m_rot/10000)%100);
}
}
//////////////// Pembacaan tombol arah putaran / UP //////////////
if (!digitalRead(3)&!st){
st=1;
if(m==0)dis=1;
if(m==0){
m_dir=!m_dir;
}
if(m!=0){
if(sm==0)m++;
if(m==1&sm==1)mode++;
if(mode>2)mode=0;
if(m==2&sm==1)m_speed =m_speed +10;
if(m==2&sm==2)m_speed =m_speed +100;
if(m==2&sm==3)m_speed =m_speed +1000;
if(m_speed>10000)m_speed=10;
if(m==3&sm==1)m_rot =m_rot +1;
if(m==3&sm==2)m_rot =m_rot +10;
if(m==3&sm==3)m_rot =m_rot +100;
if(m==3&sm==4)m_rot =m_rot +1000;
if(m_rot>10000)m_rot=1;
if(m>4)m=1;}
}
//////////////// Pembacaan tombol Start/Down //////////////
if (!digitalRead(4)&!st){
st=1;
if(m==0)dis=1;
if(m==0){
m_st=!m_st;
sys_dly=0;
}
if(m!=0){
if(sm==0)m--;
if(m==1&sm==1)mode--;
if(mode<0)mode=3;
if(m==2&sm==1)m_speed =m_speed -10;
if(m==2&sm==2)m_speed =m_speed -100;
if(m==2&sm==3)m_speed =m_speed -1000;
if(m_speed<10)m_speed=10000;
if(m==3&sm==1)m_rot =m_rot -1;
if(m==3&sm==2)m_rot =m_rot -10;
if(m==3&sm==3)m_rot =m_rot -100;
if(m==3&sm==4)m_rot =m_rot -1000;
if(m_rot<0)m_rot=10000;
if(m<1)m=4;}
}
/////////////////// Tampilan LCD utama //////////////
if((m==0&!m_st)|dis){
dis=0;
lcd.setCursor(0, 0);
lcd.print("Mode:");
lcd.print(mode);
lcd.print(" Rot:");
if(m_rot<1|mode==0)lcd.print("OFF");
else lcd.print(m_rot);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Sablon:");
if(m_st)lcd.print("ON ");
else lcd.print("OFF");
lcd.print(" Dir:");
if(m_dir)lcd.print("FW");
else lcd.print("RW");
digitalWrite(8,0);
}
/////////////////// data mode /////////////////////
if(m_st){
if(mode==0){
digitalWrite(8,m_dir);
digitalWrite(9,0);
delayMicroseconds(10400-m_speed);
digitalWrite(9,1);
delayMicroseconds(10400-m_speed);}
if(mode>0&sys_dly<rot){
sys_dly++;
digitalWrite(8,m_dir);
digitalWrite(9,0);
delayMicroseconds(10400-m_speed);
digitalWrite(9,1);
delayMicroseconds(10400-m_speed);
}else if(mode==1) m_st=0;
else if(mode==2) {m_st=0;m_dir=!m_dir; }
}
////////////// menu MODE ////////////////
if(m==1){
lcd.setCursor(0, 0);
lcd.print(" Set Mode ");
lcd.setCursor(0, 1);
if(sm==0) lcd.print("[--------------]");
else{ lcd.print(" Mode:");
lcd.print(mode);
lcd.print(" ");}
}
////////////// menu SPEED ////////////////
if(m==2){
lcd.setCursor(0, 0);
lcd.print(" Set Speed ");
lcd.setCursor(0, 1);
if(sm==0) lcd.print("[--------------]");
else{ lcd.print(" Speed:");
lcd.print(m_speed);
lcd.print(" ");}}
////////////// menu ROTASI ////////////////
if(m==3){
lcd.setCursor(0, 0);
lcd.print(" Set Rotations ");
lcd.setCursor(0, 1);
if(sm==0) lcd.print("[--------------]");
else{ lcd.print(" Rot:");
lcd.print(m_rot);
lcd.print(" ");}
}
////////////// menu KELUAR ////////////////
if(m==4){
lcd.setCursor(0, 0);
lcd.print(" Exit ");
lcd.setCursor(0, 1);
lcd.print(" SAVE DATA ");
}
}