pushing my buttons...

Having put some code together and surprisingly got it working, there's acouple of things that I'm still confused about;

When I say 'working', the last unplanned thing that is happening is the code skips merrily past my test of a button being pressed;

buttonPin ==HIGH,

edited from buttonPin =HIGH,

and climbs back to the "VOID loop () { " like a homesick angel. I'm expecting it to wait until I press the button, then move on throught the do-something-interesting-loop.

{
snipped out some other loops
     {
     if (buttonPin ==HIGH)
        {
         Serial.println(buttonPin); 

         //do something interesting

        Serial.println("something interesting was done");
        }
     }
}

While trying to figure out what was going on with this, I used;

Serial.println(buttonPin)

...to see what is happening with the button, I was expecting to see either HIGH or LOW, even 0 or 1 perhaps...
but what I'm seeing is;

7

(7 is the pin the button is connected to via either pull up or pull down resistor, I've tried both)

I've also run a bare bones button sketch and the same thing happens, also happens on a different pin if you look at an led pin

Serial.println(ledpin)

Is the displaying of the pin number just symbolic of HIGH or is there a way of getting a more meaningfull answer to help in hunting down the reason for the homesick angel bug?

thanks.

if (buttonPin =HIGH)

should probably be

if (buttonPin==HIGH)

thank you kindly stimmer,

oh pooh :-[ of course it should.

there's still the "homesick angel" problem, but that wasn't helping any!

Clearly time to get some sleep and look at it in the morning when I might be able to see what's in front of me!

msa4,

I think what you want is:

if (digitalRead(buttonPin) == HIGH)

and

Serial.println(digitalRead(buttonPin));

Regards,

-Mike

Well with any physical switch one first has to pay attention to it's wiring and set-up. Are you using a external pull-down resistor to force the input to a LOW when the switch is not pressed? If not you will get random readings from the pin as the pin is electrically 'floating'.

The easiest way to interface a switch (without requiring an external resistor) is to wire the switch between ground and the desired input pin and then enable the internal pull-up for that pin. Then in you logic you must then treat a reading of a LOW as 'switch pressed' and reading a HIGH when not pressed.

Another thing to keep in mind when reading switches is 'contact bounce'. As the switch first makes or breaks contact there will be many HIGH/LOW transitions rather then one clean transition. There are software libraries to help 'filter' these bounces so that your software logic only sees single clean transitions. This may not be a problem with simple software routines but more important when 'counting' switch presses or other higher speed switch contact applications.

Lefty

Thanks for all the help guys!

@Mike murdock
Thanks mike, now comes back as

1

I did find this in another thread perhaps someone can tell me if it's relevant to this question?

RuggedCircuits wrote on 17.06.2009 at 22:12:24:
Minor gotcha with that implementation because AFAIK the C language does not guarantee that expressions return 1 for "true" (they can return any non-zero value).

alphabeta wrote
This might be the case, but Arduino is c++, yes?

In c++ I'm pretty sure a true will be explicitly promoted to the integral value 1.

snip
coding badly wrote
Yup (assuming the compiler is conformant). From conv...

4 An rvalue of type bool can be converted to an rvalue of type int, with
false becoming zero and true becoming one.

4 If the destination type is bool, see conv.bool. If the source type
is bool, the value false is converted to zero and the value true is
converted to one.

@retrolefty
Thanks Lefty, yep, I found this in another thread;

I'm using the momentary switch circuit, or at least I think I am! lol, After last night, I'll go check it again with this;

int ledPin = 28; // choose the pin for the LED
int inPin = 7;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
Serial.begin(9600);
}

void loop(){
  val = digitalRead(inPin);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
    digitalWrite(ledPin, LOW);  // turn LED OFF
   //Serial.println(inPin);
   Serial.println(digitalRead(ledPin));

  } else {
    digitalWrite(ledPin, HIGH);  // turn LED ON
    Serial.println(digitalRead(ledPin)); 
    delay (500);
  }
}

Yep, the switch worked as expected although I've now changed to a 10k resistor from the 4k7 I had actually used in there.

Yes, I was thinking it wouldn't be a problem here as I only need to register "any press" rather than having to count them - the simple routine in your message. If the wiring and my high / low code checkout ok, then I'll go with the button library just to be sure.

At the moment, its the code that looks suspect, or at least the positioning of the button press loop...

Firstly, C and C++ are the same with regards to "truth" and "falsehood." The if and while statements (among others) will assume truth for all values other than zero. The == equality operator will wait for the left and right sides to be evaluated, and evaluate the results as exactly 1 (TRUE) if the two sides came to the same value, 0 (FALSE) otherwise. It is a matter of style as to whether you say something like if (button == TRUE) or just if (button) when you know that button will come from some calculation that only results in 1 or 0. Arduino also provides similar HIGH and LOW constants which make more sense electronically.

Secondly, there is a big difference between "check if the button is pressed" and "wait until the user presses the button." If you want the latter, and don't mind stopping all other activity until it happens, then use while (digitalRead(buttonPin) == LOW) { ; } (assuming LOW represents the not-pressed condition, electrically, such as with a tie-down resistor). If you want other activities to continue, such as updating a display, then you should check the BlinkWithoutDelay example for inspiration on timeslicing.

Thanks for that Halley, most helpful!

My apologies, I was using both "check if the button is pressed" and "wait until the user presses the button." interchangably. There is a future development that might need both, but for the moment, I should have said

"wait until the user presses the button!"

I can stop all other activity with this project so will go with the while option.

The blink without delay sketch was very useful in the last project, also my very first project, of using the stopwatch sketch but modifying it to:

Measure thousanths of a second
Display to a 4 x 20 LCD display as the time increments
Remember both the last time and the "best time"

From digging around further, I have noticed that most sketches that have what I'll refer to as a "live" button seem to have used "Functions", and start the void loop with the button press loop... I'll see if I can find out some more about this.

Thanks again

scary though this thought is, it works! I've got my first sketch working that wasn't a mod of someone elses sketch and learned a lot doing it! ;D

Thanks for all the help I have received in this thread, the final bug was resolved with the help of Halleys suggestion of

while (digitalRead(buttonPin) == LOW) { ; }

There's a grin here that may need to be surgically removed!