Hello I'm new in Arduino IDE how can i use my water pump for every hour my water run in 30 mins on and off in 20mins i dont know how to start a program i watch many videos but i failed to programming it... I hope you will help me
const int LEDpin = 3;
unsigned long offDuration = 20000;// OFF time for LED
unsigned long onDuration = 30000;// ON time for LED
int LEDState =HIGH;// initial state of LED
long rememberTime=0;// this is used by the code
void setup()
{
pinMode(LEDpin,OUTPUT);// define LEDpin as output
digitalWrite(LEDpin,LEDState);// set initial state
}
void loop() {
if( LEDState ==HIGH )
{
if( (millis()- rememberTime) >= onDuration){
LEDState = LOW;// change the state of LED
rememberTime=millis();// remember Current millis() time
}
}
else
{
if( (millis()- rememberTime) >= offDuration){
LEDState =HIGH;// change the state of LED
rememberTime=millis();// remember Current millis() time
}
}
First, please enclose the code inside "code" tag (edit your latest post, then select the code and press the "</>" button).
Second, you can't change the LED pin state this way, you need to call digitalWrite(LEDpin, LEDstate) each time you need to change it.
Third, you don't need a variable to keep LED state, you can read it using digitalRead(LEDpin).
It means you can start from here (you still need to go on expanding your code...):
const int LEDpin = 3;
unsigned long offDuration = 20000;// OFF time for LED
unsigned long onDuration = 30000;// ON time for LED
long rememberTime=0;// this is used by the code
void setup()
{
pinMode(LEDpin,OUTPUT);// define LEDpin as output
digitalWrite(LEDpin,HIGH);// set initial state
// You also need this:
rememberTime=millis();
}
void loop() {
if( digitalRead(LEDpin)==HIGH )
{
if( ( millis()- rememberTime) >= onDuration ) {
digitalWrite(LEDpin, LOW);// change the state of LED
rememberTime=millis();// remember Current millis() time
}
}
else
{
if( (millis()- rememberTime) >= offDuration ) {
digitalWrite(LEDpin, HIGH);// change the state of LED
rememberTime=millis();// remember Current millis() time
}
}
}
const int LEDpin = 3;
unsigned long offDuration = 20000;// OFF time for LED
unsigned long onDuration = 30000;// ON time for LED
int LEDState = HIGH; // initial state of LED
long rememberTime = 0; // this is used by the code
void setup()
{
pinMode(LEDpin, OUTPUT); // define LEDpin as output
digitalWrite(LEDpin, LEDState); // set initial state
}
void loop() {
if ( LEDState == HIGH )
{
if ( (millis() - rememberTime) >= onDuration) {
LEDState = LOW;// change the state of LED
rememberTime = millis(); // remember Current millis() time
}
}
else
{
if ( (millis() - rememberTime) >= offDuration) {
LEDState = HIGH; // change the state of ALED
rememberTime = millis(); // remember Current millis() time
}
}
}
const int LEDpin = 3;
unsigned long offDuration = 20000;// OFF time for LED
unsigned long onDuration = 30000;// ON time for LED
int LEDState = HIGH; // initial state of LED
unsigned long previousOnTime = millis();
unsigned long previousOffTime = millis();
bool Tick = True; //Tick==true led on
void setup()
{
pinMode(LEDpin, OUTPUT); // define LEDpin as output
digitalWrite(LEDpin, LEDState); // set initial state
}
////
void loop()
{
if ( Tick )
{
digitialWrite( LEDpin, HIGH );
if ( (millis() - previousOnTime >= onDuration)
{
previousOFFTime = millis();
Tick = !Tick;
}
}
else
{
digitialWrite( LEDpin, LOW );
if ( (millis() - previousOFFTime) >= offDuration) {
previousOnTime = millis();
Tick = !Tick;
}
}
}
Check this small example using an array for the controlling of the timer.
const int PumpPin = 8;
constexpr unsigned long OffDuration = 20000;// OFF time for Pump
constexpr unsigned long OnDuration = 30000;// ON time for Pump
constexpr unsigned long PumpTimes[] {OffDuration,OnDuration};
void setup()
{
pinMode(PumpPin, OUTPUT); // define LEDpin as output
digitalWrite(PumpPin, HIGH); // set initial state
}
void loop()
{
static unsigned long timeStamp=millis();
if (millis()-timeStamp>=PumpTimes[digitalRead(PumpPin)])
{
timeStamp=millis();
digitalWrite(PumpPin,digitalRead(PumpPin)^1);
}
}