Button press to light an LED and to print a message over serial

Hello, I have a (possibly?) unusual problem and was wondering if someone could help please :slight_smile:

I am trying to mock up a circuit (as a proof of concept for a larger project). This version has 2 buttons and 2 LEDs.

What I want to happen is that when the Green button is pressed, the Green LED lights and latches on, and the text "Green" is sent to the serial port. (eventually I will use an external serial port, but for the time being I am just monitoring on the Serial Monitor.

When the black button is pressed, I want the green LED to go out and the red LED to latch on, and the text "Black' to be sent.
What is actually happening is that as soon as I press the green button, it send the text over and over and over and over... also, I haven't managed to work out the latching thing but I will work it out :slight_smile:

Please note that I have reversed the high and low, think this is due to my circuit but it's not a huge deal. Also, I haven't put the code in for the black button - but I assume it's the same as the green button one?


// Define the pins

const int GreenButton = 7;
const int BlackButton = 8;
const int RedLED = 5;
const int GreenLED = 6;

void setup() { //Setup the pin modes
pinMode(GreenButton, INPUT);
pinMode(BlackButton, INPUT);
pinMode(RedLED, OUTPUT);
pinMode(GreenLED, OUTPUT);

void loop() {
int stateGreenButton = digitalRead(GreenButton); //read the state of the Green button
if(stateGreenButton == 1){ //if it is pressed
digitalWrite(GreenLED, LOW); //write 1 or HIGH to Green LED Pin ((NOTE: REVERSED))
} else { //if not pressed
digitalWrite(GreenLED, HIGH); //write 0 or LOW to Green LED pin ((NOTE: REVERSED))


I don't know how you have your "buttons" or LEDs rigged.

const byte RedLED = 5;
const byte GreenLED = 6;
const byte GreenButton = 7;
const byte BlackButton = 8;

byte stateGreenButton = 0;
byte stateBlackButton = 0;

void setup() 
{    //Setup the pin modes
  //pinMode(GreenButton, INPUT);
  //pinMode(BlackButton, INPUT);
  pinMode(RedLED, OUTPUT);
  pinMode(GreenLED, OUTPUT);

void loop() 
  stateGreenButton = digitalRead(GreenButton);
  stateBlackButton = digitalRead(BlackButton);
  if(stateGreenButton == 1)
  { //if it is pressed
    digitalWrite(GreenLED, HIGH);
  if(stateBlackButton == 1)
    digitalWrite(GreenLED, LOW);
    digitalWrite(RedLED, HIGH);

Rudimentary, not very snappy.