Wait until

I am stumped with a very simple project, just cant catch the logic on it.
all I want to do is wait till a digital input goes high, then Serial.println("High"); 1 time
then wait till it goes back low and send Serial.println("Low"); 1 time then restart the loop waiting until input goes high again. Can someone please give me a code sample so I can visually see where this is all going wrong.

int inPin = 7; // pushbutton connected to digital pin 7
int val = 0; // variable to store the read value

void setup()
{
pinMode(inPin, INPUT); // sets the digital pin 7 as input
// initialize serial communications:
Serial.begin(9600);
}

void loop()
{

val = digitalRead(inPin); // read the input pin

do
{delay(1);} while (inPin == LOW);
val = digitalRead(inPin); // read the input pin
{delay(1);} while (inPin == High);

// sets the LED to the button's value
Serial.println(val);
delay(1); // delay in between reads for stability

}

do
{delay(1);} while (inPin == LOW);
  val = digitalRead(inPin);   // read the input pin
{delay(1);} while (inPin == High);

You used 'inPin' when you should have used 'val'. inPin can never be HIGH or LOW, because it's 7.

Maybe look at http://www.arduino.cc/en/Tutorial/StateChangeDetection.

Confused Tried this but the loop totally ignores the do while's
int inPin = 7; // pushbutton connected to digital pin 7
int val = 0; // variable to store the read value

void setup()
{
pinMode(inPin, INPUT); // sets the digital pin 7 as input
// initialize serial communications:
Serial.begin(9600);
}

void loop()
{
val = digitalRead(inPin); // read the input pin
do
{delay(1);Serial.println("Cold");} while (inPin == LOW);

do
{delay(1);Serial.println("Initial Hot"); val = 1;} while (inPin == HIGH and val == 0);

do
{delay(1);Serial.println("Hot");} while (inPin == HIGH);

Serial.println("Cold");
val = 0;
delay(100); // delay in between reads for stability
}

  1. Please post your code between code tags, not inline in the post. Press the </> button in the post or “Reply” window, or type them manually like this:-
[code]Place your code here[/code]
It will appear in a block like this
  1. Place each statement on a separate line to make your code easier to read. Cramming it onto single lines as you’ve done still uses the same amount of program memory in the chip.

  2. Format your code correctly. This can be done by using “Ctrl T” in the IDE.

It’s not too late to edit and make these corrections. You’re much likely to get help if you make it as easy as possible for people to help you. :slight_smile:


And as guix said, “inPin can never be HIGH or LOW, because it’s 7”:-int inPin = 7;   // pushbutton connected to digital pin 7Didn’t you read his reply?

Hi brizey,

@guix and @OldSteve are telling you that inPin is seven because your use of inPin assumes that inPin will be the state of pin 7, but it is not. Unless you assign a different value to inPin, it will always be 7. You defined it because it is easier to deal with a name for the pin, than the number of the pin. For one, it's easier to remember if your name means something that will remind you what it does. And 2 - say you write 500 lines of code and reference inPin 25 times. Can you imagine how tedious it would be if you had to change every instance of "7" to a different number? By using a name, you need only change it once - in the definition.

You have already done the hard work of fixing this. The line "val = digitalRead(inPin);" puts the state of pin 7 (or as we are calling it now, inPin) into the variable "val". When you are checking on the state of inPin, you need to use val, not inPin.

So instead of this line:

{delay(1);Serial.println("Cold");} while (inPin == LOW);

use this one instead:

{delay(1);Serial.println("Cold");} while (val == LOW);

Then check every use of inPin and change it to val if appropriate.

A note about naming: the name val stands for value. It's great for saving space in your source code, but that practice leads to making 'write-only' code. Note that compiled code is the same size, no matter how long variable names are. I like to use descriptive names, such as valueOf_inPin. That way, when you read the code, you can understand what it is doing without needing too many comments.