State machine..

I'm making a small thing where I needed to detect two states. Once when my hand is above the device and once when it is taken off. In both the cases I needed to output a single signal(a serial println() for example here) only once..

So here is the part of the function I'm writing:

boolean handState = true;

void handDetector() {
  hand = digitalRead(irPin);  // it is a ir obstacle sensor 

  if (hand != handState) { // if the ir sensor's digital output changes state
    if (hand == LOW) { // if it detects hand is on top
      counter = counter+1;
      if(counter >= 1){ // to prohibit multiple outputs
        Serial.println("Dock Up");
        counter = 0;
      }
    }
    else{ //if hand is removed
      counter = counter+1;
      if(counter > 1){
        Serial.println("Dock Down");
        counter = 0; 
      }
    }
    handState = hand;
  }
}

It is supposed to send "DOCK UP" once when I put up my hand on top of the device and then when I remove my hand it should spit "DOCK DOWN".. But it is spitting twice "DOCK DOWN" when I remove my hand and doesn't spit anything when I put my hand on top of the IR sensor.

Help here ..

  if (hand != handState) { // if the ir sensor's digital output changes state

These variable names just do not appear to have any relationship. currentHandState and previousHandState do look like they have a relationship. Using names that make sense makes for much easier to read code.

      counter = counter+1;

Have you ever wondered why the language is called C++, rather than C=C+1?

Your logic is flawed, apparently, but there are some many red herrings in the code that it is hard to say how.

When hand (currentHandState) is not the same as handState (previousHandState), you have either just covered the sensor or just uncovered the sensor.

You should be setting handState (previousHandState) to hand (currentHandState) at the end of loop(), UNCONDITIONALLY. Try moving the assignment statement out of the if(hand != handState) block.

At a minimum, you are comparing a boolean with an int.

Change

boolean handState = true;

to

int handState = LOW;

And please post the entire sketch, since often the problems are not where the programmer thinks they are. Have you set the pin as an input for example? Is it wired correctly? etc.

dattasaurabh82: I'm making a small thing where I needed to detect two states. Once when my hand is above the device and once when it is taken off. In both the cases I needed to output a single signal(a serial println() for example here) only once..

What about bouncing signals?

A sensor going from LOW to HIGH might give a bouncing signal like: LOW - LOW - LOW - LOW - HIGH - LOW - HIGH - LOW - HIGH - HIGH - HIGH - HIGH - HIGH - HIGH - HIGH

A sensor going from HIGH to LOW might give a bouncing signal like: HIGH - HIGH - HIGH - HIGH - LOW - LOW - LOW - HIGH - LOW - LOW - LOW - LOW - LOW - LOW

Or something like that.

Is your sensor providing a bouncing signal when changing state that needs "software debouncing" by special timings while reading the signal?

arduinodlb: At a minimum, you are comparing a boolean with an int.

Change

boolean handState = true;

to

int handState = LOW;

It is perfectly reasonable to compare two different types of variables. Using boolean is legal, even if it is confusing. Using byte or uint8_t would be better. Using int to hold the values 0 or 1 is a waste of SRAM.

PaulS: It is perfectly reasonable to compare two different types of variables. Using boolean is legal, even if it is confusing. Using byte or uint8_t would be better. Using int to hold the values 0 or 1 is a waste of SRAM.

I didn't say it wouldn't work, I said you shouldn't do it because it is meaningless and can lead to hidden issues. No, it is not reasonable to compare a boolean to an int. Sure, use byte or uint_8.

It is perfectly reasonable if you know that in C/C++ logical comparisons are done in the following way: False if all the bits are zero. True if any bit is 1.

So you can compare any integral value.

since often the problems are the problems are almost always not where the programmer thinks they are.

There...fixed.

KeithRB: It is perfectly reasonable if you know that in C/C++ logical comparisons are done in the following way: False if all the bits are zero. True if any bit is 1.

So you can compare any integral value.

No it's not reasonable at all, and people here should not be encouraging this sort of thinking. You should not be thinking about the underlying implementation and "what I can get away with" (yes, it's good to understand the implementation, etc. etc, etc. ). You should be thinking that one is a boolean, and the other is not. Comparing them is meaningless. The fact that it happens to work in C++ is irrelevant.

dattasaurabh82: I'm making a small thing where I needed to detect two states. Once when my hand is above the device and once when it is taken off. In both the cases I needed to output a single signal(a serial println() for example here) only once..

So here is the part of the function I'm writing:

boolean handState = true;

void handDetector() {   hand = digitalRead(irPin);  // it is a ir obstacle sensor

  if (hand != handState) { // if the ir sensor's digital output changes state     if (hand == LOW) { // if it detects hand is on top       counter = counter+1;       if(counter >= 1){ // to prohibit multiple outputs         Serial.println("Dock Up");         counter = 0;       }     }     else{ //if hand is removed       counter = counter+1;       if(counter > 1){         Serial.println("Dock Down");         counter = 0;       }     }     handState = hand;   } }





It is supposed to send "DOCK UP" once when I put up my hand on top of the device
and then when I remove my hand it should spit "DOCK DOWN"..
But it is spitting twice "DOCK DOWN" when I remove my hand and doesn't spit anything when I put my hand on top of the IR sensor. 

Help here ..

dattasaurabh82:
I'm making a small thing where I needed to detect two states. Once when my hand is above the device and once when it is taken off. In both the cases I needed to output a single signal(a serial println() for example here) only once.. 

So here is the part of the function I'm writing: 


boolean handState = true;

void handDetector() {   hand = digitalRead(irPin);  // it is a ir obstacle sensor

  if (hand != handState) { // if the ir sensor's digital output changes state     if (hand == LOW) { // if it detects hand is on top       counter = counter+1;       if(counter >= 1){ // to prohibit multiple outputs         Serial.println("Dock Up");         counter = 0;       }     }     else{ //if hand is removed       counter = counter+1;       if(counter > 1){         Serial.println("Dock Down");         counter = 0;       }     }     handState = hand;   } }





It is supposed to send "DOCK UP" once when I put up my hand on top of the device
and then when I remove my hand it should spit "DOCK DOWN"..
But it is spitting twice "DOCK DOWN" when I remove my hand and doesn't spit anything when I put my hand on top of the IR sensor. 

Help here ..

dattasaurabh82:
I'm making a small thing where I needed to detect two states. Once when my hand is above the device and once when it is taken off. In both the cases I needed to output a single signal(a serial println() for example here) only once.. 

So here is the part of the function I'm writing: 


boolean handState = true;

void handDetector() {   hand = digitalRead(irPin);  // it is a ir obstacle sensor

  if (hand != handState) { // if the ir sensor's digital output changes state     if (hand == LOW) { // if it detects hand is on top       counter = counter+1;       if(counter >= 1){ // to prohibit multiple outputs         Serial.println("Dock Up");         counter = 0;       }     }     else{ //if hand is removed       counter = counter+1;       if(counter > 1){         Serial.println("Dock Down");         counter = 0;       }     }     handState = hand;   } }





It is supposed to send "DOCK UP" once when I put up my hand on top of the device
and then when I remove my hand it should spit "DOCK DOWN"..
But it is spitting twice "DOCK DOWN" when I remove my hand and doesn't spit anything when I put my hand on top of the IR sensor. 

Help here ..

If you use your mouse and position it on the first bracket after if (hand != handState) { HERE

you will note the closing bracket OUTSIDE your code flow , at the end of the function.

Is that what you want?

Maybe you just want to check the "hand" output by itself first, than compare the state.

Also you should decide if the function will work on global variables or locals.

Generally functions get parameters passed to them and return something (global). but you can change that after you get the flow sorted out.

Keep at it , stuff like this is what makes snippets so popular here!

Good luck.

I would also wait if the signal is still at present after the pin change.

A "state machine" is defined by states (hence the name) and transitions between the states (as result of actions or events). Take a look at this example of state machine Arduino sketch: http://timewitharduino.blogspot.ca/2014/06/wise-clock-chronometer-completed.html

I am not talking about "implementations" - which has a specific meaning in C/C++ - I am talking about Standards. In C/C++ a boolean is not a magical type. It is one of the integral types.

The bool type participates in integral promotions. An r-value of type bool can be converted to an r-value of type int, with false becoming zero and true becoming one. As a distinct type, bool participates in overload resolution.

https://msdn.microsoft.com/en-us/library/tf4dy80a.aspx

KeithRB: I am not talking about "implementations" - which has a specific meaning in C/C++ - I am talking about Standards. In C/C++ a boolean is not a magical type. It is one of the integral types.

https://msdn.microsoft.com/en-us/library/tf4dy80a.aspx

Use bits for bools. You can not only save space but you can put related bools in the same unsigned vars and if you arrange them right, evaluate the lot in one compare instruction.

You should see how that works with PC videocards via blit operations!

You should see how that works with PC videocards via blit operations!

or an Amiga! https://en.wikipedia.org/wiki/Amiga_Advanced_Graphics_Architecture

KeithRB: or an Amiga! https://en.wikipedia.org/wiki/Amiga_Advanced_Graphics_Architecture

I first experimented with block-bit-logic on my Amiga1000 in 87 or 88. They were amazing for the time but that was IIRC 16-bit color 640x400. If it hadn't been for Jack Trample (yeah yeah Tramiel) we might have seen more!

I got more out of my Voodoo3 card 10 years later. :o) Nowadays there's mobos with more onboard.

I just never found paying work I could use that much in and a contract would show up before I got far.

GoForSmoke: Use bits for bools. You can not only save space but you can put related bools in the same unsigned vars and if you arrange them right, evaluate the lot in one compare instruction.

You should see how that works with PC videocards via blit operations!

Unless you're hard-up for space, use boolean for booleans, as it makes the code easier to follow. If you really need to save space, this is a good suggestion, but booleans make very good booleans. Embedded programmers can have a tendency to optimise too early, but there is a complexity cost to optimising (yes, I am an embedded programmer and have been for way too long).

Arduino UNO has 2K RAM.

Yes I can waste a lot of bytes while doing trivial exercises, even use C Strings some.

But after a while it becomes habit and then “the way to do that”.
I am sure that a lot of people who won’t learn string.h functions are solid in the use of String and likely don’t know or care how it operates as long as they don’t have to learn something else.
So they learn to write fat code and get less out of hardware, buy a bigger chip right?

Usually I do give flags their own bytes but come on, how do you store led states for an 8x8 led array?

And here’s another, I use 2 bits to track states for one pin and determine when change has occurred. One is the state when last checked (bit 1) and the other is the now state (bit 0). Before every read I shift the byte << 1 then mask off the top bits with & 3 and add the read which is always 0 or 1. Every read, it’s new.
switch (pin states) // 1 check for all
case 0 : LOW - LOW, no state change
case 1 : LOW - HIGH, state change
case 2 : HIGH - LOW, state change
case 3 : HIGH - HIGH, no state change

It’s fast in terms of very small actions here and there during run time make a picture (this one is very small) that can be tested quickly as opposed to 2 byte vars, prev and data
if ( prev < data ) LOW - HIGH, state change // 1 check
elseif ( prev > data ) HIGH - LOW, state change // +1 check
else no state change. // another step.

If I want to separate it more that way, I need steps already in that 2-bit method.

how do you store led states for an 8x8 led array?

I do it as 8 byte array. 64 bits total for 64 LEDs.