Hi guys . Have some questions to ask
My program is currently like this.
Code:
#include <Servo.h>
Servo myservo;
const int inputPin = 3;
const int ledPin1 = 4;
const int ledPin2 = 5;
const int inputPin1= 6;
const int testMode = 7;
int pos = 0;
boolean executeOnce = true;
int hour = 0;
int min = 0;
int sec = 0;
void setup() {
myservo.attach(8);
myservo.write(0);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(inputPin, INPUT);
pinMode(inputPin1, INPUT);
pinMode(testMode, INPUT);
Serial.begin(9600);
}
void loop(){
delay(1000);
sec = sec + 1;
if (digitalRead(testMode) == HIGH && digitalRead(inputPin1) == HIGH) // Off All led . Default Mode 48 hour.
{
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
else if (digitalRead(testMode) == HIGH && digitalRead(inputPin1) == LOW && digitalRead(inputPin) == HIGH) //24 Hour
{
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
}
else if (digitalRead(testMode) == HIGH && digitalRead(inputPin1) == LOW && digitalRead(inputPin) == LOW) // 12 Hour
{
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
}
else if (digitalRead(testMode) == LOW && digitalRead(inputPin) == HIGH) // TestMode
{
digitalWrite(ledPin1, HIGH);
delay(250);
digitalWrite(ledPin1, LOW);
delay(250);
}
if (digitalRead(testMode) == LOW && digitalRead(inputPin) == LOW) // Testmode
{
if(executeOnce)
{
digitalWrite(ledPin2, HIGH);
delay(500);
digitalWrite(ledPin2, LOW);
delay(500);
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
executeOnce = false;
}
}
else if ((digitalRead(testMode) == LOW && digitalRead(inputPin) == LOW) == false) {
executeOnce = true;
}
Serial.print(hour);
Serial.print("Hour");
Serial.print(min);
Serial.print("Min");
Serial.print(sec);
Serial.print("SEC");
}
If i write the program like this , there will be a 1 second delay in everything i do due to the delay(1000).
EG. when i press button for led . it will only light up / turn off after 1 second .
But i added the delay just for the timer so that it will count 1 sec by 1 sec.
If i dont add the delay(1000) , the seconds will be spamming my serial monitor as there is no delay.
Any idea on what can i do about it ? so that the delay is just for the timer. instead of affecting my LEDs/Servo/Pushbuttons.
Another question is that how can i turn my servo 180deg every 24 hour , hold for one minute and turn back to 0deg ?
i'm making a plant watering system so that the servo turn 180deg to let water flow through from the water tank for 1min
after which the servo will turn back to 0deg . and it will constantly do this every 24 hour as long as the 24 hour mode is activated
Thanks