How to only display on serial print once

I'm an amateur at Arduino programing today is my first day infact

I have created a code

int INITIALIZE = 0;

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

}

void loop()
{
  if (digitalRead(2) > 0) {
    INITIALIZE = 1;
  } else {
    INITIALIZE = 0;
  }
  if (INITIALIZE == HIGH) {
    Serial.println("initialized");
  } else {
    Serial.println("Please Initialize");
  }

i was wondering could i make it say "please initialize" and "initialized" once and if so how

Place it in setup :smiley: But if you want to constantly check, don't check for HIGH / LOW of the button but use state change detection. See the examples or grab an easy library like Bounce2 :slight_smile:

I do not understand when I do this

int INITIALIZE = 0;

void setup()
{
  pinMode(2, INPUT);
  Serial.begin(9600);
   if (digitalRead(2) > 0) {
    INITIALIZE = 1;
  } else {
    INITIALIZE = 0;
  }
  if (INITIALIZE == HIGH) {
    Serial.println("hello world");
  } else {
    Serial.println("Please Initialize");
  }
}

void loop()
{

}

the button does nothing

How do you know that?

mynamejake:
the button does nothing

I suspect if you hold the button down and then press the reset button on the UNO board you'll see a different message printed.

Hint: You only get one pass through setup() then the code drops into loop() forever.

From what I suspect you want to do, all the answers were already given above. However, if you are new to programming as well, you may not quite get it.

In your first sketch setup() does what is required. loop() then simply keeps looping and run the same code over and over. That will keep printing one of the messages depending on whether the button (I assume you are looking at a button) is pressed or not. I think you should see that if you look at your code.

When you moved the code to setup(), it will read the button once on startup and never again. You will probably not have the button pressed when it executes that read. The program then moves on to loop(), doing nothing forever.

As suggested, use the state of INITIALIZE to decide whether you need to read the button, or whether you need to initialize, or not.

Willem

Or better still, use the transition from "not pressed" to "pressed" to trigger the printing of the message.
Use the state change example sketch for clues.