Wednesday, November 4, 2015

Robot controlled by Sonar

How to build a Robot on an Arduino that can be controlled by an ultrasonic sensor:
by J.B. Wylzan




ARDUINO SKETCH:

/* ===============================================================
      Project Homotronics: Robot controlled by Sonar
      Author: J. B. Wylzan with some help from the Arduino community
      Website: http://www.ihackrobot.blogspot.com
      Abstract: Control a robot via an ultrasonic sensor.
================================================================== */

#define trigPin 12
#define echoPin 11
#define IN1 10
#define IN2 9
const int redLed = 6;     
const int greenLed = 7;   
const int yellowLed = 8;  

// ENABLE PWM 3,5,6,9,10,11

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);    
  pinMode(echoPin, INPUT);
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
    pinMode(redLed, OUTPUT);
 pinMode(greenLed, OUTPUT);
 pinMode(yellowLed, OUTPUT);
}

void loop() {
  long time, range;
  digitalWrite(trigPin, HIGH); 
  delayMicroseconds(2);   //2
  digitalWrite(trigPin, LOW);

  time = pulseIn(echoPin, HIGH);
  range = (time/2)/29.1;    //58.2

  if (range < 20)
  {
    analogWrite(IN1, 0);        
    analogWrite(IN2, 255);
   
    Serial.println("Moving forward");
    Serial.println("Blinking Green");
     Serial.print(range);
    Serial.println(" cm");

 //   delay(1000);
  //  digitalWrite(redLed, HIGH);
//delay(1000);
//digitalWrite(redLed, LOW);
  }
  else        
    {
        analogWrite(IN1, 255);   
        analogWrite(IN2, 0);      
       
        Serial.println("stopping");
         Serial.println("Blinking Red");
          Serial.print(range);
    Serial.println(" cm");

     // digitalWrite(yellowLed, HIGH);
//delay(1000);
//digitalWrite(yellowLed, LOW);                                         
    } 
}





Disclaimer:  We shall not be liable for any loss or damage of whatever nature - direct, indirect, consequential, or otherwise - which may arise as a result of your use of any information on this website. However, if you are interested in using any of the projects for personal or educational purposes, please inform the author by email. 

Public Domain Notice: Copyright (c) 2000. All rights reserved. This article is part of a book entitled iHackRobot. Copies are welcome to be shared or distributed publicly as long proper citations are observed. Please cite as follows: Biotronics: The Silver Species, Joey Lawsin, 1988, USA.

================================================================== 
The Homotronics® and Homodruinos® logos are registered trademarks.
Copyright Biotronics© Inc. iHackRobot®. All Rights Reserved.
Patent Pending. 2000 © ®

L.A.W.S.I.N. Educational Production
 ================================================================== 




No comments:

Post a Comment