Can't find the bug in my arduino code? :(

Hello guys, :)

This is my first time on this forum but I've got a question, so I made this code that is supposed to check 3 analog inputs and tell me in the serial which one's are above 500 and as soon as the first one is above 500 the second and third also display they are above 500 even if they aren't the same goes for the second if he's above 500 the third also says it's above 500. I know this explanation is poor but I'm not quite sure how to explain this error :o :o :o

Here is the code:

void setup() { Serial.begin(9600); Serial.print("Systems online: ");

int sensorValue2 = 2; sensorValue2 = analogRead(A2); if (sensorValue2 > 500) { Serial.print("1, "); } else { Serial.print("Error #001, "); } Serial.println();

int sensorValue1 = 1; sensorValue1 = analogRead(A1); if (sensorValue1 > 500) { Serial.print("2, "); } else { Serial.print("Error #002, "); }

int sensorValue0 = 0; sensorValue0 = analogRead(A0); if (sensorValue0 > 500) { Serial.print("3, "); } else { Serial.print("Error #003, "); } } void loop() {

}

Sorry for my bad English I know I'm supposed to post this in my own languages but in my country there aren't that much people who know about Arduino.

Anything inside setup() ONLY gets called once.

If you want to keep reading your analog inputs, you need to move all that code to loop().

int sensorValue2 = 2;
  sensorValue2 = analogRead(A2);

No need to initialise then assign, may as well do it on the same line:

int sensorValue2 = analogRead(A2);

Your English is ok, but can you try break down your problem into steps because I still don't think this program will do what you want from it?

Hi sjrosje

Can you post a diagram of what you have connected to the analog ports. A photo of a hand-drawn diagram is OK so long as it is clear.

as soon as the first one is above 500 the second and third also display they are above 500 even if they aren't the same

Why do you think the second and third inputs should not be above 500 in this situation?

Regards

Ray

Welcome to the Forum. Please read Nick Gammon’s two posts at the top of this Forum for guidelines on posting here. When posting code, please use the code tags ("</>") and use Ctrl-T in the IDE to reformat your code in a more standard format. This will help us help you.

Thank you all for replying :slight_smile:

So first of all I updated the code with the tips you gave me

void setup() {
  Serial.begin(9600);
}

void loop() {

  Serial.print("Systems online: ");

  int sensorValue0 = analogRead(A0);
  if (sensorValue0 > 500) {
    Serial.print("1, ");
  }
  else
  {
    Serial.print("Error #001, ");
  }

  int sensorValue1 = analogRead(A1);
  if (sensorValue1 > 500) {
    Serial.print("2, ");
  }
  else
  {
    Serial.print("Error #002, ");
  }

  int sensorValue2 = analogRead(A2);
  if (sensorValue2 > 500) {
    Serial.print("3, ");
  }
  else
  {
    Serial.print("Error #003, ");
  }

  Serial.println();

}

But now I’ve run into a new problem (see picture Attachment) As you can see if 1 analog pin is above 500 the serial monitor “says” they all are above 500, the reason why i had the code first in the startup was because i only needed to check the inputs 1 time this is actully part of a bigger code. And the stuff that I am going to attach to it are mostly light sensor and i do have some basics skils in arduino but I’m better with things like logic gates

Btw is there an why to like tag people when you answer ther reply?

I hope you know what’s wrong and have a great day :slight_smile:

In the Arduino, there is one ADC which is multiplexed (switched) to each of the analog input pins when required (i.e. when you make a call to analogRead(n)).

The input circuit of the ADC has a small capacitor which is charged or discharged according to the input voltage and the source impedance of the circuit that is connected to the analog input pin.

You are leaving the other two input pins disconnected when you apply 5V to the first pin. When you try to read their values, there is nowhere for the charge on the capacitor to go to, so the input reads the same voltage as you applied to the other pin.

The recommended impedance of the circuit connected to the analog input is 10K or less.

I appreciated that you may be wiring them this way for testing. But what are you actually going to measure with the analog inputs?

Thank you for your reply

This is the plan simply so Arduino gets power and will set 3 digital pins high (with transistors attached ) and they will all be connected to 3 different machine’s and those will be connected to an end gate and if all of them are working then Arduino will get 1 digital pin high and the code will go on but if one won’t turn on the end gates wont go open and the digital pin will be low than the Arduino should check all 3 of them (via analog because i don’t know how to check 3 after each other with digital pin) so i will be able to see which one is not working because they will be located 3 feet underground

And no I am not farming weed or something like that :slight_smile:

than the Arduino should check all 3 of them (via analog because i don't know how to check 3 after each other with digital pin)

So each of the 3 machines will provide a digital input to a different Arduino pin? And you need to print a different message depending on which pins are HIGH and which are LOW?

Don't use analogRead() :)

Look at digitalRead() and pinMode()in the Arduino reference on this website.

If you have used all the digital pins on the Arduino for other things, you can use the analog pins (A0, A1, etc) as [u]digital[/u] inputs if you call pinMode() first.

3 different machine's and those will be connected to an end gate and if all of them are working then Arduino will get 1 digital pin high

Do you mean "AND gate"? Since you need to run three wires plus GND back to the Arduino anyway, you don't need external logic. You could "AND" the three input signals in your program.

Thank you once again! :)

So after doing some research updated my code with digitalRead()

void setup() {
  Serial.begin(9600);
}

void loop() {

  Serial.print("Systems online: ");
  pinMode(11, INPUT);
  pinMode(12, INPUT);
  pinMode(13, INPUT);

  int dig1 = digitalRead(11);
  if (dig1 == HIGH) {
    Serial.print("1, ");
  }
  else {
    Serial.print("Error #001, ");
  }

  int dig2 = digitalRead(12);
  if (dig2 == HIGH) {
    Serial.print("2, ");
  }
  else {
    Serial.print("Error #002, ");
  }

  int dig3 = digitalRead(13);
  if (dig3 == HIGH) {
    Serial.print("3, ");
  }
  else {
    Serial.print("Error #003, ");
  }

  Serial.println();
}

Now I did some more research and don't see why it doens't work because it makes the same "mistakes" as he did with the AnalogRead() It could be my arduino because when i just bought it I accidently over powerd it with like 20v through the 13 pin..? (I know how stupid it sounds) But he works good but sometimes he just shutdowns or stuff like that (I've just order a new one) But maybe you've got some tips left? So not Thank you very much for all your help I wish I used this forum befor I would know so much more :)

Have a greatday

OK, so now the errors you are getting may be down to how your hardware is connected to the Arduino.

Can you post a diagram. A photo of a hand-drawn diagram is fine so long as it is clear.

Please show what sort of outputs your machine has - are they digital outputs from an electronic circuit or are they relays / switches that close mechanical contacts?

20V :o

Better order several replacements to have on hand.

As the machine (One of them is a pump) So as soon as the pomp is powered it opens a mechanical delay and 5+ goes to pin 13 DigitalRead() And the same goes for pin 11 and 12 only different machine’s
So I thought this would work but I think I’m doing something wrong? :frowning:

as soon as the pomp is powered it opens a mechanical delay and 5+ goes to pin 13 DigitalRead()

Different languages use "open" and "close" for switches in different ways :) And I'm not sure if you mean "delay" or "relay".

When the pump is powered, a mechanical relay (?) connects the Arduino pin to +5V, is that right?

And when the pump is not powered, does the relay connect the Arduino pin to anything, or does it leave pin it disconnected?

If it leaves it disconnected, this is called a "floating input pin". It can read +5V even though nothing is connected to it.

You need to connect a pulldown resistor (value = 10K) between the pin and GND on the Arduino. When the pump is not powered, the resistor will make sure that you get a LOW value when you do digitalRead(). When the pump connects the pin to +5V, you will get HIGH value.

It works! :)

Yes it is a relay for sure but the 10k resistor to ground works perfectly

Thnx you so much for your help :)

That's good to hear :)

For future reference, there is another way to solve the problem, but you would need to change the wiring so that the relay connects the Arduino pin to GND, not +5V. Since your pumps are underground, maybe that is not easy to do.

But if you could change the wiring, the Arduino has an internal pullup resistor which you can enable like this:

pinMode(11, INPUT_PULLUP);

You would use this internal resistor instead of connecting a physical resistor. You would also need to change your code because LOW would mean pump on and HIGH would mean pump off.

Yeah I’ll stick with just adding the resistors

But still next time I’ll build something I will use the
pinMode(11, INPUT_PULLUP); way

Thank you for all your help :slight_smile: