Walking in the Dark

Background: For people who have migrane, it is terribly painful to turn on the light in the room (any light is unbearable at times) or have sound playing, talking would be painful. Even though this doesn’t mean they can’t open their eyes to see things. However, the light and sound sesitivity make it really difficult for them to walk around in the house for certain necesaties.

Therefore, my project is to help people when they have to walk in the dark, and lights cannot be used as an indicator. Instead of lights, I used buzzer as indicator. If the person is too close to the wall on the left, the buzzer on the left (stick on the person’s hand) will vibrate. Same goes to the buzzer on the right. However, if they have a pet at home, the pir sensor attached to their chest will be active and the buzzer will both buzz at a certain meter.

[Insert picture here]

 

Here is the code part of it:


/* Jean Zhang


*/


const int leftDistPin = A1;
const int rightDistPin = A2;
const int pirPin = 8;

const int buzzLeft = 6;
const int buzzRight = 10;

const int critDist = 950; // somewhat arbitrary number

int leftDist = 0;
int rightDist = 0;

int buzzState = 0;
int buzzStatePrev = 0;


bool isPet = false;


void petPresent() {
 if (digitalRead(pirPin == HIGH)) {
 isPet = !isPet;
 }
 Serial.println(isPet);
}


void determineState() {

if (leftDist > critDist) {
 if (rightDist > critDist) {
 buzzState = 1;
 }
 else {
 buzzState = 2;
 }
 }
 else {
 if (rightDist > critDist) {
 buzzState = 3;
 }
 else {
 buzzState = 4;
 }
 }


 if (isPet == true) {
 buzzState = 5;
 }
}

void setup() {
 // put your setup code here, to run once:

Serial.begin(9600);

pinMode(leftDistPin, INPUT);
 pinMode(rightDistPin, INPUT);
 pinMode(pirPin, INPUT);

pinMode(buzzLeft, OUTPUT);
 pinMode(buzzRight, OUTPUT);


 //attach Interrupt
 if (isPet == HIGH) {
 attachInterrupt( digitalPinToInterrupt (pirPin), petPresent, HIGH);
 }
}


void loop() {
 // put your main code here, to run repeatedly:

//read distance and compare to critDist
 leftDist = analogRead(leftDistPin);
 rightDist = analogRead(rightDistPin);

buzzStatePrev = buzzState;
 determineState();

Serial.print("left");
 Serial.print(leftDist);
 Serial.print(" right");
 Serial.println(rightDist);
 Serial.print("buzzState");
 Serial.println(buzzState);

if (buzzStatePrev != buzzState) {
 switch (buzzState) {
 case 1: break;
 case 2:
 digitalWrite(buzzRight, HIGH);
 break;
 case 3:
 digitalWrite(buzzLeft, HIGH);
 break;
 case 4:
 digitalWrite(buzzRight, HIGH);
 digitalWrite(buzzLeft, HIGH);
 break;
 case 5:
 digitalWrite(buzzRight, HIGH);
 digitalWrite(buzzLeft, HIGH);
 delay (500); //@TODO: write a function that does not use delay
 digitalWrite(buzzRight, LOW);
 digitalWrite(buzzLeft, LOW);
 delay (500); // Yeah, try not to use delay
 break;


 }
 }


}

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.