One last question before finishing my first project, I hope...
Now everything works well in my code (more or less) and I just want to set PIN 9 (SPin) HIGH when input A2 is HIGH but with delay of 1 second, and when A2 is LOW I want to set it LOW with delay of 0.7 seconds.
Tried that in code below but it doesn't work, it sets output HIGH or LOW right away.
What did I do wrong?
#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 = 9;
int DPG = 10;
int DPD = 11;
int GPin = A2;
int Tipke;
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 displayed text
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 (digitalRead(GPin) == LOW) {
unsigned long TimerA;
TimerA=millis();
if (millis() - Time >= 1000)
{
Time += 1000;
Consumption(); //
}
digitalWrite(VPin, LOW);
digitalWrite(MPin, LOW);
//PROBLEMATIC PART I GUESS
if (TimerA - millis() >= 1000){
digitalWrite(SPin, LOW);
}
}
else {
unsigned long TimerB;
TimerB=millis();
if (millis() - Time >= 1000)
{
Time += 1000;
Working();
}
digitalWrite(VPin, HIGH);
digitalWrite(MPin, HIGH);
if (TimerB - millis() >= 700){
digitalWrite(SPin, HIGH);
}
}
Buttons();
Vrijeme();
}
void Consumption() {
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("STATUS: ON");
lcd.setCursor(0, 1);
lcd.print("Consupt : ");
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("Consupt: ");
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()
{
Tipke = analogRead(A0);
if (Tipke < 50) {
lcd.setCursor(0, 0);
lcd.print(" UP ");
digitalWrite(DPG, HIGH);
}
else if (Tipke < 250) {
lcd.setCursor(0, 0);
lcd.print(" DOWN ");
digitalWrite(DPD, HIGH);
}
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);
}
else {
digitalWrite(DPG, LOW);
digitalWrite(DPD, LOW);
}
}