Input Pin Always Reads HIGH

I have written a code for something to change its display depending on what goes into an input pin. However, no matter what the input is (have tried 5 V, left empty, and GND), the display shows 5 (the sign for HIGH).

Any help that could be given would be greatly appreciated.

#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
//#include <TinyWireM.h> // Enable this line if using Adafruit Trinket, Gemma, etc.

#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

int inPin=12;
int val=0;
int time=0;
// if input is LOW, then start timer

Adafruit_7segment matrix = Adafruit_7segment();
void setup()
{ pinMode(inPin, INPUT);

#ifndef __AVR_ATtiny85__
  Serial.println("7 Segment Backpack Test");

void loop() {
while (val=LOW) {

 while (val=HIGH) {
while (val=LOW) {

Same withe the while (val = HIGH)

while(val == LOW)

will send 2 while input is held low.


Next part of the question:

I want the microcontroller to check periodically what state the input is at. I thought the while loop would cover that, but changing the value from high to low doesn't cause the display to change.

The arduino is running a loop already, just check the pin and do your response and exit, letting it run again right away - there is no reason to try to re-implement the loop, it's already looping.

So an "if" condition instead of a "while"? I don't quite understand.

Your code checks the input each time through the loop(). How is your input wired? Is it pulled to Vcc or ground until changed, that is, never floating?


Is there an option when reading a pin for no input? Current configuration for my design (this was the testing the basic code phase)

A=5V B=Switch C= Pin


if the switch was open, then C would register as closed. However, when the pin's wire's other side is going to nothing specific (as in the case of an open switch), it simply flips back and forth between 2 and 5 on the display.

So, yes, floating would be the better term for the switch is open case.

Floating pins are bad.

In your current configuration, you could tie C to ground as well as the pin via a 10K resistor. Now the pin would see ground unless the switch is made in which case it sees the 5V.

Easier is to tie A to ground and enable the internal pullup resistor. When you do that a made switch reads LOW and an open switch reads HIGH, so you kind of have to reverse your thinking a little. But it makes for much nicer circuits.

Here is a way to wire your switch so that it never floats. Use pinMode(pin, INPUT_PULLUP) to enable the internal pullup resistor.