It keep saying a function-definition is not allowed here before'{' token. does anyone know what's wrong with my code?

#include<LiquidCrystal.h> // lcd Header

LiquidCrystal lcd(7,6,5,4,3,2); // pins for LCD connection

#define buzzer 12 // buzzer pin

#define led 13 // ledpin

#define x A0 // x_out pin of accelerometer

#define y A1 // y_out pin of accelerometer

#define z A2 // z_out pin of accelerometer

/variables/

int xsample=0;

int ysample=0;

int zsample=0;

long start;

int buz=0;

/Macros/

#define samples 50

#define maxVal 20 // max change limit

#define minVal -20 // min change limit

#define buzTime 5000 // buzzer on time

void setup() {

{

lcd.begin(16,2); // initialising lcd

Serial.begin(9600); // initialising serial

delay(1000);

lcd.print("Earthquake ");

lcd.setCursor(0,1);

lcd.print("Detector ");

delay(2000);

lcd.clear();

lcd.print("Calibrating.....");

lcd.setCursor(0,1);

lcd.print("Please wait...");

pinMode(buzzer,OUTPUT);

pinMode(led, buz);

buz=0;

digitalWrite(buzzer, buz);

digitalWrite(led, buz);

for(int i=0;i<samples;i++) // taking samples for calibrating

{

xsample+=analogRead(x);

ysample+=analogRead(y);

zsample+=analogRead(z);

}

xsample/=samples; // taking avg for x

ysample/=samples; // taking avg for y

zsample/=samples; // taking avg for z

delay(3000)

;lcd.clear();

lcd.print("Calibrated");

delay(1000)

;lcd.clear();

lcd.print("Device Ready");

delay(1000)

;lcd.clear();

lcd.print(" X Y Z ");

}

void loop()

{

int value1=analogRead(x); // reading x out

int value2=analogRead(y); // reading y out

int value3=analogRead(z); // reading z out

int xValue=xsample-value1; // finding change in x

int yValue=ysample-value2; // finding change in y

int zValue=zsample-value3; // finding change in z

/displaying change in x,y and z axis values over lcd/

lcd.setCursor(0,1)

lcd.print(xValue);

lcd.setCursor(6,1);

lcd.print(yValue);

lcd.setCursor(12,1);

lcd.print(zValue);

delay(100)

/comparing change with predefined limits/

if(xValue < minVal || xValue > maxVal || yValue < minVal || yValue > maxVal || zValue < minVal || zValue > maxVal)

{

if(buz == 0)

start=millis(); // timer start

buz=1; // buzzer / led flag activated

}

else if(buz == 1) // buzzer flag activated then alerting earthquake

{

lcd.setCursor(0.0);

lcd.print("Earthquake Alert");

if(millis()>= start+buzTime)

buz=0;

}

else

{

lcd.clear();

lcd.print(" X Y Z ");

}

digitalWrite(buzzer, buz);

digitalWrite(led, buz);

/sending values to processing for plot over the graph/

Serial.print("x=");

Serial.printIn(xValue);

Serial.print("y=");

Serial.printIn(yValue);

Serial.print("z=");

Serial.printIn(zValue);

Serial.printIn("$");

}

Welcome to the forum.

I don't know what the problem is with your code yet, but it will be easier to find out if you first redo your topic by placing your code inside the tags as recommended in the topic they recommended you read when you signed up to the forum.

Use Ctrl + T in the Arduino IDE to indent it before copying the code.

PS: What is this:

void setup() {

{

I think you copied this from a PDF or an HTML document.

  • The "/*" beginning of comments and "*/" end of comment are changed to single slants "/"
  • You have one too many open braces "{"
  • missed a semi-colon ( ; ) at the end of a few lines
  • a point (.) rather than a comma (,) separating values
  • mistaking an uppercase "I" (ninth letter) for a lowercase "'l'" (twelfth letter)

To make your sketch work, follow the errors reported during the compile. The errors tell you the line number of the error(s).

; missing

; missing. Probably more , didn't look deeper

No, if you qoute the original post then you'll see "*".
Extra braces are ok if there are closing braces.

Missing ";" is a problem

This is because of the missing code tags.
@nuha_21 : Here you can see how important they are. The forum software treats the '*' as a formatting character ( setting to italic ). So your code is corrupted in your post.

1 Like

Basically some missing semicolons, "/" instead of "//" before comments, dots instead of commas and incorrect calling of println function.

The version below might compile... do what you want is another problem...

#include<LiquidCrystal.h> // lcd Header

LiquidCrystal lcd(7,6,5,4,3,2); // pins for LCD connection

#define buzzer 12 // buzzer pin
#define led 13 // ledpin
#define x A0 // x_out pin of accelerometer
#define y A1 // y_out pin of accelerometer
#define z A2 // z_out pin of accelerometer

//variables

int xsample=0;
int ysample=0;
int zsample=0;
long start;
int buz=0;

//Macros

#define samples 50
#define maxVal 20 // max change limit
#define minVal -20 // min change limit
#define buzTime 5000 // buzzer on time

void setup() {

lcd.begin(16,2); // initialising lcd
Serial.begin(9600); // initialising serial
delay(1000);
lcd.print("Earthquake ");
lcd.setCursor(0,1);
lcd.print("Detector ");
delay(2000);
lcd.clear();
lcd.print("Calibrating.....");
lcd.setCursor(0,1);
lcd.print("Please wait...");
pinMode(buzzer,OUTPUT);
pinMode(led, buz);
buz=0;
digitalWrite(buzzer, buz);
digitalWrite(led, buz);

for(int i=0;i<samples;i++) // taking samples for calibrating
{
xsample+=analogRead(x);
ysample+=analogRead(y);
zsample+=analogRead(z);
}

xsample/=samples; // taking avg for x
ysample/=samples; // taking avg for y
zsample/=samples; // taking avg for z

delay(3000);
lcd.clear();
lcd.print("Calibrated");
delay(1000);
lcd.clear();
lcd.print("Device Ready");
delay(1000);
lcd.clear();
lcd.print(" X Y Z ");
}

void loop() {

int value1=analogRead(x); // reading x out
int value2=analogRead(y); // reading y out
int value3=analogRead(z); // reading z out
int xValue=xsample-value1; // finding change in x
int yValue=ysample-value2; // finding change in y
int zValue=zsample-value3; // finding change in z

//displaying change in x,y and z axis values over lcd

lcd.setCursor(0,1);
lcd.print(xValue);
lcd.setCursor(6,1);
lcd.print(yValue);
lcd.setCursor(12,1);
lcd.print(zValue);
delay(100);

//comparing change with predefined limits

if(xValue < minVal || xValue > maxVal || yValue < minVal || yValue > maxVal || zValue < minVal || zValue > maxVal)
{

if(buz == 0) {
start=millis(); // timer start
buz=1; // buzzer / led flag activated
}
}

else if(buz == 1) // buzzer flag activated then alerting earthquake
{
lcd.setCursor(0,0);
lcd.print("Earthquake Alert");
if(millis()>= start+buzTime)
buz=0;
}

else {
lcd.clear();
lcd.print(" X Y Z ");
}

digitalWrite(buzzer, buz);
digitalWrite(led, buz);

//sending values to processing for plot over the graph

Serial.print("x=");
Serial.println(xValue);
Serial.print("y=");
Serial.println(yValue);
Serial.print("z=");
Serial.println(zValue);
Serial.println("$");
}

Take a look at How to get the best out of this Forum

2 Likes

10 posts were split to a new topic: Discussion re: corruption of code not posted in code blocks

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7,3 POSITIVE);

#define buzzer 12

#define led 13

#define x A0

#define y A1

#define z A2

int xsample=0;

int ysample=0;

int zsample=0;

long start;

int buz=0;

/Macros/

#define samples 9

#define maxVal 3

#define minVal -3

#define buzTime 2000

void setup()

{

lcd.begin(16,2);

Serial.begin(9600);

delay(1000);

lcd.print("EarthQuake");

lcd.setCursor(0,1);

lcd.print("Detector");

delay(2000)

lcd.clear();

lcd.print("Calibrating.....");

lcd.setCursor(0,1);

lcd.print("Please wait...");

pinMode(buzzer,OUTPUT);

pinMode(led,OUTPUT);

buz=0;

digitalWrite(buzzer,buz);

digitalWrite(led,buz);

for(int i=0;i<samples;i++)

{

xsample+=analogRead(x);

ysample+=analogRead(y);

zsample+=analogRead(z);

}

xsample/=samples;

ysample/=samples;

zsample/=samples;

delay(3000)

lcd.clear();

lcd.print("Calibrated");

delay(1000);

lcd.clear();

lcd.print("Device Ready");

delay(1000);

lcd.clear();

lcd.print(" X Y Z ")

}

void loop()

{

int value1=analogRead(x);

int value2=analogRead(y);

int value3=analogRead(z);

int xValue=xsample-value1;

int yValue=ysample-value2;

int zValue=zsample-value3;

lcd.setCursor(0,1);

lcd.print(xValue);

lcd.setCursor(6,1);

lcd.print(yValue);

lcd.setCursor(12,1)

lcd.print(zValue);

delay(100);

if(xValue < minVal||xValue > maxVal||yValue < minVal||yValue > maxVal||zValue < minVal||zValue > maxVal)

{

if(buz == 0)

start=millis();

buz=1;

}

else if(buz == 1)

{

lcd.setCursor(0,0);

lcd.print("Earthquake Alert ");

if(millis()>= start+buzTime)

buz=0;

}

else

{

lcd.clear();

lcd.print(" X Y Z ");

}

digitalWrite(buzzer,buz);

digitalWrite(led,buz);

Serial.print("x=");

Serial.printIn(xValue);

Serial.print("y=");

Serial.printIn(yValue);

Serial.print("z=");

Serial.printIn(zValue);

Serial.printIn("$");

}

  • missing a ',' before "POSITIVE"
  • are you sure POSITIVE is a valid argument?
  • in many cases the code is missing a ';' at the end of the preceeding line
    lcd.print("Detector");
    delay(2000)
    lcd.clear();
Tst:29:5: error: expected ';' before 'lcd'
     lcd.clear();
     ^~~

there's no ';' after delay(2000)

  • "println", not "printIn"
1 Like

@nuha_21
You continue to disrespect the forum and post the code without tags.

Is this a continuation of the same question as in your last topic?

Opening of the multiple threads about the same topic is violation of the forum rules.

…also its worth reading about code tags, which make your code more readable and easier for others to help you.

Is this your first sketch in Arduino ?
Then I suggest to start with a blinking led.
Every comma and every semicolon matters in the 'C' and 'C++' language.

Most libraries are on Github and most libraries have examples. Here is an example for the "LiquidCrystal_I2C" library: https://github.com/johnrickman/LiquidCrystal_I2C/blob/master/examples/HelloWorld/HelloWorld.pde

Here is your sketch in Wokwi simulation (with a few changes to make it compile):

/**/

a7

Actually it should be easy but I will not burn time on somebody that will not spend the time to follow the posting guidelines on the forum especially when being asked several time. The time I would have spend on your problem will be spent helping somebody who took the time to read the guidelines.

1 Like

@nuha_21 ,

I've merged your new topic with your previous topic as they are both about problems with the same or similar code, so one naturally follows the other. Please keep all your questions about the same code and project here, don't keep starting new questions. If you keep starting new topics about the same thing you risk a ban from the forum.

I note that you have been advised to post in code tags but fail to do so. The forum guide is here:

Please read the guide before posting anything else, especially how to post code.

Is there a language problem here?

Thank you.

1 Like

It really helps to read post #2 and post #3.

I think this is a troll or similar, she has only posted twice with the same nonsense.

1 Like

seems to be a typical newbie, or just someone who needs an Arduino to do something without having a desire to learn to program