Hello.
Problem again. I need to enable output 12 (later I'll add more of them) and put custom message on LCD only while input A2 is HIGH. But when I try to do that with following code first my buttons on LCD don't react any more (I'm using Microbot MR007-005.1 LCD Shield), and output doesn't come back to LOW right away, it takes second or two... I used "while" to achieve desired function. Where did I make mistake?
BTW buttons and everything else worked before i added "while" and I need to have Consumption() displayed almost all the time (not while displaying UP time)...
CODE BEFORE ADDING WHILE
#include <LiquidCrystal.h>
#include <StopWatch.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
StopWatch sw_secs(StopWatch::SECONDS);
int VPin = 12;
int MPin = 8;
int SPin = 7;
int DPG = 4;
int DPD = 2;
int GPin = A2;
void setup() {
//Welcome message
lcd.begin(16, 2);
lcd.setCursor(4, 0);
lcd.print("SOMETHING");
delay(1000);
lcd.setCursor(5, 1);
lcd.print("SOMETHING");
delay(2000);
//My way to blink display
lcd.clear();
delay(500);
lcd.setCursor(5, 1);
lcd.print("SOMETHING");
lcd.setCursor(4, 0);
lcd.print("SOMETHING");
delay(500);
lcd.clear();
delay(500);
lcd.setCursor(5, 1);
lcd.print("SOMETHING");
lcd.setCursor(4, 0);
lcd.print("SOMETHING");
delay(500);
pinMode(VPin, OUTPUT);
pinMode(MPin, OUTPUT);
pinMode(SPin, OUTPUT);
pinMode(DPG, OUTPUT);
pinMode(DPD, OUTPUT);
pinMode(GPin, INPUT);
lcd.clear();
sw_secs.start();
}
void loop()
{
static unsigned long Time;
if (millis() - Time >= 1000)
{
Time += 1000;
Consumption();
}
Buttons();
Vrijeme();
}
void Consumption() {
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("STATUS: ON");
lcd.setCursor(0, 1);
lcd.print("Consumption:");
lcd.setCursor(12, 1);
lcd.print("kg/h");
int Potenciometar = A1;
int Pot1 = 0;
int Pot2 = 0;
Pot1 = analogRead(A1) / 10;
Pot2 = Pot1 / 1.023;
lcd.setCursor(9, 1);
lcd.print(Pot2);
}
void Vrijeme() {
}
void Buttons()
{
int Tipke;
Tipke = analogRead(0);
if (Tipke < 50) {
Consumption;
lcd.setCursor(0, 0);
lcd.print("UP ");
}
else if (Tipke < 250) {
Consumption;
lcd.setCursor(0, 0);
lcd.print("DOWN ");
}
else if (Tipke < 450) {
lcd.print("");
lcd.setCursor(0, 0);
lcd.print("UP Time: ");
int h, m, s;
s = sw_secs.elapsed();
m = s / 60;
h = s / 3600;
s = s - m * 60;
m = m - h * 60;
lcd.setCursor(0, 1);
lcd.print(h);
lcd.setCursor(1, 1);
lcd.print(" hrs ");
lcd.setCursor(6, 1);
lcd.print(m);
lcd.setCursor(8, 1);
lcd.print("min");
lcd.setCursor(9, 1);
delay(3000);
}
}
CODE AFTER WHILE
#include <LiquidCrystal.h>
#include <StopWatch.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
StopWatch sw_secs(StopWatch::SECONDS);
int VPin = 12;
int MPin = 8;
int SPin = 7;
int DPG = 4;
int DPD = 2;
int GPin = A2;
void setup() {
//Welcome message
lcd.begin(16, 2);
lcd.setCursor(4, 0);
lcd.print("SOMETHING");
delay(1000);
lcd.setCursor(5, 1);
lcd.print("SOMETHING");
delay(2000);
//My way to blink display
lcd.clear();
delay(500);
lcd.setCursor(5, 1);
lcd.print("SOMETHING");
lcd.setCursor(4, 0);
lcd.print("SOMETHING");
delay(500);
lcd.clear();
delay(500);
lcd.setCursor(5, 1);
lcd.print("SOMETHING");
lcd.setCursor(4, 0);
lcd.print("SOMETHING");
delay(500);
pinMode(VPin, OUTPUT);
pinMode(MPin, OUTPUT);
pinMode(SPin, OUTPUT);
pinMode(DPG, OUTPUT);
pinMode(DPD, OUTPUT);
pinMode(GPin, INPUT);
lcd.clear();
sw_secs.start();
}
void loop()
{
while (digitalRead(GPin) == LOW) {
static unsigned long Time;
if (millis() - Time >= 1000)
{
Time += 1000;
Consumption();
}}
while (digitalRead(GPin) == HIGH); {
static unsigned long Time;
if (millis() - Time >= 1000)
{
Time += 1000;
Working();
}
digitalWrite(VPin, HIGH);
}
Buttons();
Vrijeme();
}
void Consumption() {
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("STATUS: ON");
lcd.setCursor(0, 1);
lcd.print("Consumption:");
lcd.setCursor(12, 1);
lcd.print("kg/h");
int Potenciometar = A1;
int Pot1 = 0;
int Pot2 = 0;
Pot1 = analogRead(A1) / 10;
Pot2 = Pot1 / 1.023;
lcd.setCursor(9, 1);
lcd.print(Pot2);
}
void Vrijeme() {
}
void Working() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("STATUS: Working");
lcd.setCursor(0, 1);
lcd.print("Consumption:");
lcd.setCursor(12, 1);
lcd.print("kg/h");
int Potenciometar = A1;
int Pot1 = 0;
int Pot2 = 0;
Pot1 = analogRead(A1) / 10;
Pot2 = Pot1 / 1.023;
lcd.setCursor(9, 1);
lcd.print(Pot2);
}
void Buttons()
{
int Tipke;
Tipke = analogRead(0);
if (Tipke < 50) {
Consumption;
lcd.setCursor(0, 0);
lcd.print("UP ");
}
else if (Tipke < 250) {
Consumption;
lcd.setCursor(0, 0);
lcd.print("DOWN ");
}
else if (Tipke < 450) {
lcd.print("");
lcd.setCursor(0, 0);
lcd.print("UP Time: ");
int h, m, s;
s = sw_secs.elapsed();
m = s / 60;
h = s / 3600;
s = s - m * 60;
m = m - h * 60;
lcd.setCursor(0, 1);
lcd.print(h);
lcd.setCursor(1, 1);
lcd.print(" hrs ");
lcd.setCursor(6, 1);
lcd.print(m);
lcd.setCursor(8, 1);
lcd.print("min");
lcd.setCursor(9, 1);
delay(3000);
} }