Redefinition void loop

Error Message:

sketch_sep11a\sketch.ino: In function 'void loop()':

sketch_sep11a\sketch.ino:27:6: error: redefinition of 'void loop()'
void loop()
^~~~

sketch_sep11a\sketch_sep11a.ino:21:6: note: 'void loop()' previously defined here
void loop()
^~~~

exit status 1

Compilation error: redefinition of 'void loop()'
Code:

//Digital Arduino Clock Code


#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,5,4,3,2);

int h=12;
int m;
int s;
int flag;
int TIME;

const int hs=8;
const int ms=9;

int state1;
int state2;

void begin()
{
  lcd.begin(16,2);
  pinMode(hs,INPUT_PULLUP);
  pinMode(ms,INPUT_PULLUP);
}

void loop()
{

 lcd.setCursor(0,0);
 s=s+1;
 lcd.print("TIME:");
 lcd.print(h);
 lcd.print(":");
 lcd.print(m);
 lcd.print(":");
 lcd.print(s);

 if(flag<12)lcd.print("AM");
 if(flag==12)lcd.print("PM");
 if(flag>12)lcd.print("PM");
 if(flag==24)flag=0;

 delay(1000);
 lcd.clear();
 if(s==60){
  s=0;
  m=m+1;
 }
 if(m==60)
 {
  m=0;
  h=h+1;
  flag=flag+1;
 }
 if(h==13)
 {
  h=1;
 }
 lcd.setCursor(0,1);

 lcd.print("HAVE A NICE DAY");

 //-------Time
// setting-------//
 state1=digitalRead(hs);

 if(state1==0)
{
  h=h+1;
  flag=flag+1;
  if(flag<12)lcd.print("AM");
  if(flag==12)lcd.print("PM");
  if(flag>12)lcd.print("PM");
  if(flag==24)flag=0;
  if(h==13)h=1;
}
state2=digitalRead(ms);
if(state2==0){
  s=0;
  m=m+1;
}

  
}


What is the problem?

Try void setup(), not void begin(), for starters.

Welcome to the forum

As a guess, you have more than one sketch in the same folder. They will show up as tabs in the IDE

Both sketches have a loop() function in them so when the compiler combines the .ino files together the resulting sketch has two loop() functions in it. Hence the error

The error message is very clear:

You have two sketch files in the same directory. Both have loop() functions. Hence the "void loop()' previously defined here" error. Solution: delete or move the sketch file that shouldn't be in the directory.

1 Like

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