Printing only once in serial

Ive read a bit on it and searched but theres nothing that can help me. Just alot of bitterness ...

Anyways. I want to print to lcd whenever digitalRead senses anything.

So if digitalread(waterpump) turns on i want it to be printed. Then when digitalread senses it shuts off to print off. And i need to do a few on and offs. For different things

Instead of it just printing crazy fast a million ons. I also want to attach a time stamp to that on and off.

Basically i want to use my serial print to be a log so i can check that all my auto functions are working.

So how do i make serial.print just once and how do i do it for digital read high and low.

Hopefully that made sense..
Thanks

Well, since you didn't bother to post any code, it's hard to tell what you're doing wrong.

But, as a SWAG, I'd say you want to look in the IDE: File --> Examples --> 02.Digital --> StateChangeDetection

Not much to go on but I'll assume you're polling your digitalRead in which case set a flag when it turns on and print on. Next test, if it's on, check to see if the flag is set and if so, do nothing. Clear the flag and do a similar test for off.

awesome a starting point, thank you so much :slight_smile:

whats polling and flags?

like have you done this?
if so dont dont it again untell it switches?

my problem is i dont know now alot of the terms so finding the right questions to ask is kinda hard

so i found an example, i dont know how pseudo this is but im guessing theres a bool here? which im not to familiar with.

can someone do me a knowledge? it would be nice to have it broken down a bit so i can figure out what to learn

void loop()
{  
val = digitalRead(SwitchPin);   // read the input pin
if (val == 1 && pushed ==FALSE);

{
pushed = TRUE;
Serial.print("On");
}
}

You will then need to reset the flag at some point to enable it to print again.

You could follow with 

If (digitalRead(switchPin) == 0 && pushed == TRUE);
{
pushed = FALSE;
Serial.print ("off");
}

A bool is simply a variable that can indicate if something is true or false. It's like e.g. an int but can only hold 0 (false) or 1 (true).

The code that you provided is not pseudo code; if arranged correctly, it should work (providing the missing pieces like pin definitions and variable declarations and the setup() function are correct).

void loop()
{
  val = digitalRead(SwitchPin);   // read the input pin
  if (val == 1 && pushed == FALSE);
  {
    pushed = TRUE;
    Serial.print("On");
  }

  if (digitalRead(switchPin) == 0 && pushed == TRUE);
  {
    pushed = FALSE;
    Serial.print ("off");
  }
}

Notes:
1)
Be aware that switches and push buttons can bounce so you might get a few readings when you press or release the them.
2)
Input pins can give random results if they are not connected to a defined level. Read up on floating inputs.

so the value is the reading of the pin and what it equals, high or low,

and the rest is kinda like a flip flop?

if its high and not pressed print on, if its low and pressed print off

do i got it or am i backwards?

so im guessing the button needs a pull down?

and i have a debounce library, cause i hate millis

ok so i came up with this

and it just prints on off, over and over and over again

#include <Button.h>
Button button1(10);
void setup(){
  button1.begin();
Serial.begin(9600);

 
}

void loop()
{
    boolean val = 0;
   boolean pushed = 0;
  val = digitalRead(10);   // read the input pin
  if (val == 1 && pushed == false);
  {
    pushed = true;
    Serial.println("On");
  }

  if (digitalRead(10) == 0 && pushed == false);
  {
    pushed = false;
    Serial.println ("off");
  }
}

You got it; it’s like an SR flipflop.

And yes, you need a pull-down resistor. Or a pull-up (internal or external) and reverse the logic.

It’s fine to use a debounce library. You should however not hate millis(); I suspect that it is because you do not quite understand it.

jokefox:
ok so i came up with this

and it just prints on off, over and over and over again

#include <Button.h>

Button button1(10);
void setup(){
  button1.begin();
Serial.begin(9600);

}

void loop()
{
    boolean val = 0;
  boolean pushed = 0;
  val = digitalRead(10);  // read the input pin
  if (val == 1 && pushed == false);
  {
    pushed = true;
    Serial.println(“On”);
  }

if (digitalRead(10) == 0 && pushed == false);
  {
    pushed = false;
    Serial.println (“off”);
  }
}

Every time loop ends (and that’s thousands of times a second), it will forget the variables. That’s not a problem for val but a problem for pushed.

If you want the value of pushed to be remembered when loop ends, you need to make it static.

void loop()
{
  boolean val = 0;
  static boolean pushed = 0;
  ...
  ...
}

Or make it a global variable.

jokefox:
awesome a starting point, thank you so much :slight_smile:

whats polling and flags?

These words are not formal parts of a programming language, they name a couple of programming techniques.

Polling means to check something repeatedly at some sort of interval. Poling means your code is "pulling" values from some source of data. The opposite of polling is using an interrupt or (in higher level coding environments) event listeners, where something external to your code invokes the code you have written. Most arduino code involves polling input pins.

Flags means a boolean, yes/no variable. The flag is raised or lowered, the boolean variable is set to true or to false. Using flag kinda-sorta means that something will set a global or public boolean variable to true, and some other thing will notice that the flag is true, do something about it, then set that flag back to false again. A flag is a message from some thing to some other thing by way of a shared boolean variable that both these things can access.

In Arduino code, the 'something' and the 'something else' here are often a block of code and then that same block of code being run later at some other time. Or the 'something' and the 'something else' might be one function that reads the water level and some other function that turns a pump on and off.

so with this updated code its still doing the on/off over and over and over

#include <Button.h>
Button button1(10);

 
void setup(){
  button1.begin();
Serial.begin(9600);
pinMode(10, INPUT);

}

void loop()
{
   boolean val = 0;
  static boolean pushed = 0; 
  val = digitalRead(10);   // read the input pin
  if (val == 1 && pushed == false);
  {
    pushed = true;
    Serial.println("On");
  }

  if (digitalRead(10) == 0 && pushed == false);
  {
    pushed = false;
    Serial.println ("off");
  }
}

First of all be consistent. If you use bools, use true and false; definitely not a mix of 0, 1, true and false.

You also have a mistake in the second if statement. See if you can spot it.

Lastly, including the button library but not really using it does not make sense (to me).

you guys do know i only want "on" or "off" printed a single time in the serial print, because so far, i dont see how this is
going to do that

What I missed was that semi-colon after the if statements. So it will always execute the block between the curlies { and }.

I have reworked your code. Requirement is that the button is connected between pin and ground.

const byte pinButton = 2;

void setup()
{
  Serial.begin(57600);
  pinMode(pinButton, INPUT_PULLUP);

}

void loop()
{
  boolean val;
  static boolean pushed = false;
  
  val = digitalRead(pinButton);   // read the input pin

  Serial.print("val = "); Serial.println(val);
  Serial.print("pushed = "); Serial.println(pushed);

  if (pushed == false)
  {
    if (val == 0)
    {
      pushed = true;
      Serial.println("On");
    }
  }
  else
  {
    if (val == 1)
    {
      pushed = false;
      Serial.println ("off");
    }
  }
  Serial.println();
  delay(3000);

}

For debugging purposes, the additional serial prints and the delay were added. I removed the button library stuff that you had as you don’t seem to use it. Mixing that library with ordinary digitalReads does not make sense. I have no experience with the library but it would make sense if it has its own methods for determining the state of the button.

The library just debounces the button. So i dont need to do a millis thing for it.

And wow. What a small mistake that maked a huge difference.