Serial Monitor statements

Hello everyone,

i am new in programming, so sorry for this probably boring question.

For my project i read a analog sensor (IR) and send the data to the serial monitor.
But instead of having a never ending stream of data i want to have just the statements "high" and "low".
So, lets say the analog.read collects values from 0 to 1000, i want the serial monitor to print "low", when the values are <500 and "high, once they are above.
But i want the serial monitor to print the statement only when it changes from high to low or viceversa.
So instead of reading:
high
high
high
high
high
low
low
low

my output should become just:
high
low
high
low
and so on...

hmmm... I think it would be wise to create a boolean and to check if its still the same, right?
What should the sketch look like? Can anyone help?

Thanks and cheers,
Christopher

hmmm... I think it would be wise to create a boolean and to check if its still the same, right?

Something like that, yes. You need to Compare the current value to the threshold. If above, print the value (if the above flag is false), set the above flag to true, and set the below flag to false.

If below, print the value (if the below flag is false), set the below flag to true, and set the above flag to false.

What should the sketch look like?

I'm sure that you can figure it out. Or, you go first. Post your code.

Test code that has a toggle function.

//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(160);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

ok, here's the code. please dont laugh...

boolean above = true;
boolean below = false;

void setup() {
Serial.begin(9600);

}

void loop() {
int sensorValue = analogRead(A0);

if (sensorValue < 500)
{
if (above = true)
{
Serial.println("oh, i'm down!");
above = false;
below = true;
// here i try to "lock the door", once the line is printed. But it still doesn't work....
}
}
else
{
Serial.println("Oh, i'm Up!");
above = true;
below = false;

}
}

Please CTRL+T in the arduino IDE before copy-n-pasting code, as it makes the sketch easier to read.
Then use code tags in the forum editor when inserting code (button with # sign on it).

boolean above = true;
boolean below = false;


void setup() {
    Serial.begin(9600);
}


void loop() {
    int sensorValue = analogRead(A0);

    if (sensorValue < 500)
    {
        if (above = true)
        {
            Serial.println("oh, i'm down!");
            above = false;
            below = true;
            // here i try to "lock the door", once the line is printed. But it still doesn't work....
        }
    }
    else
    {
        Serial.println("Oh, i'm Up!");
        above = true;
        below = false;

    }
}

There's an error:

        if (above = true)

This will always be true, because you're not making a test, but an assignment. You need two equal signs:

        if (above == true)

The trick is to use a "prevState" variable and check that against the current analog level. If the previous reading was low and the current one is high, you print "From low to high". Same story if the previous was high and the current one is low.

HTH

Thank you, i'll try that.

Let us know the results :slight_smile:

Hello,
if someone is interested reading:
i tried that prevState proposal by tuxdino and tadaaah! Seems to work. Thank you :slight_smile:

  int prevState = 0;
  int nowState = 0;
  // i'm two int variables
  
void setup() {
  Serial.begin(9600);

}

void loop() {

  int sensorValue = analogRead(A0);
  prevState = sensorValue;
  // prevState gets the actual number
  delay (1);
  sensorValue = analogRead(A0);
  // read once again
  nowState = sensorValue;
  // nowState is even more actal!!!
  if (nowState>500 && prevState<500)
    {
       Serial.println("UP");
            }
  if (nowState<500 && prevState>500)
      {
         Serial.println("DOWN");
            }
}

Only thing i don't understand:
Every now and then the monitor doesn't change like tic tac between the two statements, but prints the same statement 'UP' or 'DOWN' twice - how can this happen? Once the analog signal goes from up to down it has to go up again to go down, right? This is a mystery to me... Might have something to do with debouncing?
Anyway, thanks everybody for reading and commenting. It helped me a lot.
(The next thing i want to do is count the time between the changes and print it insdead of printing UP or DOWN.)

You're almost there... But why did you duplicate analog value reading ? Why two "prevState" variables ? That will just make the code less clear and more error-prone.

ok, right.
Here's a smaller version of the code. Looks easier to me too.
(right now i don't have a board to try, i'll check it out this evening)
Again, thanks for commenting!
Regards, Christopher

  /*
AnalogRead IR 02.12.2012
 */
  
int prevState = 0;
  
void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(A0);
  
  prevState = sensorValue;
  
  delay (1);
  sensorValue = analogRead(A0);

  if (sensorValue>500 && prevState<500)
    {
       Serial.println("UP");
            }
  if (sensorValue<500 && prevState>500)
      {
         Serial.println("DOWN");
            }
}

Why do you read the analog value two times with a delay(1) in between ?

Also, prevValue = currValue must be placed at the end of the function, otherwise you'll be comparing two variables containing the exact same data.

PS: why do you keep on posting badly indented code ? Is it so hard to press CTRL+T ?

Ok, sorry for the ugly code. I'll keep that in mind...

The plan was:
1st: read the analog value
2nd: store the analog value in the variable "prevState"
(the delay was set here to make shure i don't get the same result from the sensor twice... )
3rd: read the analog value again and compare it with the "prevSate"

It seems to work (still not solved: the "double" UPs and DOWNs).

So in this version i need two int variables, sensorValue and prevState to do the job.
But maybe i haven't quite understood the concept. Am i totally wrong?
Regards, Christopher

/*
AnalogRead IR 02.12.2012
 */

int prevState = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(A0);

  prevState = sensorValue;

  delay (1);
  sensorValue = analogRead(A0);

  if (sensorValue>500 && prevState<500)
  {
    Serial.println("UP");
  }
  if (sensorValue<500 && prevState>500)
  {
    Serial.println("DOWN");
  }
}

elvum:
The plan was:
1st: read the analog value
2nd: store the analog value in the variable "prevState"
(the delay was set here to make shure i don't get the same result from the sensor twice... )
3rd: read the analog value again and compare it with the "prevSate"

Instead of reading it twice, just read it once, and at the end of the loop, store the result into a static or global variable; you don't even need the delay because of some calculus principle that I can't remember about having to hit a value if you have a continuous function that crosses that value.

Try this

const int BELOW_THRES = 0;
const int ABOVE_THRES = 1;

int prevState = BELOW_THRES;

int thres = 500;


void setup() {
    Serial.begin(9600);
}


void loop() {
    int currValue = analogRead(A0);
    int currState;

    if (currValue > thres) {
        currState = ABOVE_THRES;
    }
    else {
        currState = BELOW_THRES;
    }

    if (prevState != currState) {
        if (currState == ABOVE_THRES) {
            Serial.println("UP");
        }
        else {
            Serial.println("DOWN");
        }
    }

    prevState = currState;
}

thanks, this one works excellent!
I must admit i'll have to spend some time to find out why....
Regards,
Christopher

One of the most obvious errors you made in your sketch (and I already have pointed it out in a previous answer) is the assignment to prevState. The whole point about prevState is to compare it with currState. If you write prevState = currState and then compare their values, you're doing it wrong. You have to do that assignment after comparing the two values.

I'm glad my code works for you. Feel free to ask if there's something else you don't understand. :slight_smile: