Why analogRead doesn't work?

I’ve a problem. I wanna make simple bomb disabling simulator - just for fun.
I’ve connected two wires from digital_pin 9 to A0 and from dp 10 to A1.
Although program doesn’t work in a loop. Even when wires are still connected (not cutted) program goes back to begining. Can you help me and take a look if there are any mistakes in the code?
Thanks a lot

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
  delay(5);
  //checking analogRead on serial
  // Serial.begin(9600);
  // while (!Serial);
  pinMode(6, OUTPUT);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(13, OUTPUT);

  for (int a = A3; a < A6; a++)
  {
    pinMode(a, OUTPUT);
  }


}
void(* resetFunc) (void) = 0;

void loop()
{
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);

  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("READY?" );

  //checking analogRead on serial
  // for (int s=1; s<500;s++)
  // {
  // Serial.println(analogRead(A0));
  //  delay(1);
  // }
  //  Serial.println(analogRead(A2));

  boolean state= 0;
  if (digitalRead(7) == HIGH)
  {
    delay(30);
    state= !state;
    digitalWrite(7, state);
    while (digitalRead(7) == HIGH);
    delay(30);

  }


  {



    for (int a = A3; a < A6; a++)
    {
      digitalWrite(a, HIGH);
    }

    delay(750);
    for (int a = A3; a != A6; a++)
    {
      digitalWrite(a, LOW);
      tone(8, 200, 50);
      delay(750);
    }
    tone(8, 2000, 400);

    

    if (analogRead(A0) == 1023 && analogRead(A1) == 1023)
    {

      lcd.setCursor(0, 0);
      lcd.print("DANGER");
      lcd.setCursor(0, 1);
      lcd.print(millis() / 100);
      digitalWrite(13, HIGH);
      delay(200);

      digitalWrite(13, LOW);
      delay(200);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("DO SOMETHING");
      digitalWrite(13, HIGH);
      delay(200);

      digitalWrite(13, LOW);
      lcd.clear();
      delay(200);
    }
    else
    {
      if (analogRead(A0) == 1023 || analogRead(A1) == 1023)


      {
for (int a=A3; a<A6; a++)
{
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("OOOPS...");
        digitalWrite(a, HIGH);
        delay(100);
        lcd.clear();
        digitalWrite(a, LOW);
        delay(100);
       }
        tone(8, 200, 150);
      }



      else

      {
        lcd.clear();
        int score = millis() / 100;
        lcd.setCursor(0, 1);
        lcd.print("Good! :)");
        digitalWrite(6, HIGH);
        delay(1700);
        lcd.clear();


       
          lcd.setCursor(0, 0);
          lcd.print("SCORE: " );
          lcd.setCursor(8, 0);
          lcd.print(score);
          delay(2500);
          lcd.clear();
         

 

            lcd.setCursor(0, 0);
            lcd.print("ONCE AGAIN?" );
            digitalWrite(6, HIGH);
            lcd.setCursor(0, 1);
            lcd.print("CLICK!");
            delay(1500);

            
     
            boolean state= 0;
            if (digitalRead(7) == HIGH)
            {
            delay(20);
            state= !state;
            digitalWrite(7, state);
            while (digitalRead(7) == HIGH);
            delay(20);
            digitalWrite(6, LOW);
            resetFunc();
            }
         
          

        }
       
}

  }

}

Some suggestions: 1. Use CTRL-T to autoformat the code and make it a little clearer. 2. Use INPUT_PULLUP on your inputs so the pins aren't floating. 3. Give the pins names instead of just using numbers throughout, it's hard to follow what it does now without scrolling up & down in the code to see what the numbers are supposed to be. 4. Put some comments in to give an idea of what it's supposed to be doing.

It's really hard to read that code with the way it's formatted, but I suspect that when you cut your wire you leave those pins floating. Tie a 10K resistor from the analog pins to ground so that when you cut the wire the pin will read 0.

Stop using analogRead() use digitalRead to check if the wire is still connected.

analogRead will never give you 1023 when used in this way!.

Mark

Or use the internal pullups and look for them to go high when the wire is cut, and drive the outputs low accordingly.

Ok, sure. I'll format code next time. Thanks for your answers. I'll try them and tell how it worked. Thanks!

Ok, so once again with much better format, defined pins and comments :slight_smile:

Program still doesn’t work, even with pulled up and pulled down resistors. I left pulled up version.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

#define win_ledPin 6
#define start_buttonPin 7
#define buzzerPin 8
#define wire1Pin 9
#define wire2Pin 10
#define wire1_inputPin A0
#define wire2_inputPin A1
#define red_ledPin 13


void setup()
{
  delay(5);
  pinMode(win_ledPin, OUTPUT);
  pinMode(start_buttonPin, INPUT_PULLUP);
  pinMode(buzzerPin, OUTPUT);
  pinMode(wire1Pin, OUTPUT);
  pinMode(wire2Pin, OUTPUT);
  pinMode(wire1_inputPin, INPUT_PULLUP); // tried also with pin pulled down to ground with 10k resistor - same story
  pinMode(wire2_inputPin, INPUT_PULLUP); // tried also with pin pulled down to ground with 10k resistor - same story
  pinMode(red_ledPin, OUTPUT);

  for (int a = A3; a < A6; a++) // this loop is just to define output pins to 3 leds
  {                                         // just for countown signal, like "ready, set, go"
    pinMode(a, OUTPUT);
  }

}
void(* resetFunc) (void) = 0;

void loop()
{
  digitalWrite(wire1Pin, LOW); // low because pulled up input pins
  digitalWrite(wire2Pin, LOW); // low because pulled up input pins

  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("READY?" );

  boolean state = 0;
  if (digitalRead(start_buttonPin) == HIGH) // this loop supports button pin
  {
    delay(30);
    state = !state;
    digitalWrite(start_buttonPin, state);
    while (digitalRead(start_buttonPin) == HIGH);
    delay(30);

  }
  {

    for (int a = A3; a < A6; a++) // like before - countdown before rest of program
    {
      digitalWrite(a, HIGH); // like before - countdown before rest of program
    }

    delay(750);
    for (int a = A3; a < A6; a++) // countdown before rest of program
    {
      digitalWrite(a, LOW);
      tone(buzzerPin, 200, 50);
      delay(750);
    }
    tone(buzzerPin, 2000, 400); // like before - countdown before rest of program

    // in this point disamling starts

    if (digitalRead(wire1_inputPin) == LOW && digitalRead(wire2_inputPin) == LOW)
    {

      lcd.setCursor(0, 0);
      lcd.print("DANGER");
      lcd.setCursor(0, 1);
      lcd.print(millis() / 100);
      digitalWrite(red_ledPin, HIGH);
      delay(200);
      digitalWrite(red_ledPin, LOW);
      delay(200);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("CUT THE WIRE");
      digitalWrite(red_ledPin, HIGH);
      delay(200);
      digitalWrite(red_ledPin, LOW);
      lcd.clear();
      delay(200);

      ///////////
      // and now problem appears
      // after few miliseconds program stops and comes back to very beginning
      ////////

    }
    else
    {
      if (digitalRead(wire1_inputPin) == LOW || digitalRead(wire2_inputPin) == LOW)

      {
        for (int a = A3; a < A6; a++) // warning signal, starts after cutting just one wire
        {
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("OOOPS...");
          digitalWrite(a, HIGH);
          delay(100);
          lcd.clear();
          digitalWrite(a, LOW);
          delay(100);
        }
        tone(buzzerPin, 200, 150); // warning signal, starts after cutting just one wire
      }

      else

      {
        lcd.clear();
        int score = millis() / 100;
        lcd.setCursor(0, 1);
        lcd.print("Good ");
        digitalWrite(win_ledPin, HIGH);
        delay(1700);
        lcd.clear();

        lcd.setCursor(0, 0);
        lcd.print("SCORE: " );
        lcd.setCursor(8, 0);
        lcd.print(score);
        delay(2500);
        lcd.clear();

        lcd.setCursor(0, 0);
        lcd.print("ONCE AGAIN?" );
        digitalWrite(win_ledPin, HIGH);
        lcd.setCursor(0, 1);
        lcd.print("CLICK!");
        delay(1500);

        boolean state = 0; // after clicking button program starts again
        if (digitalRead(start_buttonPin) == HIGH)
        {
          delay(20);
          state = !state;
          digitalWrite(start_buttonPin, state);
          while (digitalRead(start_buttonPin) == HIGH);
          delay(20);
          digitalWrite(win_ledPin, LOW);
          resetFunc(); // program runs again
        }

      }

    }

  }

}

"Doesn't work"... please be more specific.

Please look at the comments in the code. There is explanation. Despite wires are still connected (not cutted) program stops and comes back to beginning.

Why bother connecting the other end of the wires to an output pin? The wire is really just a switch. One end to ground, and the other to any pin with an input pull up enabled.

lt helps to simplify things as much as possible.

when you do this check:

    if (digitalRead(wire1_inputPin) == LOW && digitalRead(wire2_inputPin) == LOW)

It happens over and over and over because the else isn't true until a wire is cut. So of course, you get the intro sequence over and over and over...

About connecting wires just to ground - very good point.

I think I understand this check, but the problem is the program comes back to start point. Not to the beggining of this loop/check -cause it would be perfect - but to the beginning of whole program ( to "ready?" printed on lcd). Do you understand my point? Thanks :)

aarg: Why bother connecting the other end of the wires to an output pin? The wire is really just a switch. One end to ground, and the other to any pin with an input pull up enabled.

Because he's making a bomb simulator. You have to pull out the correct jumper lead or boom.

OP: when an analog input doesn't have anything attacked to it, it 'floats' - the value you read off it is a random number. It won't reliably tell you yes/no there is a wire connected.

Ready? prints again because it is at the beginning of the loop. The program runs, checks if programmed conditions are met and then starts over. That is how void loop() works.

PaulMurrayCbr: Because he's making a bomb simulator. You have to pull out the correct jumper lead or boom.

It's the red one, oh wait, it's a trap, NOT the re.....

@JimboZA: Take off those amber sunglasses! :astonished:

@Frostline - THANKS!! That was it. I knew that it is something simple. After first check program cames back to beggining of the void loop. Now i put unnecesarry lines in the setup part and it’s working vell :slight_smile:

@JimboZA - exactly. But now there is a joke that you have to cut all two wires but you cannot loose. I think I will add some randomness or sth in the code in the future nd it will be much more interesting. Maybe some coundown timer…

@aarg - I left version with output wire pins to keep wires near the arduino board, but of course they can be attached to the ground as well.

thanks all who have replied for your help!

Have A Nice Day!

I’m putting correct version of the code - maybe you’ll do sth like this or even better :wink:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

#define win_ledPin 6
#define start_buttonPin 7
#define buzzerPin 8
#define wire1Pin 9
#define wire2Pin 10
#define wire1_inputPin A0
#define wire2_inputPin A1
#define red_ledPin 13

void(* resetFunc) (void) = 0;
void setup()
{
  delay(5);
  pinMode(win_ledPin, OUTPUT);
  pinMode(start_buttonPin, INPUT_PULLUP);
  pinMode(buzzerPin, OUTPUT);
  pinMode(wire1Pin, OUTPUT);
  pinMode(wire2Pin, OUTPUT);
  pinMode(wire1_inputPin, INPUT_PULLUP); // tried also with pin pulled down to ground with 10k resistor - same story
  pinMode(wire2_inputPin, INPUT_PULLUP); // tried also with pin pulled down to ground with 10k resistor - same story
  pinMode(red_ledPin, OUTPUT);

  for (int a = A3; a < A6; a++) // this loop is just to define output pins to 3 leds 
  {                                          // just for countown signal, like "ready, set, go"
    pinMode(a, OUTPUT);
  }

  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("READY?" );

  boolean state = 0;
  if (digitalRead(start_buttonPin) == HIGH) // this loop supports button pin
  {
    delay(30);
    state = !state;
    digitalWrite(start_buttonPin, state);
    while (digitalRead(start_buttonPin) == HIGH);
    delay(30);

  }
  {

    for (int a = A3; a < A6; a++) // like before - countdown before rest of program
    {
      digitalWrite(a, HIGH); // like before - countdown before rest of program
    }

    delay(750);
    for (int a = A3; a < A6; a++) // countdown before rest of program
    {
      digitalWrite(a, LOW);
      tone(buzzerPin, 200, 50);
      delay(750);
    }
    tone(buzzerPin, 2000, 400); // like before - countdown before rest of program
  }
}
void loop()
{

  digitalWrite(wire1Pin, LOW); // low because pulled up input pins
  digitalWrite(wire2Pin, LOW); // low because pulled up input pins
  if (digitalRead(wire1_inputPin) == LOW && digitalRead(wire2_inputPin) == LOW)
  {

    lcd.setCursor(0, 0);
    lcd.print("DANGER");
    lcd.setCursor(0, 1);
    lcd.print(millis() / 100);
    digitalWrite(red_ledPin, HIGH);
    delay(200);
    digitalWrite(red_ledPin, LOW);
    delay(200);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("CUT THE WIRE");
    digitalWrite(red_ledPin, HIGH);
    delay(200);
    digitalWrite(red_ledPin, LOW);
    lcd.clear();
    delay(200);


  }
  else
  {
    if (digitalRead(wire1_inputPin) == LOW || digitalRead(wire2_inputPin) == LOW)

    {
      for (int a = A3; a < A6; a++) // warning signal, starts after cutting just one wire
      {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("OOOPS...");
        digitalWrite(a, HIGH);
        delay(100);
        lcd.clear();
        digitalWrite(a, LOW);
        delay(100);
      }
      tone(buzzerPin, 200, 150); // warning signal, starts after cutting just one wire
    }

    else

    {
      lcd.clear();
      int score = millis() / 100;
      lcd.setCursor(0, 1);
      lcd.print("NICE WORK 007 ");
      digitalWrite(win_ledPin, HIGH);
      delay(1700);
      lcd.clear();

      lcd.setCursor(0, 0);
      lcd.print("SCORE: " );
      lcd.setCursor(8, 0);
      lcd.print(score);
      delay(2500);
      lcd.clear();

      lcd.setCursor(0, 0);
      lcd.print("ONCE AGAIN?" );
      digitalWrite(win_ledPin, HIGH);
      lcd.setCursor(0, 1);
      lcd.print("CLICK!");
      delay(1500);

      boolean state = 0; // after clicking button program starts again
      if (digitalRead(start_buttonPin) == HIGH)
      {
        delay(20);
        state = !state;
        digitalWrite(start_buttonPin, state);
        while (digitalRead(start_buttonPin) == HIGH);
        delay(20);
        digitalWrite(win_ledPin, LOW);
        resetFunc(); // program runs again
      }

    }

  }

}