How to Prompt Serial Monitor to Display New Lines of Text with Each Button Press

Hi all. I'm a beginner with Arduino, and I'm using it for a Digital and Electronic Art class. I have a functioning code; however, I'm wondering how to make the serial monitor sequentially display a new case (these are poem stanzas) with each press of the button.

Right now, the serial monitor displays the entire poem in one button press.

Thanks in advance!

const int buttonPin = 2;

int buttonPushCounter = 0;
boolean buttonState = LOW;
boolean lastButtonState = LOW;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}
void loop() {
  buttonState = digitalRead(buttonPin);

if (buttonState != lastButtonState)
{
  if (buttonState == HIGH)
  {
    buttonPushCounter++;
      {
        buttonPushCounter = 0;
      }
  Serial.println(buttonPushCounter);
  switch (buttonPushCounter)
  {     
    case 0:
      Serial.println("It seems foreign, now.");
      Serial.println("All of this,");
      Serial.println("Being here.");
      break;
  }
  }
}
if (buttonState != lastButtonState)
{
  if (buttonState == HIGH)
  {
    buttonPushCounter++;
      {
        buttonPushCounter = 1;
      }
  Serial.println(buttonPushCounter);
  switch (buttonPushCounter)
  {
      case 1:
      Serial.println("The chambers,");
      Serial.println("The calcified surface of a heart,");
      Serial.println("A tarred, rotten thing.");
      break;
  }
  }
}
if (buttonState != lastButtonState)
{
  if (buttonState == HIGH)
  {
    buttonPushCounter++;
      {
        buttonPushCounter = 2;
      }
  Serial.println(buttonPushCounter);
  switch (buttonPushCounter)
  {
    case 2:
    Serial.println("Still, a pool of warm blood within,");
    Serial.println("Feverish, thrasing, desperate.");
    break;
  }
  }
}
if (buttonState != lastButtonState)
{
  if (buttonState == HIGH)
  {
    buttonPushCounter++;
      {
        buttonPushCounter = 3;
      }
  Serial.println(buttonPushCounter);
  switch (buttonPushCounter)
  {
    case 3:
    Serial.println("Let it circulate endlessly through familiar chambers and valves.");
    Serial.println("Moving, yes.");
    Serial.println("But only from within.");
    break;
  }
  }
}
if (buttonState != lastButtonState)
{
  if (buttonState == HIGH)
  {
    buttonPushCounter++;
      {
        buttonPushCounter = 4;
      }
  Serial.println(buttonPushCounter);
  switch (buttonPushCounter)
  {
    case 4:
    Serial.println("Chambers and valves.");
    Serial.println("All I am is chambers and valves,");
    Serial.println("A breathing, wretched thing.");
    break;
  }
  }
}
lastButtonState = buttonState;
}

Hi,
try this code:

const int buttonPin = 2;
int buttonPushCounter = 0;
boolean buttonState = LOW;
boolean lastButtonState = LOW;
bool flag =  false;
//----------------------------------------------------------
void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}
//----------------------------------------------------------
void loop() {
  if (digitalRead(buttonPin) == HIGH)
  {
    delay(30);
    while (digitalRead(buttonPin) == HIGH)
    {
      flag = true;
    }
  }
  if (flag == true)
  {
    buttonPushCounter++;
    if (buttonPushCounter > 5)
    {
      buttonPushCounter = 1;
    }
     flag = false;
    //Serial.println(buttonPushCounter);
    switch (buttonPushCounter)
    {
      case 0:
        break;

      case 1:
        Serial.println("It seems foreign, now.");
        Serial.println("All of this,");
        Serial.println("Being here.");
        Serial.println("");
        break;

      case 2:
        Serial.println("The chambers,");
        Serial.println("The calcified surface of a heart,");
        Serial.println("A tarred, rotten thing.");
        Serial.println("");
        break;

      case 3:
        Serial.println("Still, a pool of warm blood within,");
        Serial.println("Feverish, thrasing, desperate.");
        Serial.println("");
        break;

      case 4:
        Serial.println("Let it circulate endlessly through familiar chambers and valves.");
        Serial.println("Moving, yes.");
        Serial.println("But only from within.");
        Serial.println("");
        break;

      case 5:
        Serial.println("Chambers and valves.");
        Serial.println("All I am is chambers and valves,");
        Serial.println("A breathing, wretched thing.");
        Serial.println("");
        break;
    }
  }
}

Thank you so much! I really appreciate it. This is exactly what I was looking for. :smiley:

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