Go Down

Topic: US-100 Ultrasonic Sensor Problem (Read 67 times) previous topic - next topic

dotted-eighths

Hi,

For our voice-controlled electric powered wheelchair project, we are using US-100 sensors for object detection. If an object is detected within 1 foot, our microcontroller will set the relay modules (for the motors) to LOW. We will be using six sensors for that. The problem is our fourth sensor would automatically output "16 cm" though no object is that near the sensor. The remaining sensors seem to be working fine, except for that one. When I tried to place my hand near it, it would output a different value, like 9 cm, etc. but then when I remove my hand from the sensor's range, it will again continually send a "16 cm" output.

What might be the problem? Your help will be much appreciated.

Code: [Select]

#include <SoftwareSerial.h>
#include "VoiceRecognitionV3.h"

VR myVR(10,3);

uint8_t records[7]; // save record
uint8_t buf[64];

int leftMotorRelay1 = 30;
int leftMotorRelay2 = 31;
int rightMotorRelay1 = 32;
int rightMotorRelay2 = 33;

#define turnLeft    (0)
#define turnRight   (1)
#define goForward   (2)
#define stopMotor   (3)
#define reverse   (4)

#define firstEchoPin 14
#define firstTrigPin 15
#define secondEchoPin 16
#define secondTrigPin 17
#define thirdEchoPin 18
#define thirdTrigPin 19
#define fourthEchoPin 20
#define fourthTrigPin 21
#define fifthEchoPin 22
#define fifthTrigPin 23
#define sixthEchoPin 24
#define sixthTrigPin 25

int firstSensorDuration, firstSensorDistance, secondSensorDuration, secondSensorDistance, thirdSensorDuration, thirdSensorDistance;
int fourthSensorDuration, fourthSensorDistance, fifthSensorDuration, fifthSensorDistance, sixthSensorDuration, sixthSensorDistance;

void printSignature(uint8_t *buf, int len)
{
  int i;
  for(i=0; i<len; i++){
    if(buf[i]>0x19 && buf[i]<0x7F){
      Serial.write(buf[i]);
    }
    else{
      Serial.print("[");
      Serial.print(buf[i], HEX);
      Serial.print("]");
    }
  }
}

void printVR(uint8_t *buf)
{
  Serial.println("VR Index\tGroup\tRecordNum\tSignature");

  Serial.print(buf[2], DEC);
  Serial.print("\t\t");

  if(buf[0] == 0xFF){
    Serial.print("NONE");
  }
  else if(buf[0]&0x80){
    Serial.print("UG ");
    Serial.print(buf[0]&(~0x80), DEC);
  }
  else{
    Serial.print("SG ");
    Serial.print(buf[0], DEC);
  }
  Serial.print("\t");

  Serial.print(buf[1], DEC);
  Serial.print("\t\t");
  if(buf[3]>0){
    printSignature(buf+4, buf[3]);
  }
  else{
    Serial.print("NONE");
  }
  Serial.println("\r\n");
}

void setup()
{
  myVR.begin(9600);
 
  Serial.begin(115200);
  Serial.println("Elechouse Voice Recognition V3 Module\r\nControl LED sample");
 
  pinMode(leftMotorRelay1, OUTPUT);
  pinMode(leftMotorRelay2, OUTPUT);
  pinMode(rightMotorRelay1, OUTPUT);
  pinMode(rightMotorRelay2, OUTPUT);
 
  if(myVR.clear() == 0){
    Serial.println("Recognizer cleared.");
  }
  else{
    Serial.println("Cannot find VoiceRecognitionModule.");
    Serial.println("Please check connection and restart Arduino.");
    while(1);
  }
 
  if(myVR.load((uint8_t)turnLeft) >= 0){
    Serial.println("LEFT loaded");
  }
 
  if(myVR.load((uint8_t)turnRight) >= 0){
    Serial.println("RIGHT loaded");
  }
 
  if(myVR.load((uint8_t)goForward) >= 0){
    Serial.println("FORWARD loaded");
  }
 
  if(myVR.load((uint8_t)stopMotor) >= 0){
    Serial.println("STOP loaded");
  }
 
  if(myVR.load((uint8_t)reverse) >= 0){
    Serial.println("REVERSE loaded");
  }
 
  pinMode(firstEchoPin, INPUT);
  pinMode(secondEchoPin, INPUT);
  pinMode(thirdEchoPin, INPUT);
  pinMode(fourthEchoPin, INPUT);
  pinMode(fifthEchoPin, INPUT);
  pinMode(sixthEchoPin, INPUT);
  pinMode(firstTrigPin, OUTPUT);
  pinMode(secondTrigPin, OUTPUT);
  pinMode(thirdTrigPin, OUTPUT);
  pinMode(fourthTrigPin, OUTPUT);
  pinMode(fifthTrigPin, OUTPUT);
  pinMode(sixthTrigPin, OUTPUT);
}

void loop()
{
  digitalWrite(firstTrigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(firstTrigPin, LOW);

  firstSensorDuration = pulseIn(firstEchoPin, HIGH);
  firstSensorDistance = (firstSensorDuration/2) / 29.1;
  Serial.print(firstSensorDistance);
  Serial.println(" cm -- Sensor 1");
  delay(500);

  digitalWrite(secondTrigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(secondTrigPin, LOW);

  secondSensorDuration = pulseIn(secondEchoPin, HIGH);
  secondSensorDistance = (secondSensorDuration/2) / 29.1;
  Serial.print(secondSensorDistance);
  Serial.println(" cm -- Sensor 2");
  delay(500);

  digitalWrite(thirdTrigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(thirdTrigPin, LOW);

  thirdSensorDuration = pulseIn(thirdEchoPin, HIGH);
  thirdSensorDistance = (thirdSensorDuration/2) / 29.1;
  Serial.print(thirdSensorDistance);
  Serial.println(" cm -- Sensor 3");
  delay(500);

  digitalWrite(fourthTrigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(fourthTrigPin, LOW);

  fourthSensorDuration = pulseIn(fourthEchoPin, HIGH);
  fourthSensorDistance = (fourthSensorDuration/2) / 29.1;
  Serial.print(fourthSensorDistance);
  Serial.println(" cm -- Sensor 4");
  delay(500);

  digitalWrite(fifthTrigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(fifthTrigPin, LOW);

  fifthSensorDuration = pulseIn(fifthEchoPin, HIGH);
  fifthSensorDistance = (fifthSensorDuration/2) / 29.1;
  Serial.print(fifthSensorDistance);
  Serial.println(" cm -- Sensor 5");
  delay(500);

  digitalWrite(sixthTrigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(sixthTrigPin, LOW);

  sixthSensorDuration = pulseIn(sixthEchoPin, HIGH);
  sixthSensorDistance = (sixthSensorDuration/2) / 29.1;
  Serial.print(sixthSensorDistance);
  Serial.println(" cm -- Sensor 6");
  delay(500);

  if(firstSensorDistance <= 31 || secondSensorDistance <=31 || thirdSensorDistance <=31 || fourthSensorDistance <=31 || fifthSensorDistance <=31 || sixthSensorDistance <=31){
       digitalWrite(leftMotorRelay1, LOW);
       digitalWrite(leftMotorRelay2, LOW);
       digitalWrite(rightMotorRelay1, LOW);
       digitalWrite(rightMotorRelay2, LOW);
  }
  else{
  int ret;
  ret = myVR.recognize(buf, 50);
  if(ret>0){
    switch(buf[1]){
      case turnLeft:
        digitalWrite(leftMotorRelay1, LOW);
        digitalWrite(leftMotorRelay2, LOW);
        digitalWrite(rightMotorRelay1, LOW);
        digitalWrite(rightMotorRelay2, LOW);
        delay(1000);
        digitalWrite(rightMotorRelay1, HIGH);
        delay(2000);
        digitalWrite(rightMotorRelay1, LOW);
        break;
      case turnRight:
        digitalWrite(leftMotorRelay1, LOW);
        digitalWrite(leftMotorRelay2, LOW);
        digitalWrite(rightMotorRelay1, LOW);
        digitalWrite(rightMotorRelay2, LOW);
        delay(1000);
        digitalWrite(leftMotorRelay1, HIGH);
        delay(2000);
        digitalWrite(leftMotorRelay1, LOW);
        break;
      case goForward:
        digitalWrite(leftMotorRelay1, LOW);
        digitalWrite(leftMotorRelay2, LOW);
        digitalWrite(rightMotorRelay1, LOW);
        digitalWrite(rightMotorRelay2, LOW);
        delay(1000);
        digitalWrite(leftMotorRelay1, HIGH);
        digitalWrite(rightMotorRelay1, HIGH);
        break;
      case stopMotor:
        digitalWrite(leftMotorRelay1, LOW);
        digitalWrite(leftMotorRelay2, LOW);
        digitalWrite(rightMotorRelay1, LOW);
        digitalWrite(rightMotorRelay2, LOW);
        break;
      case reverse:
        digitalWrite(leftMotorRelay1, LOW);
        digitalWrite(leftMotorRelay2, LOW);
        digitalWrite(rightMotorRelay1, LOW);
        digitalWrite(rightMotorRelay2, LOW);
        delay(1000);
        digitalWrite(leftMotorRelay2, HIGH);
        digitalWrite(rightMotorRelay2, HIGH);
        break;
      default:
        Serial.println("Record function undefined");
        break;
    }
    /** voice recognized */
    printVR(buf);
  }
  }
}

Krantz

My guess is that it´s a matter of power to the sensors - can you increase the amps?

jcallen

Can you switch #4s pins with #3 to see if the problem follows #4?
Can you assign #4 to 2 other unused pins?
Do you have a spare, known good sensor that you could swap out with #4?

Go Up