수행기록퀘스트3
변수 및 라이브러리 설정
#include
#include
#include
int ledState = LOW;
unsigned long previousMillis = 0;
const long intervalLong = 1000;
const long intervalMed = 500;
const long intervalShort = 100;
float old_temp = 0;
float old_hum = 0;
long sensorsum = 0;
int n = 1;
int mean = 0;
int lastmean = 0;
// buffer to read samples into, each sample is 16-bits
short sampleBuffer[256];
// number of samples read
volatile int samplesRead;
센서 셋업
// The setup routine runs once when you press reset:
void setup() {
// Sets pin 13 for output in order to blink the LED:
pinMode(13, OUTPUT);
// Initialize serial communication at 9600 bits per second:
Serial.begin(9600);
if (!HTS.begin()) {
Serial.println("Failed to initialize humidity temperature sensor!");
while (1);
}
// configure the data receive callback
PDM.onReceive(onPDMdata);
// optionally set the gain, defaults to 20
// PDM.setGain(30);
// initialize PDM with:
// - one channel (mono mode)
// - a 16 kHz sample rate
if (!PDM.begin(1, 16000)) {
Serial.println("Failed to start PDM!");
while (1);
}
if (!APDS.begin()) {
Serial.println("Error initializing APDS9960 sensor!");
}
}
주 센서 처리 루틴
// The loop routine runs over and over again forever:
void loop() {
// Read the input on analog pin 0:
int sensorValue = analogRead(A0);
unsigned long currentMillis = millis();
delay(250);
// The "Mean" value will vary the whole loop beacuse it isalways calculating the mean with the read values
// The "Last Mean" value will only show the calculated mean value just to ease the reading of the calculated value
Serial.print(" sensorValue = ");
Serial.print(sensorValue);
Serial.print(" Last Mean = ");
Serial.println(lastmean);
delay(25);
// read all the sensor values
float temperature = HTS.readTemperature();
float humidity = HTS.readHumidity();
// check if the range values in temperature are bigger than 0,5 ºC
// and if the range values in humidity are bigger than 1%
if (abs(old_temp - temperature) >= 0.5 || abs(old_hum - humidity) >= 1 )
{
old_temp = temperature;
old_hum = humidity;
// print each of the sensor values
Serial.print("Temperature = ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity = ");
Serial.print(humidity);
Serial.println(" %");
Serial.println();
}
// print each of the sensor values
Serial.print("Temperature = ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity = ");
Serial.print(humidity);
Serial.println(" %");
// print an empty line
Serial.println();
/*
// wait for samples to be read
if (samplesRead) {
// print samples to the serial monitor or plotter
for (int i = 0; i < samplesRead; i++) {
Serial.println(sampleBuffer[i]);
// check if the sound value is higher than 500
if (sampleBuffer[i]>=500){
digitalWrite(LEDR,LOW);
digitalWrite(LEDG,HIGH);
digitalWrite(LEDB,HIGH);
}
// check if the sound value is higher than 250 and lower than 500
if (sampleBuffer[i]>=250 && sampleBuffer[i] < 500){
digitalWrite(LEDB,LOW);
digitalWrite(LEDR,HIGH);
digitalWrite(LEDG,HIGH);
}
//check if the sound value is higher than 0 and lower than 250
if (sampleBuffer[i]>=0 && sampleBuffer[i] < 250){
digitalWrite(LEDG,LOW);
digitalWrite(LEDR,HIGH);
digitalWrite(LEDB,HIGH);
}
}
// clear the read count
samplesRead = 0;
}
*/
// check if a proximity reading is available
if (APDS.proximityAvailable()) {
// read the proximity
// - 0 => close
// - 255 => far
// - -1 => error
int proximity = APDS.readProximity();
if (proximity > 150) {
if (currentMillis - previousMillis >= intervalLong) {
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the green LED with the ledState of the variable and turn off the rest
digitalWrite(LEDG, ledState);
digitalWrite(LEDR, HIGH);
digitalWrite(LEDB, HIGH);
}
}
else if(proximity > 50 && proximity <= 150){
if (currentMillis - previousMillis >= intervalMed) {
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the blue LED with the ledState of the variable and turn off the rest
digitalWrite(LEDB, ledState);
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
}
}
else {
if (currentMillis - previousMillis >= intervalShort) {
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the blue LED with the ledState of the variable and turn off the rest
digitalWrite(LEDR, ledState);
digitalWrite(LEDB, HIGH);
digitalWrite(LEDG, HIGH);
}
}
// print value to the Serial Monitor
Serial.println(proximity);
}
}
void onPDMdata() {
// query the number of bytes available
int bytesAvailable = PDM.available();
// read into the sample buffer
PDM.read(sampleBuffer, bytesAvailable);
// 16-bit, 2 bytes per sample
samplesRead = bytesAvailable / 2;
}
- 첨부파일
- quest3-20220625.pptx 다운로드
로그인 후
참가 상태를 확인할 수 있습니다.