Tuesday, November 24, 2015

Ultrasonic Control Experiment





ARDUINO SKETCH:

/* ===============================================================
      Project Homotronics: Robot controlled by Sonar
      Author: J. B. Wylzan with libraries from the Arduino website
      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);                                         
    } 
}

/* ================================================================== */



 ==================================================================
"Humans can't think of something, without associating it with something."
 ~ Joey Lawsin
==================================================================

NOTICE: Articles on this site are composed on random thoughts. The transcript may not be in its final form. It maybe edited, updated or even revised in the future based on the outcomes of  the author's experiments.

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

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





No comments:

Post a Comment