/*
Name Project : Smart Glasses for blind person
Hardware :
1- Arduino uno
2- breadBoard
3- Two Leds (Red - Green)
4- Two resistor
5- Stepper for vibration
6- Button power
7- buzzer speaker
*/
// defines variables pin for front Sensor
#define echoPinFront 2 // Echo pin sensor
#define trigPinFront 3 // Triger pin sensor
// define variables pin for left Sensor
#define echoPinLeft 2 // Echo pin sensor
#define trigPinLeft 5 // Triger pin sensor
// define variables pin for right Sensor
#define echoPinRight 2 // Echo pin sensor
#define trigPinRight 6 // Triger pin sensor
// stepper pins connected
#define Ap 9 // A+ line
#define Am 11 // A- line
#define Bp 7 // B+
#define Bm 4 // B-
// variables Needed
int buzzerPin = 8; // Buzzer pin
int LedPin = 12; // Led pin contact
int LedPower = 10; // Led Power pin
int Button = 13; // power button
int iCount = 0 ; // Begin from Zero for count Stepper
float rangeOne = 200.00; // this variable number for first range avalue from sensor
float rangeTwo = 100.00; // this variable number for second range avalue from sensor
void setup() {
Serial.begin(115200); // begin Serial
// initialize Button and Led Power
pinMode(Button, INPUT);
pinMode(LedPower, OUTPUT);
digitalWrite(Button, LOW);
// initialize front Sensor mode
pinMode(trigPinFront, OUTPUT);
pinMode(echoPinFront, INPUT);
// initialize left Sensor mode
pinMode(trigPinLeft, OUTPUT);
pinMode(echoPinLeft, INPUT);
// initialize right Sensor mode
pinMode(trigPinRight, OUTPUT);
pinMode(echoPinRight, INPUT);
// set output
pinMode(Ap,OUTPUT);
pinMode(Am,OUTPUT);
pinMode(Bp,OUTPUT);
pinMode(Bm,OUTPUT);
// initialize buzzer mode
pinMode(buzzerPin, OUTPUT);
// initialize Led mode
pinMode(LedPin, OUTPUT);
}
float readDistanceCMFront() { // this function to read a value from front sensor
digitalWrite(trigPinFront, LOW);
delayMicroseconds(2);
digitalWrite(trigPinFront, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinFront, LOW);
int duration = pulseIn(echoPinFront, HIGH);
return duration * 0.034 / 2;
}
float readDistanceCMLeft() { // this function to read a value from left sensor
digitalWrite(trigPinLeft, LOW);
delayMicroseconds(2);
digitalWrite(trigPinLeft, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinLeft, LOW);
int duration = pulseIn(echoPinLeft, HIGH);
return duration * 0.034 / 2;
}
float readDistanceCMRight() { // this function to read a value from right sensor
digitalWrite(trigPinRight, LOW);
delayMicroseconds(2);
digitalWrite(trigPinRight, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinRight, LOW);
int duration = pulseIn(echoPinRight, HIGH);
return duration * 0.034 / 2;
}
void loop() {
// assign Strings to Output distance on Serial front - left - right
String frontNumber = String(readDistanceCMFront());
String distanceStringOne = String("Front : " + frontNumber);
String leftNumber = String(readDistanceCMLeft());
String distanceStringTwo = String("Left : " + leftNumber);
String rightNumber = String(readDistanceCMRight());
String distanceStringThree = String("Right : " + rightNumber);
Serial.println("--------------------");
Serial.println("Measured distance:");
Serial.println(distanceStringOne);
Serial.println(distanceStringTwo);
Serial.println(distanceStringThree);
onClickButton();
checkFrontSensor(rangeOne, rangeTwo); // call check front sensor function for front sensor
checkLeftSensor(rangeOne, rangeTwo); // call check left sensor function for front sensor
checkRightSensor(rangeOne, rangeTwo); // call check right sensor function for front sensor
delay(100);
}
void BlinkLed() { // this function blink the led in port 12
digitalWrite(LedPin, HIGH);
delay(250);
digitalWrite(LedPin, LOW);
delay(200);
digitalWrite(LedPin, HIGH);
delay(100);
digitalWrite(LedPin, LOW);
delay(100);
}
void openLed() { // this function to open led
digitalWrite(LedPin, HIGH);
}
void onClickButton() {
if (digitalRead(Button) == LOW) {
digitalWrite(Button, LOW);
digitalWrite(LedPower, LOW);
}
if (digitalRead(Button) == HIGH) {
digitalWrite(Button, HIGH);
digitalWrite(LedPower, HIGH);
BlinkLed() ; // call the blink led function
}
}
void checkFrontSensor(float x, float y) { // this function to check if a front Sensor detected from two space
float distance = readDistanceCMFront();
if (distance <= x) { // check if distance in the rang 100 CM
tone(buzzerPin, 260, 300); // Plays sound 260Hz tone for 0.200 seconds
openLed();
delay(1000);
rotateStepper(iCount);
}
if (distance <= y) {
tone(buzzerPin, 260, 100); // Plays sound 260Hz tone for 0.20 seconds
delay(200);
tone(buzzerPin, 260, 100); // Plays sound 260Hz tone for 0.500 seconds
openLed(); // call open led fuction
rotateStepper(iCount);
}
}
void checkLeftSensor(float x, float y) { // this function to check if a left Sensor detected from two space
float distance = readDistanceCMLeft();
if (distance <= x) { // check if distance in the rang 100 CM
tone(buzzerPin, 260, 300); // Plays sound 260Hz tone for 0.200 seconds
openLed();
delay(1000);
rotateStepper(iCount);
}
if (distance <= y) {
tone(buzzerPin, 260, 100); // Plays sound 260Hz tone for 0.20 seconds
delay(200);
tone(buzzerPin, 260, 100); // Plays sound 260Hz tone for 0.500 seconds
openLed(); // call open led fuction
rotateStepper(iCount);
}
}
void checkRightSensor(float x, float y) { // this function to check if a right Sensor detected from two space
float distance = readDistanceCMRight();
if (distance <= x) { // check if distance in the rang 100 CM
tone(buzzerPin, 260, 300); // Plays sound 260Hz tone for 0.200 seconds
openLed();
delay(1000);
rotateStepper(iCount);
}
if (distance <= y) {
tone(buzzerPin, 260, 100); // Plays sound 260Hz tone for 0.20 seconds
delay(200);
tone(buzzerPin, 260, 100); // Plays sound 260Hz tone for 0.500 seconds
openLed(); // call open led fuction
rotateStepper(iCount);
}
}
void rotateStepper(int a) { // this function to rotate a stepper
// This is Wave Fullstep motion - only one coil energized at a time
if ( a++ <200 ) { // stop after 200*4 steps (=100) - quarter turn with gearRatio "2:1"
delay(10);
digitalWrite(Bm,LOW) ;
digitalWrite(Ap,HIGH);
delay(10);
digitalWrite(Ap,LOW) ;
digitalWrite(Bp,HIGH);
delay(10);
digitalWrite(Bp,LOW) ;
digitalWrite(Am,HIGH);
delay(10);
digitalWrite(Am,LOW) ;
digitalWrite(Bm,HIGH);
} else while(1);
}