I am new to arduino UNO R3 but the tx light is blinking and code is not working. Kindly provide a working code. here is the 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 //led pin

#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); //initializing lcd
Serial.begin(9600); // initializing 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, OUTPUT);
buz=0;
digitalWrite(buzzer, buz);
digitalWrite(led, buz);
for(int i=0;i<samples;i++) // taking samples for calibration
{
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

/displying 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); // buzzer on and off command
digitalWrite(led, buz); // led on and off command

/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(" $");
}

I am making an earthquake detector

Please fix your code tags. You need three back ticks (```) before the code and three after the code. They need to be on their own line.

Note that we don't provide code but will try to help you to fix yours. You will however have to describe what you expect your current code to do and what it actually does.

A blinking Tx light would indicate that your code is printing information to the serial monitor.


Your topic does not seem to indicate a problem with upload, bootloader or the likes and has therefore been moved to a more suitable location on the forum.

Before posting your code and schematic, please review the forum guidelines and use code tags to ensure clarity. Additionally, include links to the technical information for your hardware and provide a preliminary schematic that shows all power, ground, and power sources.

Important Notes: How to get the best out of this forum

  • Breadboard diagrams (often referred to as "frizzes") are not considered proper schematics in this context.
  • Wiring diagrams are helpful for assembly but are not effective for debugging.
  • Screenshots are discouraged because they are often hard to read. Please provide text-based code and clear, high-quality images of schematics.

Providing the correct information will help others understand your setup and offer better assistance. Remember we cannot see your project!

Guidelines for Effective Troubleshooting:

Helpful Resources:

Schematics are an essential tool for anyone serious about electronics, providing a clear and concise way to represent and understand circuit designs.

1 Like
  • Where did you get the sketch from ? :thinking:

  • Which do you need ?

if(buz == 0)
start=millis(); // timer start

buz=1; // buzzer / led flag activated paste code here

OR

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

I'm going to bet... from here...

1 Like

You did not copy the code correctly. Here is the original code from the Earthquake Detector (without the Processing 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 //led pin

#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); //initializing lcd
  Serial.begin(9600); // initializing 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, OUTPUT);
  buz = 0;
  digitalWrite(buzzer, buz);
  digitalWrite(led, buz);
  for (int i = 0; i < samples; i++) // taking samples for calibration
  {
    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

  /*displying 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); // buzzer on and off command
  digitalWrite(led, buz); // led on and off command

  /*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(" $");
}

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