Need some help with school project [solved]

Hello, as the title says, i'm having some issues with a school project. The idea is that there are four water tanks and a pump, once both the tank sensors read "LOW", the tank should start being filled, and not stop until the top sensor is closed. I've tried various logic structures and even rebuilt my circuit, but yet, i'm still having issues. Here is the code i'm using for testing:

if(digitalRead(!H1) && digitalRead(!L1)){Pump=HIGH,F1=HIGH;
if(digitalRead(!H1) && digitalRead(L1)){Pump=LOW,F1=LOW;}
while (digitalRead(!H1) && digitalRead(L1)){Pump=HIGH,F1=HIGH;break;}}

if(digitalRead(H1) && digitalRead(L1)){Pump=LOW,F1=LOW;}
digitalWrite(A0,Pump);digitalWrite(A1,F1);

If you guys can help me at all, it will be greatly appreciated.
Edit: I am using Arduino Uno.

You should post ALL your code and use code tags. Please read the rules.

I have just noticed that the copy for forum option in the IDE now works correctly so use that.

Hi,
Can you post a diagram of your tanks and where the sensors are and where the pump(s) are.
Tank1 Tank2 Tank3 Tank4 and Pump

Hello, as the title says, i'm having some issues with a school project.
(1) The idea is that there are four water tanks and a pump,
(2) once both the tank sensors read "LOW", the tank should start being filled
(3) and not stop until the top sensor is closed.
I've tried various logic structures and even rebuilt my circuit, but yet, i'm still having issues.
Here is the code i'm using for testing:

1, Four tanks and a pump, how are they connected?
2, BOTH tanks sensors, you have 4 tanks, which tank will start to fill?
3, Which top sensor?

Sorry but please read your first post again...

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your FULL sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom..... :slight_smile:

Tom, if you use "copy for forum" from the latest IDE it automatically includes the code tags.

when did they fix that

I assume from version 1.6.5 which is what I am running. I only found it out by accident yesterday as I was trying to get some colour annotation for a book.
Also rather annoyingly it automatically places closing braces in statements, and as I do that automatically after years of coding I end up with too many.

Code completion was the main reason though.

I bet that comes soon as it is in Processing3 now.

Sorry for my delayed response. Thank you to everyone who told me what was wrong with the original post, and I will take larger strides to not mess up like that again. Okay, with that said, I'll go through the project using only one tank as an example. The tank is equipped with two switches, one high switch (tank full), and one low switch (tank empty), when the tank is full, both switches are closed. when the tank is only partially full, the high switch is open, and the low switch is closed. When the tank is completely empty, both switches are open. This should cause a fill valve to open and the pump to, well, pump. the pump should continue running until both switches are closed, at which point the pump shuts off, and the fill valve closes. Attached is the file the teacher gave us regarding this lab (please note, it is a PDF.) And finally, the code sketch:

// Arduino UNO R3 Pin Mapping
// NOTE: Analog input pins A0 through A5 are predefined.
   int D0=0,D1=1,D2=2,D3=3,D4=4,D5=5,D6=6,D7=7,D8=8,D9=9,D10=10,D11=11,D12=12,D13=13,RXPIN=0,TXPIN=1;
// NOTE: PWM (Pulse Width Modulation) is available at pins D3, D5, D6, D9, D10, and D11 (in OUTPUT mode only).
// NOTE: SPI programming pins (MISO, MOSI, SCK and SS) are predefined.


// INCLUDE statements


// DEFINE statement definitions


// Declare global variables and arrays


// setup block (executed once at the beginning of the program with local scope)
void setup()
{
pinMode(D2,INPUT);
pinMode(D3,INPUT);
pinMode(D4,INPUT);
pinMode(D5,INPUT);
pinMode(D6,INPUT);
pinMode(D7,INPUT);
pinMode(D8,INPUT);
pinMode(D9,INPUT);
pinMode(A0,OUTPUT);
pinMode(A1,OUTPUT);
pinMode(A2,OUTPUT);
pinMode(A3,OUTPUT);
pinMode(A4,OUTPUT);
}


// loop block (continuously looping block)
void loop()
{
int H1=D2,H2=D3,H3=D4,H4=D5,L1=D6,L2=D7,L3=D8,L4=D9,Pump=A0,F1=A1,F2=A2,F3=A3,F4=A4;

if(digitalRead(!H1) && digitalRead(!L1)){Pump=HIGH,F1=HIGH;
if(digitalRead(!H1) && digitalRead(L1)){Pump=LOW,F1=LOW;}
while (digitalRead(!H1) && digitalRead(L1)){Pump=HIGH,F1=HIGH;break;}}

if((digitalRead(H1) && digitalRead(L1))||(digitalRead(H1)&&digitalRead(!L1))){Pump=LOW,F1=LOW;}  
digitalWrite(A0,Pump);digitalWrite(A1,F1);


if(!H2 && !L2){Pump=HIGH,F2=HIGH;} else{Pump=LOW,F2=LOW;}
digitalWrite(A0,Pump);digitalWrite(A2,F2);
if(!H3 && !L3){Pump=HIGH,F3=HIGH;} else{Pump=LOW,F3=LOW;}
digitalWrite(A0,Pump);digitalWrite(A3,F3);
if(!H4 && !L4){Pump=HIGH,F4=HIGH;} else{Pump=LOW,F4=LOW;}
digitalWrite(A0,Pump);digitalWrite(A4,F4);


}


// Custom function section

ELET 2202 - Lab 2 (Storage Tank Fill Controller).pdf (131 KB)

Clearly you didn't bother to read response #1

Delta_G:
That is happening so often to my posts lately that I'm starting to wonder if the forum is not showing them or something.

Lol, I think it's a wetware rather than a software issue.

pert:
Clearly you didn't bother to read response #1

Right, well, I have followed the advice that was given in response #1, but I went back to look at it after you had told me about it, and, quite frankly, I don't want to bog this thread down with back to back posts of a code sketch with small changes. Also, it's having the same problem I was before, which (for right now,) is that turning off the high switch causes the fill valve and pump to run.

Again, sorry for my late reply, but if you want to see all the code with even the smallest changes, then be my guest. As such, here is the updated code sketch:

// Arduino UNO R3 Pin Mapping
// NOTE: Analog input pins A0 through A5 are predefined.
   int D0=0,D1=1,D2=2,D3=3,D4=4,D5=5,D6=6,D7=7,D8=8,D9=9,D10=10,D11=11,D12=12,D13=13,RXPIN=0,TXPIN=1;
// NOTE: PWM (Pulse Width Modulation) is available at pins D3, D5, D6, D9, D10, and D11 (in OUTPUT mode only).
// NOTE: SPI programming pins (MISO, MOSI, SCK and SS) are predefined.


// INCLUDE statements


// DEFINE statement definitions


// Declare global variables and arrays


// setup block (executed once at the beginning of the program with local scope)
void setup()
{
pinMode(D2,INPUT);
pinMode(D3,INPUT);
pinMode(D4,INPUT);
pinMode(D5,INPUT);
pinMode(D6,INPUT);
pinMode(D7,INPUT);
pinMode(D8,INPUT);
pinMode(D9,INPUT);
pinMode(A0,OUTPUT);
pinMode(A1,OUTPUT);
pinMode(A2,OUTPUT);
pinMode(A3,OUTPUT);
pinMode(A4,OUTPUT);
}


// loop block (continuously looping block)
void loop()
{
int H1=D2,H2=D3,H3=D4,H4=D5,L1=D6,L2=D7,L3=D8,L4=D9,Pump=A0,F1=A1,F2=A2,F3=A3,F4=A4;

if(!digitalRead(H1) && !digitalRead(L1)){Pump=HIGH,F1=HIGH;
if(!digitalRead(H1) && digitalRead(L1)){Pump=LOW,F1=LOW;}
while (!digitalRead(H1) && digitalRead(L1)){Pump=HIGH,F1=HIGH;break;}}

if((digitalRead(H1) && digitalRead(L1))||(digitalRead(H1)&&!digitalRead(L1))){Pump=LOW,F1=LOW;}  
digitalWrite(A0,Pump);digitalWrite(A1,F1);


if(!H2 && !L2){Pump=HIGH,F2=HIGH;} else{Pump=LOW,F2=LOW;}
digitalWrite(A0,Pump);digitalWrite(A2,F2);
if(!H3 && !L3){Pump=HIGH,F3=HIGH;} else{Pump=LOW,F3=LOW;}
digitalWrite(A0,Pump);digitalWrite(A3,F3);
if(!H4 && !L4){Pump=HIGH,F4=HIGH;} else{Pump=LOW,F4=LOW;}
digitalWrite(A0,Pump);digitalWrite(A4,F4);


}


// Custom function section

And what are your results with the updated code, does it work as expected?

A suggestion:

Delta_G:
One statement to a line will make this a little more clear to you what's going on.

Just do Tools > Auto Format this will make it much easier for us to read and can be useful for finding problems because then your code will actually be properly indented! Squashing everything into one line doesn't make your sketch compile to a smaller size.

Unfortunately, not yet. telling the system that the tank isn't full causes it to automatically fill. Also thanks for the tip about the Auto Format, i didn't even know that was a thing.

okay, so now that I've used the auto-format, here is the newest sketch:

// Arduino UNO R3 Pin Mapping
// NOTE: Analog input pins A0 through A5 are predefined.
int D0 = 0, D1 = 1, D2 = 2, D3 = 3, D4 = 4, D5 = 5, D6 = 6, D7 = 7, D8 = 8, D9 = 9, D10 = 10, D11 = 11, D12 = 12, D13 = 13, RXPIN = 0, TXPIN = 1;
// NOTE: PWM (Pulse Width Modulation) is available at pins D3, D5, D6, D9, D10, and D11 (in OUTPUT mode only).
// NOTE: SPI programming pins (MISO, MOSI, SCK and SS) are predefined.


// INCLUDE statements


// DEFINE statement definitions


// Declare global variables and arrays


// setup block (executed once at the beginning of the program with local scope)
void setup()
{
  pinMode(D2, INPUT);
  pinMode(D3, INPUT);
  pinMode(D4, INPUT);
  pinMode(D5, INPUT);
  pinMode(D6, INPUT);
  pinMode(D7, INPUT);
  pinMode(D8, INPUT);
  pinMode(D9, INPUT);
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);
}


// loop block (continuously looping block)
void loop()
{
  int H1 = D2, H2 = D3, H3 = D4, H4 = D5, L1 = D6, L2 = D7, L3 = D8, L4 = D9, Pump = A0, F1 = A1, F2 = A2, F3 = A3, F4 = A4;

  if (!digitalRead(H1) && !digitalRead(L1)) {
    Pump = HIGH, F1 = HIGH;
    if (!digitalRead(H1) && digitalRead(L1)) {
      Pump = LOW, F1 = LOW;
    }
    while (!digitalRead(H1) && digitalRead(L1)) {
      Pump = HIGH, F1 = HIGH;
      break;
    }
  }

  if ((digitalRead(H1) && digitalRead(L1)) || (digitalRead(H1) && !digitalRead(L1))) {
    Pump = LOW, F1 = LOW;
  }
  digitalWrite(A0, Pump); digitalWrite(A1, F1);


  if (!H2 && !L2) {
    Pump = HIGH, F2 = HIGH;
  } else {
    Pump = LOW, F2 = LOW;
  }
  digitalWrite(A0, Pump); digitalWrite(A2, F2);
  if (!H3 && !L3) {
    Pump = HIGH, F3 = HIGH;
  } else {
    Pump = LOW, F3 = LOW;
  }
  digitalWrite(A0, Pump); digitalWrite(A3, F3);
  if (!H4 && !L4) {
    Pump = HIGH, F4 = HIGH;
  } else {
    Pump = LOW, F4 = LOW;
  }
  digitalWrite(A0, Pump); digitalWrite(A4, F4);


}


// Custom function section

Your pin names are awful!

What's the benefit of using "D1" instead of "1"?

You should give your variables and constants meaningful names.

Here's an example of some code I'm writing.

#define SPEED_RX_PIN 8      // Connect to aileron pin on receiver
#define STEERING_RX_PIN 9   // Connect to elevator pin on receiver
#define AUX_RX_PIN 10       // Connect to which ever pin you want on receiver

// Assign your channel out pins
#define PORT_SERVOS_PIN 5       // servos on the left side of the robot
#define STARBOARD_SERVOS_PIN 6  // servos on the right side of the robot
#define AUX_SERVO_PIN 7

Your code is very hard to read since the constant names are meaningless. You use the variable "Pump" but that's the only clue we get to what's going on.

I'm sure there are plenty of people willing to help you with this but I'm not sure how many will be willing to try to help when the constant names are meaningless.

Using good variable names and constant names can make code much easier to read. Few comments are needed when good constant and variables names are used.

Speaking of comments. It looks like the only comments in the program are those added by some template. It helps both yourself and others trying to read your code if you add comments stating why some piece of code exists.

okay, so I just got some help from my instructor and the newest sketch looks like this:

// Arduino UNO R3 Pin Mapping
// NOTE: Analog input pins A0 through A5 are predefined.
int D0 = 0, D1 = 1, D2 = 2, D3 = 3, D4 = 4, D5 = 5, D6 = 6, D7 = 7, D8 = 8, D9 = 9, D10 = 10, D11 = 11, D12 = 12, D13 = 13, RXPIN = 0, TXPIN = 1;
// NOTE: PWM (Pulse Width Modulation) is available at pins D3, D5, D6, D9, D10, and D11 (in OUTPUT mode only).
// NOTE: SPI programming pins (MISO, MOSI, SCK and SS) are predefined.


// INCLUDE statements


// DEFINE statement definitions


// Declare global variables and arrays


// setup block (executed once at the beginning of the program with local scope)
void setup()
{
  pinMode(D2, INPUT);
  pinMode(D3, INPUT);
  pinMode(D4, INPUT);
  pinMode(D5, INPUT);
  pinMode(D6, INPUT);
  pinMode(D7, INPUT);
  pinMode(D8, INPUT);
  pinMode(D9, INPUT);
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);
}


// loop block (continuously looping block)
void loop()
{
  int H1 = D2, H2 = D3, H3 = D4, H4 = D5, L1 = D6, L2 = D7, L3 = D8, L4 = D9, Pump = A0, F1 = A1, F2 = A2, F3 = A3, F4 = A4;

  if (!digitalRead(L1)) {
    digitalWrite(Pump, HIGH); digitalWrite(F1, HIGH);
    while (!digitalRead(H1)) {}

  }

  if ((digitalRead(H1) && digitalRead(L1)) || (digitalRead(H1) && !digitalRead(L1))) {
    Pump = LOW, F1 = LOW;
  }
  digitalWrite(A0, Pump); digitalWrite(A1, F1);


  if (!H2 && !L2) {
    Pump = HIGH, F2 = HIGH;
  } else {
    Pump = LOW, F2 = LOW;
  }
  digitalWrite(A0, Pump); digitalWrite(A2, F2);
  if (!H3 && !L3) {
    Pump = HIGH, F3 = HIGH;
  } else {
    Pump = LOW, F3 = LOW;
  }
  digitalWrite(A0, Pump); digitalWrite(A3, F3);
  if (!H4 && !L4) {
    Pump = HIGH, F4 = HIGH;
  } else {
    Pump = LOW, F4 = LOW;
  }
  digitalWrite(A0, Pump); digitalWrite(A4, F4);


}


// Custom function section

note: still not working as intended.

DuaneDegn:
What's the benefit of using "D1" instead of "1"?

well, i'm using both the digital and analog pins, I'm still new to arduino as a whole, so it's more of a way for me to keep from getting pins mixed up. And "H1" refers to "High Switch 1", "L1" refers to "Low Switch 1", "F1" is referring to "Fill Valve 1",and so on.

translego1:
And "H1" refers to "High Switch 1", "L1" refers to "Low Switch 1", "F1" is referring to "Fill Valve 1",and so on.

That helps some. I personally think you should use "HIGH_SWITCH_1" instead of "H1" but not everyone likes long constant and variable names like I do.

You've still got severe problems with your code.

if (!H2 && !L2)

The above will always evaluate to be the same. You're not comparing the states of those pins, you're just comparing the pin numbers. I'm not sure what "!H2" evaluates as but it will generate the same value every time through the loop.

I was wrong. H2 is not a constant. It's a variable. It's still not doing what you think it is.

  int H1 = D2, H2 = D3, H3 = D4, H4 = D5, L1 = D6, L2 = D7, L3 = D8, L4 = D9, Pump = A0, F1 = A1, F2 = A2, F3 = A3, F4 = A4;

I'm not sure why you want to assign pin values to variables. I think you don't.

I think you want something like this:

  byte topSensorState1 = digitalRead(D2);
  byte topSensorState2 = digitalRead(D3);
  byte topSensorState3 = digitalRead(D4);
  byte topSensorState4 = digitalRead(D5);
  byte bottomSensorState1 = digitalRead(D6);
  byte bottomSensorState2 = digitalRead(D7);
  byte bottomSensorState3 = digitalRead(D8);
  byte bottomSensorState4 = digitalRead(D9);

  if (topSensorState1 == LOW && bottomSensorState1 == LOW) {
    digitalWrite(Pump) = HIGH;
    digitalWrite(F1) = HIGH;

You can't just use the pin numbers to figure out if a pin is high or low, you have read the pin. The same goes for setting a pin high.

"Pump = HIGH" I believe you just set the value of the variable "Pump" to one. The pin state did not change.

I strongly recommend you work through some basic example sketches.

It sounds trivial, but learning to turn a LED on and off applies to all sorts of other tasks. I think it's a good idea to revisit some of the basic input and output lessons.

Learn/relearn to control multiple LEDs. Learn/relearn to read a push button. Learn to read multiple push buttons.

Learn to control the LEDs with push buttons.

I don't know if you've done this yet, but model your pumps and sensors with LEDs and push buttons.

Use serial output to make sure you're code is doing what you think it is.

Your current program is nowhere close to working.

You really need to learn/relearn basic input output techniques.

Doing comparisons on PIN numbers is meaningless, you have been told this repeatedly and you keep ignoring it.

Pin name assignments are convoluted and unnecessary. It is not big and clever to do this but stupid.

When doing a project never write all the code and then test. Write a small piece of code, test it, get it working before adding the next little bit. Build up the project from working code.

Again, sorry for my delayed response. Okay, so I read through the latest comments and saw several things that I will address here.

I like to use short variable names so that I don't end up trying to use two slightly different names for one thing.

It's not so much that i'm trying to assign pin values to variables, it's more so I'm trying to rename the pins, so that I don't get confused which pin is which.

DuaneDegn:
You can't just use the pin numbers to figure out if a pin is high or low, you have read the pin. The same goes for setting a pin high.

I thought that was the point of using "digitalRead" and "digitalWrite", otherwise, wouldn't it have a more straight-forward name?

I do plan on doing more sample projects, but I only got this kit shortly before this project, which is the second one and the very first thing we did was set up and "SOS" message with only a single LED.

Grumpy_Mike:
Doing comparisons on PIN numbers is meaningless, you have been told this repeatedly and you keep ignoring it.

Pin name assignments are convoluted and unnecessary. It is not big and clever to do this but stupid.

When doing a project never write all the code and then test. Write a small piece of code, test it, get it working before adding the next little bit. Build up the project from working code.

I'm not quite sure I caught your meaning, but if someone could explain, that would help.

If it is, as you say, stupid to do so, then I must also be stupid. I do not claim to know everything, why else would I ask for help on this?

Thank you for the advice, i'll try to do just that in the future.

Remember, I'm still new to all of this and learning as I go, stumbling and occasionally falling, like a young child learning to walk, but I am at least making an effort to balance myself and take smaller steps as I go. While I do appreciate all of the advice and critique, it feels as if though some of you are simply shouting "You're doing it wrong!" without explaining how I'm wrong. I do hope that you remember that an awful lot of the people coming here may be new to arduino as a whole, and may not understand what you mean when you say something. it would be like speaking in star-trek technobabble to someone who just thawed out of a frozen 70 year nap.

Alright, with that wall of text out of the way, the good stuff. I did get more help from my teacher, managed to get this thing working, which is one of the biggest things I've ever done. So, for those interested, here is the new, fixed, working code:

// Arduino UNO R3 Pin Mapping
// NOTE: Analog input pins A0 through A5 are predefined.
int D0 = 0, D1 = 1, D2 = 2, D3 = 3, D4 = 4, D5 = 5, D6 = 6, D7 = 7, D8 = 8, D9 = 9, D10 = 10, D11 = 11, D12 = 12, D13 = 13, RXPIN = 0, TXPIN = 1;
// NOTE: PWM (Pulse Width Modulation) is available at pins D3, D5, D6, D9, D10, and D11 (in OUTPUT mode only).
// NOTE: SPI programming pins (MISO, MOSI, SCK and SS) are predefined.
/*the section above is part of a template created by my instructor to probably make sure that the students actually look at it.
*/
// INCLUDE statements


// DEFINE statement definitions


// Declare global variables and arrays


// setup block (executed once at the beginning of the program with local scope)
void setup()
{
  /*Assigning pin modes to the various pins used in this exercise.*/
  pinMode(D2, INPUT);
  pinMode(D3, INPUT);
  pinMode(D4, INPUT);
  pinMode(D5, INPUT);
  pinMode(D6, INPUT);
  pinMode(D7, INPUT);
  pinMode(D8, INPUT);
  pinMode(D9, INPUT);
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);
}


// loop block (continuously looping block)
void loop()
{
  /*Renaming the various pins for the sake of not telling it to do the same thing more than once with the wrong inputs.*/
  int H1 = D2, H2 = D3, H3 = D4, H4 = D5, L1 = D6, L2 = D7, L3 = D8, L4 = D9, Pump = A0, F1 = A1, F2 = A2, F3 = A3, F4 = A4;

  if (!digitalRead(L1)) {
    digitalWrite(Pump, HIGH); digitalWrite(F1, HIGH);
    while (!digitalRead(H1)) {}
    digitalWrite(Pump, LOW); digitalWrite(F1, LOW);
  }
  if (!digitalRead(L2)) {
    digitalWrite(Pump, HIGH); digitalWrite(F2, HIGH);
    while (!digitalRead(H2)) {}
    digitalWrite(Pump, LOW); digitalWrite(F2, LOW);
  }
  if (!digitalRead(L3)) {
    digitalWrite(Pump, HIGH); digitalWrite(F3, HIGH);
    while (!digitalRead(H3)) {}
    digitalWrite(Pump, LOW); digitalWrite(F3, LOW);
  }
  if (!digitalRead(L4)) {
    digitalWrite(Pump, HIGH); digitalWrite(F4, HIGH);
    while (!digitalRead(H4)) {}
    digitalWrite(Pump, LOW); digitalWrite(F4, LOW);
  }

}


// Custom function section