//#include "Arduino.h" //included in DHT.h #include //DHT Lib dependency #include const int Trigger_Pin = 2; const int Echo_PIN = 3; const int Buzzer_Pin = 4; const int DHT_Pin = 5; const int RedLED_Pin = 6; int iBuzzerFreq; float fDistance; float fTemp; float fHumidity; float fBattery; int LED_State = LOW; unsigned long lgUpdateTime; const byte SLAVE_ADDRESS = 42; DHT dht(5, DHT11); void setup() { Serial.begin(9600); //Default communication channel- //On Startup - Play Buzzer Tune indicating successful startup to the user. runStartupTune(); dht.begin(); pinMode(Trigger_Pin, OUTPUT); pinMode(Echo_PIN, INPUT); pinMode(Buzzer_Pin, OUTPUT); pinMode(RedLED_Pin, OUTPUT); lgUpdateTime = millis(); } void loop() { collectDistance(Trigger_Pin, Echo_PIN, Buzzer_Pin, fDistance, iBuzzerFreq); fHumidity = dht.readHumidity(); fTemp = dht.readTemperature(); //releaseBuzzer(Distance, buzzerFreq); /* fBattery = collectBattery(); if(fBattery < 20) LED_Blink(500); else LED_State = HIGH; */ fBattery = 100.00; if(millis() - lgUpdateTime > 1500) //Loop send approx every 1.5 seconds { lgUpdateTime = millis(); Serial.print(fDistance,2); Serial.print("|"); Serial.print(fTemp,2); Serial.print("|"); Serial.print(fHumidity,2); Serial.print("|"); Serial.print(fBattery,2); Serial.println(); } } //http://www.martyncurrey.com/turning-a-led-on-and-off-with-an-arduino-bluetooth-and-android-part-ii-2-way-control/ //https://groups.google.com/g/mitappinventortest/c/F-9jyPUp72M/m/dAtp0TTcGgAJ //delimiter //https://groups.google.com/g/mitappinventortest/c/lVaCyAeWm_4/m/ADjvy05wAwAJ //test with array of bytes - search "Kind of. I have nothing to test the code" //https://community.appinventor.mit.edu/t/bluetooth-client-speed-and-buffering-issue/16064/18?page=2 int collectBattery() { //reads battery voltage and returns battery value int in a range of 0-100 int value = 0; float voltage; float perc; value = analogRead(A0); voltage = value * 3.2/1023; perc = map(voltage, 3.6, 4.2, 0, 100); return perc; } void releaseBuzzer(int Distance, int buzzerFreq) { /* Buzzer warning when an object is within maxDetection range of the device * REFERENCE CODE: tone(pin, frequency, duration) * CASE 1: no buzzer beeps when distance above max detection * distance > maxDetection * noTone(Buzzer_Pin); * * CASE 2: continuous buzzer beeps when distance lower than min detection * distance < minDetection -> * tone(Buzzer_Pin, 1800, buzzerFreq); * * CASE 3: beep based on buzzerFrequency when distance between max detection and min detection * minDetection < distance < maxDetection * tone(Buzzer_Pin, 1800, buzzerFreq); delay(buzzerFreq); */ int minDetect = 5; int maxDetect = 20; //***Buzzer Frequency Controller*** buzzerFreq = fDistance * 5; if (fDistance > maxDetect){ //CASE1: noTone(Buzzer_Pin); } else if(fDistance < minDetect){ //CASE2: buzzerFreq = 0; tone(Buzzer_Pin, 1800, buzzerFreq); } else{ //CASE3: tone(Buzzer_Pin, 1800, buzzerFreq); delay(buzzerFreq); } } void collectDistance(int, int, int, float &fDistance, int &buzzerFreq) { /* measures duration the signal takes to traverse from TriggerPin to EchoPin * get distance from duration and convert to metric */ long lgDuration; digitalWrite(Trigger_Pin, LOW); delayMicroseconds(2); digitalWrite(Trigger_Pin, HIGH); delayMicroseconds(10); digitalWrite(Trigger_Pin, LOW); lgDuration = pulseIn(Echo_PIN, HIGH); fDistance = (lgDuration * 0.034) / 2; } void runStartupTune() { /* to be used as indicator to the user that * the device succesfully booted */ tone(Buzzer_Pin, 660, 100); delay(75); tone(Buzzer_Pin, 660, 100); delay(150); tone(Buzzer_Pin, 660, 100); delay(75); tone(Buzzer_Pin, 660, 100); delay(500); } //void LED_Blink(int frequency) //{ // /* causes the led to blink based on the global variable // * unsigned long ledinterval = 500 found at the header of the file // */ // if (millis() - previousMillis > frequency){ // previousMillis2 = millis(); // if (LED_State == LOW) // LED_State = HIGH; // else // LED_State = LOW; // digitalWrite(6, LED_State); // } //} // //RSSI bullshit //https://community.appinventor.mit.edu/t/help-me-with-implementing-a-few-formulas-that-i-need-in-my-arduino-code/20946/2