Financial Management

So uhhh… I bought… A desk lamp… For a hundred dirhams. A hundred. A hundred as in 1-0-0.

Impulsive purchases got the best of me again.

Anyways, I decided I would use this lamp for something other than playing around with its colors. So I made this kind of proximity/light sensor thing. Basically when I hold the lamp close to the the circuit, the red LED turns off and as I back up the lamp, the red LED blinks slower and slower until it stops blinking. As long as the red LED blinks/is on, the green LED can be turned on using a digital switch. Initially while making this circuit, I ran into some very silly difficulties – I attached the components on a single row which meant the circuit was never complete and I couldn’t figure out the problem the entire night for the life of me. Here is the bad circuit:

But one debugging session later here is my creation, the good circuit:

It works in a very fun way and the lamp works in an even fun-er way. Check out the video I’ve attached of the LEDs in action:

https://drive.google.com/drive/folders/1uOQwTJqiPt6b5cQ-L8GTVn3CXHp50x17?usp=sharing

Here is the code:

int green = 10;
int red = 11;
// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);  // delay in between reads for stability

if (sensorValue > 40) { // when far, turn on but don't blink
  digitalWrite(green, LOW);
  digitalWrite(red, LOW);
}
if (sensorValue > 15 && sensorValue <= 40) { // when slightly close, blink slow
  digitalWrite(green, HIGH);
  digitalWrite(red, HIGH);
  delay(100);
  digitalWrite(red, LOW);
  delay(100);
}
if (sensorValue <= 15 && sensorValue > 10) { // when closer, blink fast
  digitalWrite(green, HIGH);
  digitalWrite(red, HIGH);
  delay(250);
  digitalWrite(red, LOW);
  delay(250);
} 
if (sensorValue <= 10) { // when very close, turn off
  digitalWrite(green, HIGH);
  digitalWrite(red, HIGH);
}  

}

 

Leave a Reply