Serial.println() function prints infinite times in Serial Monitor

Why Serial Monitor keeps on printing one Serial.println() function infinite times? Can you look up the code and help me out.

// For checking slots
Serial.println("Booked Slots");
for(i=0;i<=2;i++)
{
  val[i]=digitalRead(place[i]);
  if(val[i]==1)
  {
    Serial.println(i+1);
    Serial.println();
  }
}

Can you post your code?(seems obvious, I know)

Post the complete code. Note that if you have code in the loop function, that code will be repeated infinitely.

OP's attached image:

Not sure what I'm supposed to do with a picture of text. Perhaps I should write him an answer and then just post a screen cap of it. :slight_smile:

#include <LiquidCrystal.h>
#include <Servo.h>

// Servo Motor
Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position

// LCD
int contrast=100;
const int rs=12,en=11,d4=5,d5=4,d6=3,d7=2;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

//IR sensor
int isObstacle1Pin=22;
int isObstacle2Pin=24;
int isObstacle3Pin=26;
int isObstacle4Pin=28;
int isObstacle5Pin=30;
int isObstacle1=0;
int isObstacle2=0;
int isObstacle3=0;
int isObstacle4=0;
int isObstacle5=0;

// Control time for Servo
unsigned long ServoStartTime;
const unsigned long ServoOnTime=3000;

// Slots
int count=0;
int i;
byte val[3];
const int place[3]={26,28,30};
void setup()
{
  pinMode(isObstacle1Pin,INPUT_PULLUP);
  pinMode(isObstacle2Pin,INPUT_PULLUP);
  myservo.attach(9);
  Serial.begin(9600);
  Serial.println("---------------------------------------------");
  Serial.println("    Welcome to Smart Car Parking System");
  Serial.println("---------------------------------------------");
  analogWrite(A0,contrast);
  lcd.begin(16,2);
  lcd.setCursor(0,0);
}

void loop()
{ 
isObstacle1 = digitalRead(isObstacle1Pin);
isObstacle2 = digitalRead(isObstacle2Pin);
isObstacle3 = digitalRead(isObstacle3Pin);
isObstacle4 = digitalRead(isObstacle4Pin);
isObstacle5 = digitalRead(isObstacle5Pin);
lcd.setCursor(0,0);
lcd.print("Hello");

// For checking slots
Serial.println("Booked Slots");
for(i=0;i<=2;i++)
{
  val[i]=digitalRead(place[i]);
  if(val[i]==1)
  {
    Serial.println(i+1);
    Serial.println();
  }
}

// For Entry Gate
if(isObstacle1 == HIGH)
{
 if(isObstacle3 && isObstacle4 && isObstacle5)
 {
  myservo.write(90);
 }
 else
 {
  ServoStartTime = millis();
  Serial.println("Entry gate opening");
  // lcd.setCurso(0,1)
  // lcd.print("Entry gate opening
  for (pos = 90; pos <= 180; pos += 1)
  {
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }

     while(isObstacle1 == HIGH)
     {
        isObstacle1 = digitalRead(isObstacle1Pin);
     }
  delay(3000);
  Serial.println("Please hold on gate is closing");
  for (pos = 180; pos >= 90; pos -= 1)
  {
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  Serial.println("Entry gate closed");
  Serial.println();
 }
}

// For Exit Gate
else if(isObstacle2 == HIGH)
{
  ServoStartTime = millis();
  Serial.println("Exit gate opening");
  for (pos = 90; pos <= 180; pos += 1)
  {
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
     while(isObstacle2 == HIGH)
     {
        isObstacle2 = digitalRead(isObstacle2Pin);
     }
  delay(3000);
  Serial.println("Please hold on gate is closing");
  for (pos = 180; pos >= 90; pos -= 1)
  {
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  Serial.println("Exit gate closed");
  Serial.println();
 }
 
}

ashishpandey:
Why Serial Monitor keeps on printing one Serial.println() function infinite times?

The short answer is because that line of code is in loop() and loop() repeats indefinitely.

Without knowing what you would like to happen I can't suggest how you should change your program.

...R

PS ... If you use the AutoFormat tool it will make your code much easier to read - for you as well as us.

ashishpandey:

#include <LiquidCrystal.h>

#include <Servo.h>

// Servo Motor
Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position

// LCD
int contrast=100;
const int rs=12,en=11,d4=5,d5=4,d6=3,d7=2;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

//IR sensor
int isObstacle1Pin=22;
int isObstacle2Pin=24;
int isObstacle3Pin=26;
int isObstacle4Pin=28;
int isObstacle5Pin=30;
int isObstacle1=0;
int isObstacle2=0;
int isObstacle3=0;
int isObstacle4=0;
int isObstacle5=0;

// Control time for Servo
unsigned long ServoStartTime;
const unsigned long ServoOnTime=3000;

// Slots
int count=0;
int i;
byte val[3];
const int place[3]={26,28,30};
void setup()
{
  pinMode(isObstacle1Pin,INPUT_PULLUP);
  pinMode(isObstacle2Pin,INPUT_PULLUP);
  myservo.attach(9);
  Serial.begin(9600);
  Serial.println("---------------------------------------------");
  Serial.println("    Welcome to Smart Car Parking System");
  Serial.println("---------------------------------------------");
  analogWrite(A0,contrast);
  lcd.begin(16,2);
  lcd.setCursor(0,0);
}

void loop()
{
isObstacle1 = digitalRead(isObstacle1Pin);
isObstacle2 = digitalRead(isObstacle2Pin);
isObstacle3 = digitalRead(isObstacle3Pin);
isObstacle4 = digitalRead(isObstacle4Pin);
isObstacle5 = digitalRead(isObstacle5Pin);
lcd.setCursor(0,0);
lcd.print("Hello");

// For checking slots
Serial.println("Booked Slots");
for(i=0;i<=2;i++)
{
  val[i]=digitalRead(place[i]);
  if(val[i]==1)
  {
    Serial.println(i+1);
    Serial.println();
  }
}

// For Entry Gate
if(isObstacle1 == HIGH)
{
if(isObstacle3 && isObstacle4 && isObstacle5)
{
  myservo.write(90);
}
else
{
  ServoStartTime = millis();
  Serial.println("Entry gate opening");
  // lcd.setCurso(0,1)
  // lcd.print("Entry gate opening
  for (pos = 90; pos <= 180; pos += 1)
  {
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                      // waits 15ms for the servo to reach the position
  }

while(isObstacle1 == HIGH)
    {
        isObstacle1 = digitalRead(isObstacle1Pin);
    }
  delay(3000);
  Serial.println("Please hold on gate is closing");
  for (pos = 180; pos >= 90; pos -= 1)
  {
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                      // waits 15ms for the servo to reach the position
  }
  Serial.println("Entry gate closed");
  Serial.println();
}
}

// For Exit Gate
else if(isObstacle2 == HIGH)
{
  ServoStartTime = millis();
  Serial.println("Exit gate opening");
  for (pos = 90; pos <= 180; pos += 1)
  {
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                      // waits 15ms for the servo to reach the position
  }
    while(isObstacle2 == HIGH)
    {
        isObstacle2 = digitalRead(isObstacle2Pin);
    }
  delay(3000);
  Serial.println("Please hold on gate is closing");
  for (pos = 180; pos >= 90; pos -= 1)
  {
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                      // waits 15ms for the servo to reach the position
  }
  Serial.println("Exit gate closed");
  Serial.println();
}

}

tl;dr (hint)