Hey guys,
I’m using 16*2 LCD with arduino in my school project. It’s just a dc motor driving a 2nd motor which the last represents the generator and produce about 2v which goes to analogRead and There is 4 LEDs too.
An LCD shows the status of the what happens exactly.
Actually, The first printing to the lcd is works perfectly but after that’s not.
/*
* Programmer: Mahdi Samah
* Purpose: Automatic Generator Starter AGS
* Year: 2015
*/
#include <LiquidCrystal.h>
int x=0;
int flag=1;
int motor=2;
int redLed=3;
int blueLed=4;
int greenLed=5;
int yellowLed=6;
int pwrInput=7;
int gnrRead=A5;
LiquidCrystal lcd(8,9,10,11,12,13);
void setup() {
pinMode(motor,OUTPUT);
pinMode(redLed,OUTPUT);
pinMode(blueLed,OUTPUT);
pinMode(greenLed,OUTPUT);
pinMode(yellowLed,OUTPUT);
pinMode(pwrInput,INPUT);
pinMode(gnrRead,INPUT);
lcd.begin(16,2);
lcd.print("AGS System is ON");
}
void loop() {
digitalWrite(redLed,HIGH); //Led indicates the power to the circuit
while(flag){
if(digitalRead(pwrInput)==HIGH){ //There is a main power
digitalWrite(greenLed,LOW); //Disconnect the load
lcd.clear();
lcd.print("Main Power");
delay(10000);
if(digitalRead(pwrInput)==LOW && digitalRead(blueLed)==HIGH){ //There is no main power & the generator is turned off (CASE: IF THE MAIN POWER GET BACK DURING 10 SEC)
digitalWrite(greenLed,HIGH); //Connect the load
continue;} //Bypass
digitalWrite(blueLed,LOW); //Don't turn off the generator
x=0; //Renew the loop
continue; //Bypass
}
x++; //Increase x value
if(digitalRead(pwrInput)==LOW){ //If the main power is turned off
if(analogRead(gnrRead)<450 && digitalRead(greenLed)==LOW){ //If the generator is still off & No load yet
delay(3000);
lcd.clear();
lcd.print("Starting...");
digitalWrite(motor,HIGH); //Starting the motor to drive the generator
delay(3000);
if(analogRead(gnrRead)>=450 && analogRead(gnrRead)<=640){ //If the generator reading is good (2V)
digitalWrite(motor,LOW); //Stop the motor
digitalWrite(blueLed,HIGH); //The generator is running
lcd.clear();
lcd.print("Running");
delay(3000);
digitalWrite(greenLed,HIGH); //Connect the load
if(digitalRead(blueLed)==HIGH){x=0;continue;} //If the generator is running > renew the loop
}
else if(x>=4){ //(In case which the generator is not running and exhausted 4 times to start and failed)
if(digitalRead(pwrInput)==LOW && digitalRead(redLed)==HIGH){ //If there is no main power and the led of power circuit is ON
flag=0; //Stop the loop
digitalWrite(motor,LOW); //Turn off the motor
lcd.clear();
lcd.print("Starting failure");
digitalWrite(yellowLed,HIGH); //The led indicates to "Starting failure"
break;}
}
else if(digitalRead(blueLed)==LOW){digitalWrite(motor,LOW);} //If the generator is not running > turn off the motor in order to prevent the motor from still turning
}
}
}
}
So, The string “AGS System is ON” is appears with no weirdness. Also “Starting…” appears well but others is not.
I know the problem is in the code not in hardware because I tried with simple example it works well . But I couldn’t find where the is wrong exactly (I’ve repeated the structure of the code in more than form but still…)
Regards,