0

Can anyone figure it out for me? I am new to arduino and 1sheeld, Completely beginner. In my project(Smart Cane For Blind) has currently two part. One is object’s distance detection by using ultrasonic sensor and another feature is Emergency feature. When the blind people lost his way he can press the push button to sent his location to his family member for help.

My problem is that when I integrate that two feature, The sensor part, the detection is too slow and weak.

#define CUSTOM_SETTINGS
#define INCLUDE_GPS_SHIELD
#define INCLUDE_SMS_SHIELD
#define INCLUDE_VIBRATION_SHIELD
#define INCLUDE_PUSH_BUTTON_SHIELD
#define INCLUDE_TERMINAL_SHIELD
#define INCLUDE_MIC_SHIELD

/* Include 1Sheeld library.*/
#include <OneSheeld.h>
#include <string.h>

// Define Pins for Sensor 1 
#define trigPin1 4
#define echoPin1 5

//Define Pins for Sensor 2
#define trigPin2 6
#define echoPin2 7

// Define Pin for VibMotor 1
#define motorPin1 12

// Define Pin for VibMotor 2
#define motorPin2 13

/* Define a boolean flag. */
long duration, distance, UltraSensor, UltraSensorTwo;
boolean isMessageSent = false;
float lat;
float lon;
String Message;
String Message1;
String Message2;
char latitude[10];
char longitude[10];

void setup() 
{
  /* Start communication.*/
  Serial.begin(9600);
  OneSheeld.begin();
//  PushButton.setOnButtonStatusChange(&Emergency);

  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);

  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);

  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
}

void SonarSensor(int trigPin, int echoPin)
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
}

void SensorHandler() {

  SonarSensor(trigPin1, echoPin1);
  UltraSensor = distance;

  SonarSensor(trigPin2, echoPin2);
  UltraSensorTwo = distance;

  if (UltraSensor <= 60 && UltraSensor >= 45) // Checking the distance, you can change the value
  {
    digitalWrite(motorPin1, HIGH); // motor on
    delay(200);
    digitalWrite(motorPin1, LOW); // motor off
    delay(200);
  } 
  else if(UltraSensor < 45 && UltraSensor >=30)
  {
    digitalWrite(motorPin1, HIGH); // when greater than 100cm
    delay(100);
    digitalWrite(motorPin1, LOW);
    delay(100);
  } 
  else if(UltraSensor < 30) 
  {
   digitalWrite(motorPin1, HIGH);
   delay(50);
   digitalWrite(motorPin1, LOW);
   delay(50); 
  }
  else
  {
    digitalWrite(motorPin1, LOW);
  }

  Terminal.print("SensorOne: ");
  Terminal.println(UltraSensor);

    // For VibMotor Two

  if (UltraSensorTwo <= 60 && UltraSensorTwo >= 45) // Checking the distance, you can change the value
  {
    digitalWrite(motorPin2, HIGH); // motor on
    delay(200);
    digitalWrite(motorPin2, LOW); // motor off
    delay(200);
  } 
  else if(UltraSensorTwo < 45 && UltraSensorTwo >=30)
  {
    digitalWrite(motorPin2, HIGH); // when greater than 100cm
    delay(100);
    digitalWrite(motorPin2, LOW);
    delay(100);
  } 
  else if(UltraSensorTwo < 30) 
  {
   digitalWrite(motorPin2, HIGH);
   delay(50);
   digitalWrite(motorPin2, LOW);
   delay(50); 
  }
  else
  {
    digitalWrite(motorPin2, LOW);
  }

  Terminal.print("SensorTwo: ");
  Terminal.println(UltraSensorTwo);
}         

void loop()
{ 
//    Terminal.println("Test");
//    Emergency();

    SensorHandler();
    //delay(10);
    if (PushButton.isPressed())
  {
    Terminal.println("Button Pressed");
    //delay(100);
        lat = GPS.getLatitude();
        lon = GPS.getLongitude();
        Message1 = String(lat,8);
        Message2 = String(lon,8);

        Message = "I need Help! Google Maps - https://www.google.co.in/maps/place/" + Message1 + "," + Message2;
        Terminal.print("Message Sent");

//        SMS.send("0819033655",Message);
    delay(3000);
  }
  delay(1000);
}

The following code is after I fixed by learning BlinkWithoutDelay example but the vibration rate according to the distance seems not right. (When the object is closer, the vibration motor vibrate rate should be faster).

#define CUSTOM_SETTINGS
#define INCLUDE_GPS_SHIELD
#define INCLUDE_SMS_SHIELD
#define INCLUDE_VIBRATION_SHIELD
#define INCLUDE_PUSH_BUTTON_SHIELD
#define INCLUDE_TERMINAL_SHIELD
#define INCLUDE_MIC_SHIELD

/* Include 1Sheeld library.*/
#include <OneSheeld.h>
#include <string.h>

// Define Pins for Sensor 1
#define trigPin1 4
#define echoPin1 5

//Define Pins for Sensor 2
#define trigPin2 6
#define echoPin2 7

// Define Pin for VibMotor 1
#define motorPin1 12

// Define Pin for VibMotor 2
#define motorPin2 13

/* Define a boolean flag. */
long duration, distance, UltraSensor, UltraSensorTwo;
//boolean isMessageSent = false;
unsigned long previousMillis = 0;
unsigned long interval = 1000;
float lat;
float lon;
String Message;
String Message1;
String Message2;
char latitude[10];
char longitude[10];


void setup()
{
  /* Start communication.*/
  Serial.begin(9600);
  OneSheeld.begin();
  //  PushButton.setOnButtonStatusChange(&Emergency);

  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);

  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);

  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
}

void SonarSensor(int trigPin, int echoPin)
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
}

void SensorHandler() {

  SonarSensor(trigPin1, echoPin1);
  UltraSensor = distance;

  SonarSensor(trigPin2, echoPin2);
  UltraSensorTwo = distance;

  if (UltraSensor <= 60 && UltraSensor >= 45) // Checking the distance, you can change the value
  {
    digitalWrite(motorPin1, HIGH); // motor on
    delay(200);
    digitalWrite(motorPin1, LOW); // motor off
    delay(200);
  }
  else if (UltraSensor < 45 && UltraSensor >= 30)
  {
    digitalWrite(motorPin1, HIGH); // when greater than 100cm
    delay(100);
    digitalWrite(motorPin1, LOW);
    delay(100);
  }
  else if (UltraSensor < 30)
  {
    digitalWrite(motorPin1, HIGH);
    delay(50);
    digitalWrite(motorPin1, LOW);
    delay(50);
  }
  else
  {
    digitalWrite(motorPin1, LOW);
  }

  Terminal.print("SensorOne: ");
  Terminal.println(UltraSensor);

  // For VibMotor Two

  if (UltraSensorTwo <= 60 && UltraSensorTwo >= 45) // Checking the distance, you can change the value
  {
    digitalWrite(motorPin2, HIGH); // motor on
    delay(200);
    digitalWrite(motorPin2, LOW); // motor off
    delay(200);
  }
  else if (UltraSensorTwo < 45 && UltraSensorTwo >= 30)
  {
    digitalWrite(motorPin2, HIGH); // when greater than 100cm
    delay(100);
    digitalWrite(motorPin2, LOW);
    delay(100);
  }
  else if (UltraSensorTwo < 30)
  {
    digitalWrite(motorPin2, HIGH);
    delay(50);
    digitalWrite(motorPin2, LOW);
    delay(50);
  }
  else
  {
    digitalWrite(motorPin2, LOW);
  }

  Terminal.print("SensorTwo: ");
  Terminal.println(UltraSensorTwo);
}

void loop()
{
  //    Terminal.println("Test");
  //    Emergency();
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > interval) 
  {
    previousMillis = currentMillis;
    SensorHandler();
  }
  if (PushButton.isPressed())
  {
    Terminal.println("Button Pressed");
    lat = GPS.getLatitude();
    lon = GPS.getLongitude();
    Message1 = String(lat, 8);
    Message2 = String(lon, 8);

    Message = "I need Help! Google Maps - https://www.google.co.in/maps/place/" + Message1 + "," + Message2;
    Terminal.print("Message Sent");

    //SMS.send("0819033655",Message);
    //delay(3000);
  }
  //delay(1000);
}
  • 1
    study the example sketch called BlinkWithoutDelay .... it will give you information on how to remove the delay() functions from your code – jsotola May 7 '18 at 6:56
  • @jsotola , Thanks sir. I will try to learn it now! – Htoo May 7 '18 at 6:57
  • it seems a bit faster but the vibration rate according to the distance is not seems right. here's my code after I added. can you please check for me mr.@jsotola. I will edit in my question code. – Htoo May 7 '18 at 7:32
  • You should replace ALL delays with non-blocking code like in BlinkWithoutDelay. This will only work good, if you ditch all the delays – chrisl May 7 '18 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.