อยากสอบถามเรื่องการใช้งาน เนื่องจากผมได้ Code พอ run แล้วมันติดตรง readVccB Hilight เป็นสีแดงไว้ครับ อยากทราบสาเหตุ และวิธีการแก้ไขด้วยครับ
int pin = 3;
//**************************************************************************************
// READ VOLTAGE ON VIN
long readVccA() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate AVcc in mV
return result;
}
//**************************************************************************************
//GET TEMPTURE OF CPU
double GetTemp(void)
{
unsigned int wADC;
double t;
// The internal temperature has to be used
// with the internal reference of 1.1V.
// Channel 8 can not be selected with
// the analogRead function yet.
// Set the internal reference and mux.
ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3));
ADCSRA |= _BV(ADEN); // enable the ADC
delay(20); // wait for voltages to become stable.
ADCSRA |= _BV(ADSC); // Start the ADC
// Detect end-of-conversion
while (bit_is_set(ADCSRA,ADSC));
// Reading register "ADCW" takes care of how to read ADCL and ADCH.
wADC = ADCW;
// The offset of 324.31 could be wrong. It is just an indication.
t = (wADC - 324.31 ) / 1.22;
// The returned temperature is in degrees Celcius.
return (t);
}
//************************************************************************
//BENCHMARK
long bench_cnt = 0;
void benchmark() {
bench_cnt = 0;
for(bench_cnt = 0; bench_cnt < 1000000; bench_cnt++);
}
//************************************************************************
//SETUP APP
void setup() {
Serial.begin(9600);
pinMode(pin,INPUT);
Serial.println( " ------------------------ ");
Serial.println( " - SETUP_COMPLETED - ");
Serial.println( " ------------------------ ");
}
//************************************************************************
//ENTERFRAME
void loop() {
long error = 0;
int sample_cnt = 10;
unsigned long sample = 0;
unsigned long duration = 0;
float start_time = micros();
for (int i=0; i <= sample_cnt-1; i++){
sample = pulseIn(pin, LOW,60000000); //needs timeout or it does time out
duration = duration + sample;
Serial.print( i+1 ); //removing these serial commands, makes the program run slower, why?
Serial.print( ": " );
Serial.println(float(sample)/1000000,3);
if( sample==0){
error++;
};
} //END
float time_end = (micros()-float(start_time))/1000000;
Serial.print( "Sample Count: ");
Serial.print( sample_cnt );
Serial.print( " || Time: ");
Serial.print( time_end,3 );
Serial.print( " || Duration: ");
Serial.print(float(duration)/1000000,3);
Serial.print(" || Error: ");
Serial.print(error);
Serial.print(" || VCC: ");
Serial.print(float(readVccB())/1000,3);
Serial.print(" || TEMP: ");
Serial.println(float(GetTemp()),2);
//***********************************************
//CLOCK SPEED IS THE SAME : THIS IS FINE
long unsigned startTime = 0;
long unsigned endTime = 0;
long unsigned fullTime = 0;
startTime = millis();
benchmark();
endTime = millis();
fullTime = endTime - startTime;
Serial.print(" >> BENCHMARK FOR CLOCK SPEED: ");
Serial.println(fullTime);
}
Serial.print(float(readVccB())/1000,3);
error นี้น่าจะคอมไพล์ไม่ผ่าน หรือไม่มีฟังก์ชัน readVccB() ลองเช็คโคดอีกทีครับ
ขอบคุณมากครับ ตอนนี้ผมหันมาใช้โค้ดใหม่แล้วครับ
/* Wiring Instruction: */
/* - PPD42NS Pin 1 => GND */
/* - PPD42NS Pin 2 => D7 */
/* - PPD42NS Pin 3 => 5V */
/* - PPD42NS Pin 4 => D8 */
/**********************************************/
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
byte nano[8] = {B00000,B00000,B00000,B10010,B10010,B10010,B11110,B10000};
byte pow3[8] = {B11000,B00100,B11000,B00100,B11000,B00000,B00000,B00000};
#include <avr/wdt.h>
#define PM25 0
#define PM10 1
int pin[] = {7,8};
unsigned long starttime;
unsigned long sampletime_ms = 3000;
unsigned long triggerOn[2];
unsigned long triggerOff[2];
unsigned long lowpulseoccupancy[] = {0,0};
float ratio[] = {0,0};
float count[] = {0,0};
boolean value[] = {HIGH,HIGH};
boolean trigger[] = {false, false};
void setup()
{
lcd.createChar(0,nano);
lcd.createChar(1,pow3);
lcd.begin(16, 2);
Serial.begin(9600); //Output to Serial at 9600 baud
pinMode(pin[PM25],INPUT); //Listen at the designated PIN
wdt_enable(WDTO_8S);
starttime = millis(); //Fetching the current time
}
void loop()
{
value[PM25] = digitalRead(pin[PM25]);
value[PM10] = digitalRead(pin[PM10]);
if(value[PM25] == LOW && trigger[PM25] == false) {
trigger[PM25] = true;
triggerOn[PM25] = micros();
}
if(value[PM25] == HIGH && trigger[PM25] == true) {
triggerOff[PM25] = micros();
lowpulseoccupancy[PM25] += (triggerOff[PM25] - triggerOn[PM25]);
trigger[PM25] = false;
}
if(value[PM10] == LOW && trigger[PM10] == false) {
trigger[PM10] = true;
triggerOn[PM10] = micros();
}
if(value[PM10] == HIGH && trigger[PM10] == true) {
triggerOff[PM10] = micros();
lowpulseoccupancy[PM10] += (triggerOff[PM10] - triggerOn[PM10]);
trigger[PM10] = false;
}
wdt_reset();
if ((millis()-starttime) > sampletime_ms)//Checking if it is time to sample
{
ratio[PM25] = lowpulseoccupancy[PM25]/(sampletime_ms*10.0);
count[PM25] = 1.1*pow(ratio[PM25],3)-3.8*pow(ratio[PM25],2)+520*ratio[PM25]+0.62;
ratio[PM10] = lowpulseoccupancy[PM10]/(sampletime_ms*10.0);
count[PM10] = 1.1*pow(ratio[PM10],3)-3.8*pow(ratio[PM10],2)+520*ratio[PM10]+0.62;
count[PM25] -= count[PM10];
// Begin mass concentration calculation
float concentration[] = {0,0};
double pi = 3.14159;
double density = 1.65*pow(10,12);
double K = 3531.5;
// PM10
double r10 = 2.6*pow(10,-6);
double vol10 = (4/3)*pi*pow(r10,3);
double mass10 = density*vol10;
concentration[PM10] = (count[PM10])*K*mass10;
// PM2.5
double r25 = 0.44*pow(10,-6);
double vol25 = (4/3)*pi*pow(r25,3);
double mass25 = density*vol25;
concentration[PM25] = (count[PM25])*K*mass25;
// End of mass concentration calculation
// Begin printing to Serial
Serial.print("PM10 : ");
Serial.print(concentration[PM10]);
Serial.println(" ug/m3");
Serial.print("PM10 Count : ");
Serial.print(count[PM10]);
Serial.println(" pt/cf");
Serial.print("PM2.5 : ");
Serial.print(concentration[PM25]);
Serial.println(" ug/m3");
Serial.print("PM2.5 Count: ");
Serial.print(count[PM25]);
Serial.println(" pt/cf");
Serial.println("");
// Begin printing to LCD
lcd.clear();
lcd.print("10 : ");
lcd.print(concentration[PM10]);
lcd.setCursor(11,0);
lcd.write(byte(0));
lcd.print("g/m");
lcd.write(byte(1));
lcd.setCursor(0,1);
lcd.print("2.5: ");
lcd.print(concentration[PM25]);
lcd.setCursor(11,1);
lcd.write(byte(0));
lcd.print("g/m");
lcd.write(byte(1));
// Resetting for next sampling
lowpulseoccupancy[PM25] = 0;
lowpulseoccupancy[PM10] = 0;
starttime = millis();
wdt_reset();
}
}
แต่ที่ได้มันไม่เปลี่ยนแปลงเลยครับ แม้จะเผากระดาษ หรือควันเทียน
จึงอยากทราบว่าเป็นที่ตัว sensor หรือตัวโค้ดหน่ะครับ
ต้องขออภัยตัวนี้ผมยังไม่มีข้อมูล แนะนำอย่าเพิ่งเอาโคดที่ไม่จำเป็นมาใส่ ต้องลองเช็คโคดทีละส่วนครับ
http://www.elecrow.com/wiki/index.php?title=Dust_Sensor-_DSM501A
คือผมสั่งซื้ออุปกรณ์ชิ้นนี้จากทางร้าน สามารถส่งกลับคืนเพื่อตรวจสอบว่าอุปกรณ์บกพร่องรึเปล่าได้หรือไม่ เนื่องจากโค้ดที่นำมาสามารถใช้ได้ครบถ้วนแต่เหมือนตัว sensor จะไม่ยอมทำงานตรวจจับหน่ะครับ
เลขที่รายการสั่งซื้อ #34018Q ครับ
คอมไพล์ผ่านรันได้ ไม่ได้แปลว่าสามารถทำงานได้ครบถ้วนนะครับ โคดประกอบด้วยอุปกรณ์หลายชิ้น อย่างที่แนะนำไป อยากให้ให้ทดลองทีละส่วนก่อนนะครับ ขอดูรูปประกอบด้วยเผื่อจะได้แนะนำได้