1

I was working on a school project in where i need to analog read form A0 and test if my value is bigger than some boundary (500 for example) to return 0 or 1 well with a simple code in an infinity loop this will keep returning 0000000 and 1111111 for infinity but need 1 only once when my analog value passes from 900 to 300 for example and I dont need once when It stays at 300. and the same problem for 0 : I need 0 only once when it passes from 300 to 900 and not 0 from staying at 900 thanks for help in advance

1

Store the current read value AND the previous value and check if a border has been crossed.

E.g.

if Previous value < 500 AND current value >= 500
   Going from 0 to 1 -> return/send 1
else if Previous value >= 500 AND current value < 500
   Going from 1 to 0 -> return/send 0

However, to make it more complicated, you might have debouncing effects, e.g. what if the value moves for some time around 500 to go to 200... It will still result in possibly many 0 and 1 boundary changes.

You can fix this easily by using two different value: by checking going lower < 490 and going higher > 510 (or any other appropriate value. Or use a debounce mechanism where it should be below or above the 500 value for a minimum (but short) amount of time.

0

What you are talking about is threshold with hysteresis. It's basically how the digital inputs work using schmit triggers.

+------+ 1023
| HIGH |
+------+ 900
| DEAD |
+------+ 300
| LOW  |
+------+ 0

What you want to do needs to be done in two distinct sections:

  1. Detect the incoming analog level, and
  2. Control and record the status.

The first part is simple enough:

int val = analogRead(0);
if (val > 900) {
    // High
} else if (val < 300) {
    // Low
}

With that you have two blocks of code that are valid for the two bands you are interested in. That is effectively like a button now - you have a "HIGH" and a "LOW" signal, just like digitalRead() gives you for a digital input. Now you just need to couple that with some standard button toggling code. That is - only change the status if the status actually should change. And that's simple enough. For example:

if (newStatus != oldStatus) {
    oldStatus = newStatus;
    // Do something just once
}

To expand that concept out, you get:

static bool enabled = false; // This is the saved status

int val = analogRead(0); // The incoming value

if (val > 900) { // A HIGH signal.
    if (!enabled) { // It's not already recorded as HIGH
        enabled = true; // Record it as a HIGH
        Serial.println("1");
    }
} else if (val < 300) { // A LOW signal
    if (enabled) { // It's currently HIGH, not LOW...
        enabled = false;
        Serial.println("0");
    }
}

And there you have it. If it goes HIGH and it's not recorded as HIGH then it's a new HIGH. If it goes low and it's not recorded as LOW then it's a new LOW.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.