สอบถามการเขียนโค้ดlm35 sensor กับarduino atmega 2560

พอดีทำงานเกี่ยวกับLm35 sensorค่ะแต่sensorไม่วัดอุณหภูมิเลยค่ะพอแตะที่ตัวเซนเซอร์ แต่มีการเปลี่ยนแปลงแค่ตอนจับขาเซนเซอร์

อันนี้โค้ดค่ะพยายามแก้มาหลายครั้งก็แก้เรื่องsensorให้ตรวจจับที่ตัวเซนเซอร์เป็นปกติไม่ได้คิดว่าต่อวงจรถูกแล้วนะคะ

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <math.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

#define LED1_PIN A8
#define LED2_PIN A9
#define LED3_PIN A10
#define LED4_PIN A11
#define LED5_PIN A12
#define LED6_PIN A13
#define BUZZER_PIN 9       
#define SENSOR_PIN A0   

const float T1=15, T2=18, T3=20, T4=22, T5=23, T6=25;

float currentT = 0.0;

long readVcc_mV() {
  ADMUX  = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(2);
  ADCSRA |= _BV(ADSC);
  while (ADCSRA & _BV(ADSC));
  uint16_t adc = ADC;
  return (long)(1125300L / adc);
}

int analogReadStable(uint8_t pin){
  analogRead(pin); delayMicroseconds(200);
  const int N=7;
  int a[N];
  for(int i=0;i<N;i++){ a=analogRead(pin); delay(2); }
  for(int i=0;i<N-1;i++)
    for(int j=i+1;j<N;j++)
      if(a[j]<a){ int t=a; a=a[j]; a[j]=t; }
  long s=0;
  for(int i=1;i<N-1;i++) s+=a;
  return (int)(s/(N-2));
}

float smoothTempLimited(float x){
  static float y=0;
  const float alpha=0.15f;
  float pred = alpha*x + (1-alpha)*y;
  if (pred - y > 2.0f) pred = y + 2.0f;
  if (y - pred > 2.0f) pred = y - 2.0f;
  y = pred;
  return y;
}

void alarm_buzzer(float T){
  if(T >= T6){
    for(int i=0;i<3;i++){ tone(BUZZER_PIN,2200,250); delay(200); tone(BUZZER_PIN,1700,250); delay(200); }
    noTone(BUZZER_PIN);
  }else if(T >= T5){
    tone(BUZZER_PIN,2000,400); delay(400);
  }else if(T >= T4){
    tone(BUZZER_PIN,1500,250); delay(300);
  }else{
    noTone(BUZZER_PIN);
  }
}

void setLEDs(float T){
  const int P[6]={A8,A9,A10,A11,A12,A13};
  for(int i=0;i<6;i++) digitalWrite(P,LOW);
  if(T>=T6){ for(int i=0;i<6;i++) digitalWrite(P,HIGH); }
  else if(T>=T5){ for(int i=0;i<=4;i++) digitalWrite(P,HIGH); }
  else if(T>=T4){ for(int i=0;i<=3;i++) digitalWrite(P,HIGH); }
  else if(T>=T3){ for(int i=0;i<=2;i++) digitalWrite(P,HIGH); }
  else if(T>=T2){ for(int i=0;i<=1;i++) digitalWrite(P,HIGH); }
  else if(T>=T1){ digitalWrite(P[0],HIGH); }
}

void setup() {
  Serial.begin(9600);
  analogReference(DEFAULT);
  const int P[6]={A8,A9,A10,A11,A12,A13};
  for(int i=0;i<6;i++) pinMode(P,OUTPUT);
  pinMode(BUZZER_PIN,OUTPUT);

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("LM35 Temp Monitor");
  delay(1000);
  lcd.clear();
}

void loop() {
  float Vcc = readVcc_mV()/1000.0;
  int adc = analogReadStable(SENSOR_PIN);
  float Vout = adc * (Vcc / 1023.0);
  float Traw = Vout / 0.01;
  if (Traw < -5 || Traw > 150) Traw = currentT;
  currentT = smoothTempLimited(Traw);

  Serial.print("Vcc="); Serial.print(Vcc,3);
  Serial.print("V  ADC="); Serial.print(adc);
  Serial.print("  Vout="); Serial.print(Vout,3);
  Serial.print("V  Temp="); Serial.print(currentT,1);
  Serial.println("C");

  setLEDs(currentT);
  alarm_buzzer(currentT);

  lcd.setCursor(0,0);
  lcd.print("Temp: ");
  lcd.print(currentT,1);
  lcd.print((char)223);
  lcd.print("C   ");

  lcd.setCursor(0,1);
  if(currentT < T2) lcd.print("Status: Normal   ");
  else if(currentT < T4) lcd.print("Status: Warm     ");
  else if(currentT < T6) lcd.print("Status: Hot!     ");
  else lcd.print("Status: Danger!  ");

  delay(400);
}

วงจรค่ะ

แสดงความคิดเห็น
โปรดศึกษาและยอมรับนโยบายข้อมูลส่วนบุคคลก่อนเริ่มใช้งาน อ่านเพิ่มเติมได้ที่นี่