How do i print a message when pressing a button

im trying to make a code that prints a message when i click a button.

#define BUTTON_PIN 2

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
  if (!digitalRead(BUTTON_PIN)) {
    delay(150);
    Serial.println("Hello, World!");
  }
}

/*
         +----------| USB |--------+
         | D13/SCK        MISO/D12 |
         | 3.3V           MOSI/D11~|
         | Vref             SS/D10~|
         | A0                   D9~|
         | A1       NANO        D8 |
         | A2                   D7 |
         | A3                   D6~|
         | A4/SDA               D5~|
         | A5/SCL               D4 |       ___
         | A6              INT1/D3~|       |
         | A7              INT0/D2 |---|button|--+
         | 5V                  GND |-------------+
         | RST                 RST |
         | GND    5V DO  GND   TX1 |
         | Vin    DI SCK RST   RX1 |
         +-------------------------+
*/

Welcome! What you want to do can range from impossible to easy depending on your experience etc. I would suggest you start with some of the tutorials that are on line, sorry to say some are not so good but many are very good. Start by learning the basics, you need to sense inputs and control outputs. Start with a switch and LED, they are not expensive and there is even a LED on most of the Arduinos.

At this point you have also found several tutorials on basic electronics that you have gone through. You should have acquired a copy of the Arduino Cookbook and go through that. I have no clue as to how fast you will learn this but it will probably take a few months. During this process you will learn what an IDE is and how to use to generate and upload your code to the Arduino. Let us know how you progress. https://forum.arduino.cc/t/arduino-cookbook-3rd-edition-pdf/1202391

Keep this in mind:
Gil's Crispy Critter Rules:
Rule #1. A Power Supply the Arduino is NOT!
Rule #2. Never Connect Anything Inductive to an Arduino!
Rule #3 when first starting out, add a 220R resistor in series with both Input and Output pins. (LarryD)
Rule #4 buy a DMM to measure voltages, currents and resistance. (LarryD)
Violating these rules tends to make crispy critters out of Arduinos.

Prints a message on what?
On the Serial monitor? Or on an LCD display? Or what?

Is this for school?

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.