Go Down

Topic: Error arduino self reset when displaying sensor values on LCD 16x2 (Read 53 times) previous topic - next topic

Flashlight

Hello everyone!, i'm a newbie and i have a problem with my project, this is displaying values sensor on LCD 16x2. Everything 'is fine except arduino 's self reset. Have you encountered this error before? Please give me an idea, thanks all.

......................................
This is my code:
#include <LiquidCrystal.h>
#include <TinyGPS.h>
TinyGPS gps;
#include <timer.h>
#include <timerManager.h>
LiquidCrystal lcd {37, 35, 33, 31, 29, 27};
Timer Ultrasonic; //kt1
Timer Temp; //kt2
Timer Ppm; //kt3
int thresh_temp = 35;
int thresh_ppm1 = 1200;
int thresh_ppm2 = thresh_ppm1 + 100 ;
int thresh_ppm3 = thresh_ppm2 + 300;
double Kinhdo, Vido;
String Link;
String SMS;
int _timeout;
String _buffer;
String number = "+84979xxxxxx";
const int trigPin = 8;
const int echoPin = 9;
float duration;
float distance;
float k[10];
boolean kt1 = false, kt2 = false, kt3 = false;
boolean kt5 = false;
boolean kt_2a = true, kt_2b = true;
byte m = 1;
float calculateDistance()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (float)duration * 0.034 / 2.0;
  return distance;
}
int Distance()
{
  distance = calculateDistance();
  k[m] = distance;
  /*Serial.print("Values k");
    Serial.print(i);
    Serial.print(": ");
    Serial.println(k);*/
  if (m > 1)
  {
    float a = abs(k[m] - k[m - 1]);
    if (a < 1)
    {
      kt1 = false;
      Serial.println("Nothing");
    }
    else
    {
      kt1 = true;
      Serial.println("Detect");
    }
  }
  m += 1;
  if (m == 11)
  {
    m = 1;
  }
}
int Temperature() {
  byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
  byte response[9];
  Serial1.write(cmd, 9);
  //clear the buffer
  memset(response, 0, 9);
  int i = 0;
  while (Serial1.available() == 0) {
    i++;
  }
  if (Serial1.available() > 0) {
    Serial1.readBytes(response, 9);
  }
  // print out the response in hexa
  /*for (int i = 0; i < 9; i++) {
    Serial.print(String(response, HEX));
    Serial.print("   ");
    }*/
  byte check = getCheckSum(response);
  if (response[8] != check) {
    /*Serial.println("Checksum not OK!");
      Serial.print("Received: ");
      Serial.println(response[8]);
      Serial.print("Should be: ");
      Serial.println(check);*/
  }
  int ppm_uart = 256 * (int)response[2] + response[3];
  // Serial.print("PPM UART: ");
  //Serial.println(ppm_uart);
  byte temp = response[4] - 40;
  if (temp < 100)
  {
    lcd.setCursor(0, 0);
    lcd.print("Nhiet do: ");
    lcd.print(temp);
    lcd.print(" oC ");
    Serial.print("Nhiet do: ");
    Serial.print(temp);
    Serial.println(" oC ");
  }
  if (temp > 100)
  {
    lcd.setCursor(0, 0);
    lcd.print(" Initializing...");
  }
  if ((temp > thresh_temp) && (temp < 100))
  {
    kt2 = true;
    lcd.setCursor(0, 0);
    lcd.print("High Temperature");
    delay(2000);
  }
  if (temp < thresh_temp)
  {
    kt2 = false;
  }
  byte status = response[5];
  if (status == 0x40) {
  }
  return ppm_uart;
}
byte getCheckSum(char *packet) {
  byte i;
  unsigned char checksum = 0;
  for (i = 1; i < 8; i++) {
    checksum += packet;
  }
  checksum = 0xff - checksum;
  checksum += 1;
  return checksum;
}
int co2ppm() {
  unsigned long th, tl, ppm = 0;
  do {
    th = pulseIn(3, HIGH, 1004000) / 1000;
    tl = 1004 - th;
    ppm = 2000 * (th - 2) / (th + tl - 4);
  } while (th == 0);
  if ((ppm < 50000) && (ppm > 162))
  {
    lcd.setCursor(0, 1);
    lcd.print("%CO2: ");
    lcd.print(ppm);
    lcd.print(" PPM  ");
    Serial.print("%CO2:  ");
    Serial.println(ppm);
  }
  if (ppm > 50000)
  {
    lcd.setCursor(0, 1);
    lcd.print(" Initializing...");
  }
  if ((ppm < thresh_ppm2) && (ppm > thresh_ppm1))
  {
    kt3 = true;
    lcd.setCursor(0, 0);
    lcd.print("%CO2 in HIGH lvl");
    delay(2000);
  }
  if ((ppm < thresh_ppm1) && (ppm > 216))
  {
    kt3 = false;
  }
  if ((ppm > thresh_ppm2) && (ppm < thresh_ppm3))
  {
    lcd.setCursor(0, 0);
    lcd.print("%CO2 in HIGH lvl");
    delay(2000);
  }
  if ((ppm > thresh_ppm3) && (ppm < 50000))
  {
    kt5 = true;
    lcd.setCursor(0, 0);
    lcd.print("%CO2 was harmful");
    delay(2000);
  }
  return ppm;
}
void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial3.begin(9600);
  Serial2.begin(9600);
  pinMode(3, INPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  lcd.setCursor(0, 0);
  lcd.print(" HCMUTE--2015");
  lcd.setCursor(0, 1);
  lcd.print("    Welcom!");
  delay(1000);
  //Đọc GPS

  Ultrasonic.setInterval(500);
  Ultrasonic.setCallback(Distance);
  Ultrasonic.start();
  Ppm.setInterval(500);
  Ppm.setCallback(co2ppm);
  Ppm.start();
  //Đọc nhiệt độ nơi đặt cảm biến
  Temp.setInterval(500);
  Temp.setCallback(Temperature);
  Temp.start();
  TimerManager::instance().start();
}
void loop()
{
  while ((kt1 == 1) && (kt2 == 1))
  {
    Serial.println("Canh bao: nhiet do tang cao!");
    digitalWrite(13, HIGH);
    delay(500);
    digitalWrite(13, LOW);
    delay(500);
    SendMessage2();
    kt_2a = false;
    break;
  }
  while ((kt1 == 1) && (kt3 == 1))
  {
    Serial.println("Canh bao: nong do CO2 tang cao!");
    digitalWrite(13, HIGH);
    delay(500);
    digitalWrite(13, LOW);
    delay(500);
    SendMessage1();
    break;
  }
  while ((kt1 == 1) && (kt5 == 1))
  {
    Serial.println("Canh bao: nong do khi gay hai!");
    digitalWrite(13, HIGH);
    delay(500);
    digitalWrite(13, LOW);
    delay(500);
    callNumber();
    break;
  }
  // return;
  while ((kt1 == 1) && (kt5 == 1) && (kt2 == 1))
  {
    Serial.println("Dieu kien khong khi trong xe khong an toan!");
    lcd.setCursor(0, 0);
    lcd.print("   Warning...   ");
    delay(2000);
    lcd.setCursor(0, 1);
    lcd.print("Air alert case!");
    delay(2000);
    digitalWrite(13, HIGH);
    delay(500);
    digitalWrite(13, LOW);
    delay(500);
    callNumber();
  }
 
  Ultrasonic.update();
  Temp.update();
  Ppm.update();
  TimerManager::instance().update();
}
void SendMessage1()// Canh bao nong do CO2 cao
{
  if (kt_2b == true)
  {

    GPS();
    Serial3.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
    delay(1000);
    //Serial.println ("Set SMS Number");
    Serial3.println("AT+CMGS="" + number + ""r"); //Mobile phone number to send message
    delay(1000);
    SMS = "WARNING: CO2 concentration in high level!Check your car.Access to the address to know the location:   " + Link;
    Serial3.println(SMS);
    delay(100);
    Serial3.println((char)26);// ASCII code of CTRL+Z
    delay(1000);
    _buffer = _readSerial();
    Serial.println(SMS);
    kt_2b = false;
  }
}
void SendMessage2()// Canh bao nhiet do cao
{
  GPS();
  Serial3.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);
  //Serial.println ("Set SMS Number");
  Serial3.println("AT+CMGS="" + number + ""r"); //Mobile phone number to send message
  delay(1000);
  SMS = "WARNING: Temperature in high level!Check your car.Access to the address to know the location:   " + Link;
  Serial3.println(SMS);
  delay(100);
  Serial3.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
  _buffer = _readSerial();
  Serial.println(SMS);
}
//void RecieveMessage()
//{
//Serial.println ("Serial1800L Read an SMS");
//delay (1000);
//Serial1.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
//delay(1000);
//Serial.write ("Unread Message done");
//}
String _readSerial() {
  _timeout = 0;
  while  (!Serial3.available() && _timeout < 12000  )
  {
    delay(13);
    _timeout++;
  }
  if (Serial3.available()) {
    return Serial3.readString();
  }
}



AWOL

We can't see your code.
We can't see your schematic.
We can't see your observations.

See the problem?
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up