Please Help learning programming Arduino

Hello,

I'm trying to do a little project for garden irrigation.
I have good understanding in electronics, also I have basics in programing as I have programmed in the school in Pascal and Q Basic.
This is my first try to work with Arduino, but even Arduino Sofrware does not show any mistakes it still does not do what it should.

The Idea is to pump water from a canister in to the barrel not over filling it. In the canister is installed two sensors top and bottom, top to give a signal to start pumping water in to the barrel and the bottom to stop pumping leaving the pump in the water.

Please see code below:

const int pump = 5; //pompa
const int canistertop = 10; //canisters top level sensor
const int canisterbot = 11; //canisters bottom level sensor
const int barreltop = 12; //barrels top sensor
int ctop = 0;
int cbot = 0;
int btop = 0;
void setup() {
*pinMode(pump, OUTPUT); *
pinMode(canistertop, INPUT);
*pinMode(canisterbot, INPUT); *
*pinMode(barreltop, INPUT); *
}
void loop() {
btop = digitalRead(barreltop);
ctop = digitalRead(canistertop);
if (btop == HIGH) //if barrel full

  • {*
  • delay(600000); //wait for 10 minutes*
  • }*
  • else if (ctop == HIGH) //if barrel not full*
  • {*
  • do*
  • {*
  • digitalWrite(pump, HIGH); //start pump to pump water from canister in to barrel*
  • cbot = digitalRead(canisterbot);*
  • btop = digitalRead(barreltop);*
  • }*
  • while (btop == LOW && cbot == HIGH); //pump water until barrel top sensor LOW and canister botom sensor HIGH*
  • {*
  • digitalWrite(pump, LOW); //if not swichthe pump off*
  • }*
  • }*
  • }*

Thank You in advance

What happens when you run it?

I don't really understand what the canister-top signal is good for.

I would not use delay/while/for to control timing or behaviour.

const byte pump = 5;  //pompa
const byte canistertop = 10;  //canisters top level sensor
const byte canisterbot = 11;  //canisters bottom level sensor
const byte barreltop = 12;    //barrels top sensor

void setup() {
  pinMode(pump, OUTPUT);
  pinMode(canistertop, INPUT);
  pinMode(canisterbot, INPUT);
  pinMode(barreltop, INPUT);
}

unsigned long lastCheck;
unsigned long checkAll = 1000; // 600000UL; // faster for test

void loop() {
  static bool pumpRunning = false;
  //  byte ctop = digitalRead(canistertop);
  byte cbot = digitalRead(canisterbot);
  byte btop = digitalRead(barreltop);
  unsigned long topLoop = millis();

  if (pumpRunning) {
    if (btop == HIGH || cbot == LOW) {  // if barrel full or canister empty
      lastCheck = topLoop;
      pumpRunning = false;
      digitalWrite(pump, LOW);
    }
  } else {
    if (topLoop - lastCheck >= checkAll)  {
      lastCheck = topLoop;
      if (btop == LOW && cbot == HIGH) {  // if barrel not full and canister not empty
        pumpRunning = true;
        digitalWrite(pump, HIGH);  // pump water from canister in to barrel
      }
    }
  }
}

joncikas:
The Idea is to pump water from a canister in to the barrel not over filling it. In the canister is installed two sensors top and bottom, top to give a signal to start pumping water in to the barrel and the bottom to stop pumping leaving the pump in the water.

Wouldn’t it also be essential to have a sensor in the barrel to detect when it is full?

…R

There is a sensor at the top of the barrel which called -barreltop-
For some strange reason pump starts as soon as I switch the power ON regardless what my sensors showing... :frowning:
Please advise....

How are your switches connected?
I checked the code using INPUT_PULLUP, switches closing to ground, an LED as pump.

As you are not using INPUT_PULLUP, do you have external pullups or pulldowns, or active sensors?

Delay I used with idea - If barrel full I want system to wait for ten minutes, as irrigation system during that time might use some water from the barrel
Because I do not want a pump often switching ON and OFF I installed canister Top level switch. The pump switches on only when canister is full.

Whats regarding pullups I have connected 10k resistors from ground to the digital legs 10, 11 and 12 where I get level signals.

joncikas:
Delay I used with idea - If barrel full I want system to wait for ten minutes, as irrigation system during that time might use some water from the barrel

My code does the same without delay.
(if you change the 1 second test intervall to the commented value)

joncikas:
Because I do not want a pump often switching ON and OFF I installed canister Top level switch. The pump switches on only when canister is full.

So the pump will once lower the canister below canister top, and never start after that.

Or did you forget to mention the slave that is to fill the canister?

joncikas:
There is a sensor at the top of the barrel which called -barreltop-

You did not mention it in your Original Post

In the canister is installed two sensors top and bottom, top to give a signal to start pumping water in to the barrel and the bottom to stop pumping leaving the pump in the water.

...R

    while (btop == LOW && cbot == HIGH);  //pump water until barrel top sensor LOW and canister botom sensor HIGH

Your pump is only going to pump if barrel top sensor LOW and canister bottom sensor HIGH.
That's not what you want right ?