Using analog inputs...

Hey everyone,

I know a little about the coding but not a huge amount so any help is great :)

I have the following code (slightly shortened for your viewing)

int analog15 = analogRead(A15);


void loop()
{

if (analog15 = LOW)
{
      myGLCD.print("Door 1 Closed", 200, 50, 0);
}
if (analog15 = HIGH)
{
      myGLCD.print("Door 1 Opened", 200, 50, 0);
}
}

Obviously this is outputting to a display, which uses my digital pins. The output seems to be HIGH as the message says "opened" which I expected. I have put a wire to GND and a wire to pin 15 to make it low, but that doesnt seem to work.. I am thinking that as they are analog in, I can't use High and Low?

Thanks in advance :)

Okay, ive changed that now so its

if (analog15 == LOW)
{
      myGLCD.print("Door 1 Opened", 200, 50, 0);
}
if (analog15 == HIGH)
{
      myGLCD.print("Door 1 Closed", 200, 50, 0);
}

But I still cant seem to change its state... so I need to declare it as an input?

LOW = 0, HIGH = 1. So, no, they do not mean 0 volts and 5 (or 3.3) volts. I don't know what Arduino that you are using, but A15 may (depending on the processor) be use as a digital pin and you would use digitalRead(A15) for high and low. Are you reading a switch? Post a schematic so we know what you are trying to do. This is looking like an XY question.

Its an arduino Mega 2560 (sorry)

|500x308

How do I got about makeing the analogs read digital in. Bit hesitant to try it myself incase I start changing the display pins..

Okay, so I have:

int a0 = digitalRead(A0);

void loop()

if (a0 == LOW)
{
      myGLCD.print("Door 1 Opened", 200, 50, 0);
}
if (a0 == HIGH)
{
      myGLCD.print("Door 1 Closed", 200, 50, 0);
}

But still struggling to get it to change state?

Here is the proper way to wire a switch. In your post the switch has no pull down so is floating and could read anything. Wire the switch between ground and the input. Enable the internal pullup resistor with pinMode(A15, INPUT_PULLUP). The pin will read HIGH when the switch is open and LOW when the switch is closed.digin.jpg

int a0 = digitalRead(A0);

void loop()

if (a0 == LOW)
{
      myGLCD.print("Door 1 Opened", 200, 50, 0);
}
if (a0 == HIGH)
{
      myGLCD.print("Door 1 Closed", 200, 50, 0);
}

You are reading the switch outside of loop. It will be read only once (if at all) then the loop just goes into an endless loop with that one initial value

Move the digitalRead(A0);

inside (after) the void loop() and add braces "{}" to bound the void loop() code

void loop()  
{
  int a0 = digitalRead(A0);

  if (a0 == LOW)
    {
      myGLCD.print("Door 1 Opened", 200, 50, 0);
    }
  if (a0 == HIGH)
    {
      myGLCD.print("Door 1 Closed", 200, 50, 0);
    }

groundfungus:
Here is the proper way to wire a switch. In your post the switch has no pull down so is floating and could read anything. Wire the switch between ground and the input. Enable the internal pullup resistor with pinMode(A15, INPUT_PULLUP). The pin will read HIGH when the switch is open and LOW when the switch is closed.digin.jpg

Okay, I’ll give that a try, but in the meantime, there’s no reason why my code wouldn’t work, right?

(

void loop()
{
int a0 = digitalRead(A0);

if (a0 == LOW)
{
      myGLCD.print("Door 1 Opened", 200, 50, 0);
}
if (a0 == HIGH)
{
      myGLCD.print("Door 1 Closed", 200, 50, 0);
}

)

Working

Thanks everyone :)