problem code

Why Void Loop doesn't implement this code.

void loop() {
Liquid_level = digitalRead(3);
if (Liquid_level == 1) {
digitalWrite(7, HIGH );
//delay(100);
}

{
if (Liquid_level == 0) {
digitalWrite(7, LOW );
}
delay(100);

}

Here is the whole code. The rest is fulfilled. thanks

#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h>

int Liquid_level = 0;
void setup() {
pinMode(3, INPUT);
pinMode(4, OUTPUT);// connected to S terminal of Relay
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}

void loop() {
Liquid_level = digitalRead(3);
if (Liquid_level == 1) {
digitalWrite(7, HIGH );
//delay(100);
}

{
if (Liquid_level == 0) {
digitalWrite(7, LOW );
}
delay(100);

}
{
digitalWrite(4, LOW); // turn relay ON
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
delay(20000);// keep it ON for 3 seconds

digitalWrite(4, HIGH);// turn relay OFF
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
delay(3000);

digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
delay(99999);
}
}

Liquid_level = digitalRead(3);
  if (Liquid_level == 1) {
    digitalWrite(7, HIGH );
    //delay(100);
  }

  {
  if (Liquid_level == 0) {
    digitalWrite(7, LOW );

akadigitalWrite(7, digitalRead(3));

sorry but i didn't understand

the two codes work independently, but combining them into work is where I go wrong. thanks

first code:

#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h>

int Liquid_level=0;
void setup() {

pinMode(3,INPUT);
pinMode(7,OUTPUT);
}

void loop() {

Liquid_level=digitalRead(3);
if (Liquid_level==1){
digitalWrite(7,HIGH);
}

if (Liquid_level==0){
digitalWrite(7,LOW);

}
delay(100);

}

the second code:

void setup() {
Serial.begin(9600);
pinMode(4, OUTPUT);// connected to S terminal of Relay
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);

}

void loop() {

digitalWrite(4,LOW);// turn relay ON
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
delay(20000);// keep it ON for 3 seconds

digitalWrite(4, HIGH);// turn relay OFF
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
delay(3000);

digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
delay(9999);
}

and the final code:

#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h>

int Liquid_level = 0;
void setup() {
pinMode(3, INPUT);
pinMode(4, OUTPUT);// connected to S terminal of Relay
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);

}

void loop() {

Liquid_level = digitalRead(3);
if (Liquid_level == 1) {
digitalWrite(7, HIGH );
//delay(100);
}

{
if (Liquid_level == 0) {
digitalWrite(7, LOW );
}
delay(100);

}
{
digitalWrite(4, LOW); // turn relay ON
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
delay(20000);// keep it ON for 3 seconds

digitalWrite(4, HIGH);// turn relay OFF
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
delay(3000);

digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
delay(999999);
}
}

How do you know it does not implement the code?

There is a 100 second delay at the end of loop; maybe it might look as if it's not working.

What is the expected behaviour?
What does it do?

Add serial print statements at relevant places to debug your code. So you can see what it is doing.

Your code mentions 'relay'; link please. How is everything wired; schematic please.

Please edit your post and add

[code]before your code and [/code]
 after your code

.

Please learn to indent properly; use tools -> autoformat in the IDE.

Liquid_level = digitalRead(3);
  if (Liquid_level == 1) {
    digitalWrite(7, HIGH );
    //delay(100);
  }

  {
  if (Liquid_level == 0) {
    digitalWrite(7, LOW );
}

akadigitalWrite (7, digitalRead(3));

If you are expecting the input from pin 3 to be responsive, you need to get rid of the delay() statements. In the original sketch the input is read every 100mS, in the combined sketch you have three delays, one is 20 seconds, the second 3 seconds, and the last either 99.999 seconds or 999.999 seconds, depending on which of your postings I'm reading. That means you are only reading the input once every 123 seconds (a bit over 2 minutes) at best, or once every 1023 seconds (almost 17 mintues) at worst. Also, the input has no memory of anything that happens during that interval, so if you are pressing a button, or reading a switch, the digitalRead only tells you its state at the instant the digitalRead is executed, not whether it was pressed at some time between reads.

Have a look at the tutorial for BlinkWithoutDelay to see how to do timing delays without the use of delay()

thank you very much

Also I think you have your curly brackets wrong. Why do not write statements like this:

if (some condition is true or false)
{
Do something here
}
else if (some other condition true or false)
{
Do something here
}
else
{
Do something here
}

I'm trying to control 3 relays with different run times, and monitor the fluid level with a non -contact level sensor to give me an LED
pin 4,5,6 - relay
pin 3 - level sensor input
pin 7 - LED output
when the level drops the LED lights up

so far i have done it with two arduino nano microcontrollers with one controlling the relays and the other a contactless sensor. Now it is trying to combine the two codes and go to one microcontroller

drago81:
Now it is trying to combine the two codes and go to one microcontroller

You should look at state machines either here or here, and have 3 states say st_relay1, st_relay2 and st_relay3, cycling delay()-lessly with millis(). Then have the sensor/led outside the state machine being checked each time through loop().

Exactly...state machine is the way to go

Try this...

(Read the comments, set the times in the variables, and check the line marked <<<<)

// https://forum.arduino.cc/index.php?topic=663480
// 3x relays, 1x sensor/led
// 8 feb 2020

/*
  pin 4,5,6 - relay
  pin 3 - level sensor input
  pin 7 - LED output
  when the level drops the LED lights up
*/

//*************************
//to use RELAY_ON or RELAY_OFF in digitalWrite(), instead of HIGH or LOW
//(un-)comment the next line depending on the relay's logic <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//#define weHaveAnActiveLowRelay // (I'm testing with leds from digital pin to ground)
#ifdef weHaveAnActiveLowRelay
#define RELAY_ON LOW   // most relay modules use this *active low* logic
#define RELAY_OFF HIGH
#else
#define RELAY_ON HIGH   // for testing with an *active high* relay or led to ground
#define RELAY_OFF LOW
#endif
//************************

//  there is a bwod on led13 to prove no blocking
unsigned long previousBlink;
int blinkInterval = 500;
bool blinkState;

long relay1Ontime = 1000; //set these to real times, check data type is big enough
long relay2Ontime = 2000;
long relay3Ontime = 3000;
unsigned long stateChangedAt;

//states
enum {ST_relay1, ST_relay2, ST_relay3} currentState = ST_relay1;
// enum is just a handy way to give the numbers 0,1,2 some human-friendly names,
//    for use in the switch..case further down

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println("setup() ... ");
  Serial.println("3x relays, 1x sensor/led, forum 663480");

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(3, INPUT_PULLUP); //note switch is to ground, was convenient for me 
  pinMode(4, OUTPUT);// connected to S terminal of Relay
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

  Serial.println("setup() done");
  Serial.println(" ");
  //Serial.print("relay1 ");
}

void loop()
{
  proofOfLife(); // just a delay()-less "pulse"
  manageStates(); // all of which is delay()-less, so...
  digitalWrite(7, digitalRead(3)); //... this will be checked each time thru loop()
} //loop

void proofOfLife()
{
  if (millis() - previousBlink >= blinkInterval)
  {
    previousBlink = millis();
    blinkState = !blinkState;
    digitalWrite(LED_BUILTIN, blinkState);
  }
}

void manageStates()
{
  switch (currentState)
  {
    case ST_relay1:
      digitalWrite(4, RELAY_ON); 
      digitalWrite(5, RELAY_OFF);
      digitalWrite(6, RELAY_OFF);
      if (millis() - stateChangedAt >= relay1Ontime) //means we've been here long enough...
      {
        currentState = ST_relay2; //... so choose next state, and
        stateChangedAt = millis(); //... make a note of when we switched states
        //Serial.print("relay2 ");
      }
      break;

    case ST_relay2:
      digitalWrite(4, RELAY_OFF);
      digitalWrite(5, RELAY_ON);
      digitalWrite(6, RELAY_OFF);
      if (millis() - stateChangedAt >= relay2Ontime)
      {
        currentState = ST_relay3;
        stateChangedAt = millis();
        //Serial.println("relay3");
      }
      break;

    case ST_relay3:
      digitalWrite(4, RELAY_OFF);
      digitalWrite(5, RELAY_OFF);
      digitalWrite(6, RELAY_ON);
      if (millis() - stateChangedAt >= relay3Ontime)
      {
        currentState = ST_relay1; //repeat cycle
        stateChangedAt = millis();
        //Serial.print("relay1 ");
      }
      break;
  }//switch
}//manageStates

Thank you very much. Works very well

drago81:
Thank you very much. Works very well

Cool... do you understand it :wink:

how can i reverse the logic between pin 3 and pin 7. when pin 3 is logical 0 pin 7 is logical 1

drago81:
how can i reverse the logic between pin 3 and pin 7. when pin 3 is logical 0 pin 7 is logical 1

1 - 1 = 0
1 - 0 = 1