#include <Wire.h> // Comes with Arduino IDE
#include <LiquidCrystal_I2C.h> //library for I2C LCD 16x2
/* A5 => SCL, A4 => SDA */
/* monorail control program
/* Modified 10/05/2018 by M. Ambar Farid
/*-----( Declare Constants )-----*/
#define R1 2
#define R2 3
#define R3 4
#define R4 5
#define R5 6
#define R6 7
#define R7 8
#define R8 9
#define WD 10
/*-----( Declare objects )-----*/
// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x20, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
void setup() {
// initialization only for one time
//initialization serial
//initialization pin output digital relay and watchdog
pinMode(R1, OUTPUT);
digitalWrite(R1,HIGH);
pinMode(R2, OUTPUT);
digitalWrite(R2,HIGH);
pinMode(R3, OUTPUT);
digitalWrite(R3,HIGH);
pinMode(R4, OUTPUT);
digitalWrite(R4,HIGH);
pinMode(R5, OUTPUT);
digitalWrite(R5,HIGH);
pinMode(R6, OUTPUT);
digitalWrite(R6,HIGH);
pinMode(R7, OUTPUT);
digitalWrite(R7,HIGH);
pinMode(R8, OUTPUT);
digitalWrite(R8,HIGH);
pinMode(WD, OUTPUT); //output for watchdog
lcd.begin(16,2); // initialization lcd 16x2
Serial.begin(9600); //initialization serial
lcd.clear();
lcd.setCursor(0,0);
lcd.print("MONORAIL CONTROL");
lcd.setCursor(3,1);
lcd.print("Powered By:");
delayActive(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("M.A.Farid");
lcd.setCursor(0,1);
}
void loop() {
if(Serial.available()) {
char inChar = (char)Serial.read();
lcd.setCursor(2,1);
lcd.print("STATION ON");
lcd.setCursor(10,1);
switch(inChar) {
case '1':
lcd.write(inChar);
digitalWrite(R1,LOW);
delayActive(400);
digitalWrite(R1,HIGH);
delay(200);
digitalWrite(R8,LOW);
delayActive(400);
digitalWrite(R8,HIGH);
break;
case '2':
lcd.write(inChar);
digitalWrite(R2,LOW);
delayActive(400);
digitalWrite(R2,HIGH);
delay(200);
digitalWrite(R8,LOW);
delayActive(400);
digitalWrite(R8,HIGH);
break;
case '3':
lcd.write(inChar);
digitalWrite(R3,LOW);
delayActive(400);
digitalWrite(R3,HIGH);
delay(200);
digitalWrite(R8,LOW);
delayActive(400);
digitalWrite(R8,HIGH);
break;
case '4':
lcd.write(inChar);
digitalWrite(R4,LOW);
delayActive(400);
digitalWrite(R4,HIGH);
delay(200);
digitalWrite(R8,LOW);
delayActive(400);
digitalWrite(R8,HIGH);
break;
case '5':
lcd.clear();
lcd.setCursor(2,0);
lcd.write("NON STATION !");
digitalWrite(R5,LOW);
lcd.setCursor(2,1);
lcd.write("GET OUT BOX");
delayActive(1000);
lcd.setCursor(2,1);
lcd.write("FROM CONVEYOR");
delayActive(2000);
digitalWrite(R5,HIGH);
break;
case '6':
lcd.write(inChar);
digitalWrite(R6,LOW);
delayActive(400);
digitalWrite(R6,HIGH);
delay(200);
digitalWrite(R8,LOW);
delayActive(400);
digitalWrite(R8,HIGH);
break;
case '7':
lcd.write(inChar);
digitalWrite(R7,LOW);
delayActive(400);
digitalWrite(R7,HIGH);
delay(200);
digitalWrite(R8,LOW);
delayActive(400);
digitalWrite(R8,HIGH);
break;
case '8':
lcd.write(inChar);
digitalWrite(R8,LOW);
delayActive(1000);
digitalWrite(R8,HIGH);
break;
default:
break;
}
lcd.setCursor(0,1);
lcd.print(" ");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SYSTEM READY");
}
watchdogTrigger();
}
void delayActive(int millisecond) {
if(millisecond<100){
watchdogTrigger();
} else {
while (1) {
watchdogTrigger();
if (millisecond > 1000){
delay(900);
millisecond -= 1000;
} else {
delay(millisecond);
break;
}
}
}
}
void watchdogTrigger(){
digitalWrite(WD,HIGH);
delay(50);
digitalWrite(WD,LOW);
delay(50);
}
Comments