Proximity Measurer

Chelsea James

IDM 382: Internet of Things
Fall 2020

In today’s world, COVID-19 is rampid and it is in our hands to stay safe and do our best to protect ourselves from it. The Proximity Measurer is a way to determine if you are too close to someone. This Proximity Measurer uses an ultrasonic sensor to determine the distance of a person by using sound waves. If you are within 6 feet (about 183 cm), the device with flash red and you will get a “Too close!” warning message that states your distance. If you are beyond 6 feet, the green light will flash, indicating that you are a safe distance away and you will get a “Safe!” message that has your distance in centimeters.


Goals

  • Track distance of a user
  • Flash red & green lights
  • Display warning messages
  • Allow the user to see light, warning message, and distance on the cloud

Final Presentation


Lesson Learned

I was able to add the finishing touches to my project. I ran into many issues before getting to this point and I learned a lot about the process of troubleshooting. I did some experimenting with my project and I even tried it on an Arduino Uno which helped me figure out what my issue was. It took a lot of trial and error to find out what my issues were but once I got it working, it felt very rewarding.

I have had issues with my project for weeks. I tried troubleshooting many different ways and I still couldn’t get it to work. I thought my project wouldn’t work at all and I was ready to use a backup project that I had done a few years ago. The issue that I found was that my ultrasonic sensor is an analog sensor and it did not work in the digital pins. Since it is analog it worked on the Marker 1010’s analog pins (A1, A2 etc.) I’m not entirely sure why it only works on analog pins because when I tested it on my Arduino Uno it worked on analog pins.


Sample Code

    #include "thingProperties.h"

    // sensor
    int cm = 0;
    int distance = 0;

    long readUltrasonicDistance(int triggerPin, int echoPin)
    {
      pinMode(triggerPin, OUTPUT);  
      digitalWrite(triggerPin, LOW);
      delayMicroseconds(2);
      // Sets the trigger pin to HIGH state for 10 microseconds
      digitalWrite(triggerPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(triggerPin, LOW);
      pinMode(echoPin, INPUT);
      // Reads the echo pin, and returns the sound wave travel time in microseconds
      return pulseIn(echoPin, HIGH);
    }

    //CUSTOM TIMERS
    long lastMessageTime = 0;
    long messagedDelay = 3000;

    String lastMessageTimeStr = "";

    void setup() {
      // Initialize serial and wait for port to open:
      Serial.begin(9600);
      // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
      delay(1500); 
      pinMode(5, OUTPUT);
      // pinMode(6, OUTPUT);
      pinMode(7, OUTPUT);
      // Defined in thingProperties.h
      initProperties();
      // Connect to Arduino IoT Cloud
      ArduinoCloud.begin(ArduinoIoTPreferredConnection);
      /*
         The following function allows you to obtain more information
         related to the state of network and IoT Cloud connection and errors
         the higher number the more granular information you’ll get.
         The default is 0 (only errors).
         Maximum is 4
     */
      setDebugMessageLevel(2);
      ArduinoCloud.printDebugInfo();
    }

    void loop() {
      ArduinoCloud.update();
      // Your code here 
      {
          cm = 0.01723 * readUltrasonicDistance( A1, A2);
          Serial.print(cm);
          Serial.println(" cm");
          // if the distance is further than 350 turn LED off
          //350cm = 11.5 feet
          if (cm > 350) {
            digitalWrite(5, HIGH);
            digitalWrite(6, LOW);
            digitalWrite(7, LOW);
          }
          // GREEN ON = SAFE
          //250cm= 8.2 ft
          if (cm <= 350 && cm > 183) {
            digitalWrite(5, HIGH);
            digitalWrite(6, LOW);
            digitalWrite(7, LOW);
          }
          //RED ON= NOT SAFE
          //6 feet= 182.88 cm
          if (cm <= 183 && cm > 0) {
            digitalWrite(5, LOW);
            digitalWrite(6, LOW);
            digitalWrite(7, HIGH);
          }
          // RED ON= REALLY NOT SAFE
          if (cm <= 0) {
            digitalWrite(5, LOW);
            digitalWrite(6, LOW);
            digitalWrite(7, HIGH);
          }
         }

      //RED light switch
      if (cm <= 183 && cm > 0) {
       lightswitch = false;
       measurer =  lastMessageTimeStr +"Too close!! Your distance is " + cm + "cm!";
       messagedDelay;
      } else {
       lightswitch = true;
       measurer =  lastMessageTimeStr +"Safe! Your distance is " + cm + "cm!";
       messagedDelay;
      }
    }
    //END LOOP

    void onMeasurerChange() {
      // Do something
    }
    void onLightswitchChange() {
      // Do something
    }
Back to Top