Concept
I wanted to create a device to detect whether or not a cup was removed from its place. To do this, there is an LDR beneath the cup, and a button that sets the resting value of the LDR when the cup is placed on top. When the cup is removed, the value of the LDR increases and so the red LED turns on to indicate that the cup has been tampered with. A yellow LED turns on to indicate that the resting value of the LDR is being set.
Demonstration
The video demonstrates the above concept.
Code
#include <Arduino.h> const int redLEDPin = 7; const int buttonPin = 2; const int LDRPin = A0; const int yellowLEDPin = 8; int restingValue = 0; void setup() { pinMode(redLEDPin, OUTPUT); pinMode(yellowLEDPin, OUTPUT); pinMode(buttonPin, INPUT); pinMode(LDRPin, INPUT); Serial.begin(9600); digitalWrite(redLEDPin, HIGH); //initially turn on the red LED digitalWrite(yellowLEDPin, LOW); //initially turn off the yellow LED } void loop() { //read and print the value of the LDR int LDRValue = analogRead(LDRPin); int buttonValue = digitalRead(buttonPin); //round the value to the nearest 100 for more consistent readings LDRValue = round(LDRValue / 100) * 100; //button is used to reset the resting value of the LDR if(buttonValue == HIGH) { digitalWrite(yellowLEDPin, HIGH); //to indicate that the button is pressed and the LDR resting value is being reset restingValue = LDRValue; return; } digitalWrite(yellowLEDPin, LOW); //turn off the yellow LED //for debugging Serial.println(LDRValue); //turn on the LED if the button is pressed if (LDRValue <= restingValue) { digitalWrite(redLEDPin, LOW); } else { digitalWrite(redLEDPin, HIGH); // ISSUE! TURN ON LED!! CUP IS MISSING!! } }
Technical Structure
This device is an amalgamation of different individual circuits. There is a sub-circuit to take analog input from a momentary switch. There are two LED circuits that take analog input in, so the LEDs only have HIGH and LOW states. There is one analog sensor, which is the LDR, to determine whether or not the cup has been moved.
Reflection
I realize that this device is not very useful if used in the dark. Since it is hard to see if someone messes with someone in the dark by naked eye, this would be a situation where it would be good to use this device. Since it fails in this particular case, I feel that this is a missed opportunity, and so the device isn’t particularly useful in real world applications. To make a device better suited to the task, I could use an infra-red sensor, or an ultrasonic sensor, so that proximity from the cup to the sensor is reached. However, that sensor is prone to being subdued by a replacement object. I am currently thinking about a foolproof device to protect the cup from being tampered with or taken away.