Week 9 – Ghost Detector

Concept

For this project, I decided to detect ghosts (or any other presence) using an ultrasonic sensor and a red “SOS” LED, and transmit a message saying “hi” using Morse code on a yellow LED. I looked up the Morse codes from this website. The circuit diagram for my ghost detector can be found below:

Implementation

I used 2 sensors for this project: an LDR and an ultrasonic sensor.
The LDR detects if the surroundings are dark enough and the ultrasonic sensor detects if there is “something” nearby ­čÖé

If the ultrasonic sensor detects something is near, and if the surroundings are dark, the red LED blinks an SOS signal in Morse code. While the red LED is flashing, you know something is nearby, that’s where the yellow switch comes in. When we press the yellow switch, the yellow LED starts blinking “hi” in Morse code to give a friendly message to whatever was detected by the ultrasonic sensor. While the yellow LED is flashing, the red LED doesn’t flash an SOS signal because we wish to portray a positive vibe to our new friend.

The digital part of the circuit consists of the switch that reads digital signals and turns the yellow LED on and the red LED off. The ultrasonic sensor feeds in pulse data through its ECHO pin, which is then converted into a float distance value using a simple equation. The analog part consists of an LDR which is used to detect whether it is dark enough for the red SOS to start blinking. I did an analog read to get lighting info from this sensor and used it inside the if condition for the red LED.

To implement the “SOS” and “hi” signals in the two LEDs, I made use of two functions. I utilized the idea we discussed in class where we made an LED blink. I used a simple for loop to make the LED blink the required number of times and adjusted the delays between blinks to make the LED blink fast or slow. This allowed me to depict any letter in the Morse code using my LEDs. Once this was done, I caught the readings from the sensors and the switch to make the LEDs blink accordingly.

The code for my project can be found below:

int pushButton = 4;    // for the push button switch
int red_ledPin = 11;   // Define the red LED pin number
int yellow_ledPin = 8; // Define the yellow LED pin number

const int TRIG_PIN = 10;           // Arduino pin connected to Ultrasonic Sensor's TRIG pin
const int ECHO_PIN = 12;           // Arduino pin connected to Ultrasonic Sensor's ECHO pin
const int DISTANCE_THRESHOLD = 50; // in centimeters

// distance calculation variables for ultrasonic sensor:
float duration_us, distance_cm;

// the setup routine runs once when you press reset:
void setup()
{
    pinMode(red_ledPin, OUTPUT);    // set the red LED to output mode
    pinMode(yellow_ledPin, OUTPUT); // set the yellow LED to output mode
    pinMode(TRIG_PIN, OUTPUT);      // set arduino pin to output mode
    pinMode(ECHO_PIN, INPUT);       // set arduino pin to input mode

    // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
    // make the pushbutton's pin an input:
    pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop()
{
    // send a 10us pulse to the ultrasonic sensor
    digitalWrite(TRIG_PIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIG_PIN, LOW);

    // measure duration of pulse from ECHO pin
    duration_us = pulseIn(ECHO_PIN, HIGH);
    // calculate the distance
    distance_cm = 0.017 * duration_us;

    // read the input pin:
    int buttonState = digitalRead(pushButton);
    int sensorValue = analogRead(A0);

    // if the button is not pressed, the distance is closer than 15cm, and it is dark enough, then blink SOS
    if (buttonState == HIGH && distance_cm < 15 && sensorValue > 800)
    {
        blinkSOS(red_ledPin);
    }
    // otherwise if the button is pressed, then blink "hi" on the yellow LED
    else if (buttonState == LOW)
    {
        digitalWrite(red_ledPin, LOW);
        blinkHi(yellow_ledPin);
    }

    // for debugging: 

    // // print out the state of the button:
    // Serial.println(buttonState);
    // delay(1); 

    // // print the value to Serial Monitor
    // Serial.print("distance: ");
    // Serial.print(distance_cm);
    // Serial.println(" cm");

    // delay(10);

    // Serial.println(sensorValue);
    // delay(1);
}

// Morse code for "SOS": ... --- ...
void blinkSOS(int ledNum)
{
    // fast blink thrice for "..."
    for (int i = 0; i < 3; i++)
    {
        digitalWrite(ledNum, HIGH); // turn the LED on (HIGH is the voltage level)
        delay(150);                 // wait for a second
        digitalWrite(ledNum, LOW);  // turn the LED off by making the voltage LOW
        delay(150);                 // wait for a second
    }
    // wait between pattern switch
    delay(500);
    // slow blink for "---"
    for (int i = 0; i < 3; i++)
    {
        digitalWrite(ledNum, HIGH); // turn the LED on (HIGH is the voltage level)
        delay(400);                 // wait for a second
        digitalWrite(ledNum, LOW);  // turn the LED off by making the voltage LOW
        delay(400);                 // wait for a second
    }
    // wait between pattern switch
    delay(500);
}

// Morse code for "hi": .... ..
void blinkHi(int ledNum)
{
    // blink 4 times for "...."
    for (int i = 0; i < 4; i++)
    {
        digitalWrite(ledNum, HIGH); // turn the LED on (HIGH is the voltage level)
        delay(150);                 // wait for a second
        digitalWrite(ledNum, LOW);  // turn the LED off by making the voltage LOW
        delay(150);                 // wait for a second
    }
    // wait between pattern switch
    delay(500);
    // blink twice for ".."
    for (int i = 0; i < 2; i++)
    {
        digitalWrite(ledNum, HIGH); // turn the LED on (HIGH is the voltage level)
        delay(150);                 // wait for a second
        digitalWrite(ledNum, LOW);  // turn the LED off by making the voltage LOW
        delay(150);                 // wait for a second
    }
    // wait between pattern switch
    delay(500);
}

Challenges

I spent a lot of time on this project playing around with wires and experimenting, especially when I was running into unusual results. The difficult part though, was that in order to test a simpler, shorter circuit to clear my confusion, I had to take apart the larger circuit I was working on since we only have one breadboard. At times I realized I was making a very small error which was almost impossible to notice in the larger circuit which had a ton of wires all over the board. Drawing the circuit diagram before implementing the physical circuit was helpful but at times I was confused as to which end a resistor would go to or if it would even make a difference. Whenever I was stuck somewhere, I tried looking at circuit diagrams for simpler, smaller circuits. For example, when I was stuck at implementing the yellow switch and was tired of trying out different connections to make it work, I looked up a simple basic circuit for a switch with an LED and tried to implement that into my larger circuit, and this proved very helpful.

The Ghost Detector

 

Leave a Reply