Advertisement
celem

servoranger.ino

Aug 29th, 2012
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.79 KB | None | 0 0
  1. #include "ktms1201.h"
  2. #include "common.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <Servo.h>
  6.  
  7. /*
  8.  Demonstration of HC-SR04 distance sensor. (<$4)
  9.  Displays distance to:
  10.  (1) the serial port
  11.  (2) KTM-S1201 LCD display (<$3)
  12.  (3) Rotates a RC Servo (<$7)
  13.  
  14.  HC-SR04 portions based on a sketch by terry@yourduino.com
  15.  KTM-S1201 LCD portions based on AVR code by Jack Botner
  16.  Remainder of code by Edward Comer
  17.  
  18.  This code serves no useful purpose other then to demo the
  19.  HC-SR04, KTM-S1201 and servo.
  20.  */
  21.  
  22. // HR-SR04 definitions
  23. #define TRIG_PIN  12
  24. #define ECHO_PIN  13
  25. #define CM 1
  26. #define INC 0
  27.  
  28. // KTM-S1201 LCD pin defines are in common.h
  29.  
  30. // Global variables
  31. long  duration, distance_cm, distance_inc;
  32. char buffer [50];
  33. Servo myservo;  // create servo object to control a servo
  34. int pos = 0;    // variable to store the servo position
  35.  
  36. void setup()
  37. {
  38.   // Establish pin directions for LCD connection
  39.   pinMode(LCD_SCK_PIN,OUTPUT);
  40.   pinMode(LCD_SI_PIN,OUTPUT);
  41.   pinMode(LCD_CD_PIN,OUTPUT);
  42.   pinMode(LCD_RST_PIN,OUTPUT);
  43.   pinMode(LCD_BUSY_PIN,INPUT);
  44.   pinMode(LCD_CS_PIN,OUTPUT);
  45.   pinMode(13,OUTPUT);
  46.   //Do one time to Initalize KTM-S1201
  47.   //digitalWrite(LCD_VCC, HIGH); // sets the Power On
  48.   //digitalWrite(LCD_VSS, LOW);
  49.  
  50.   digitalWrite(LCD_CD_PIN, HIGH); //Put in copmmand mode
  51.   digitalWrite(LCD_CS_PIN, HIGH); //deselect KTM-S1201
  52.   digitalWrite(LCD_SCK_PIN, HIGH);
  53.   digitalWrite(LCD_SI_PIN,  HIGH);
  54.   digitalWrite(LCD_RST_PIN, LOW); // reset lcd
  55.  
  56.   delay(10);
  57.   digitalWrite(LCD_RST_PIN, HIGH);
  58.   delay(10);
  59.   LCD_init();
  60.  
  61.   // Initialize HC-SR04 LCD display
  62.   pinMode(TRIG_PIN,OUTPUT);
  63.   pinMode(ECHO_PIN,INPUT);
  64.  
  65.   //Initialize serial port
  66.   Serial.begin(9600);
  67.   Serial.println("HC-SR04 DEMO");
  68.  
  69.   // Initialize servo
  70.   myservo.attach(9);   // attaches the servo on pin 9 to the servo object
  71. }
  72.  
  73.  
  74. void loop()
  75. {
  76.   long range;
  77.   range = Ranging(INC);
  78.   ltoa(range,buffer,10);
  79.   strcat(buffer," IN");
  80.   LCD_puts2(buffer, 0 );
  81.   Serial.print(range);
  82.   Serial.println(" inches");
  83.   pos = range;
  84.   pos *= 2;               // Double servo travel for more drama
  85.   pos &= 0x7f;          // but limit rotation to 127 degrees   
  86.   myservo.write(pos);     // tell servo to go to position in variable 'pos'
  87.     delay(15);            // waits 15ms for the servo to reach the position
  88.   delay(1000);            // slow to human speeds
  89. }
  90.  
  91. long Timing()
  92. {
  93.   digitalWrite(TRIG_PIN, LOW);
  94.   delayMicroseconds(2);
  95.   digitalWrite(TRIG_PIN, HIGH);
  96.   delayMicroseconds(10);
  97.   digitalWrite(TRIG_PIN, LOW);
  98.   duration = pulseIn(ECHO_PIN,HIGH);
  99.   return duration;
  100. }
  101.  
  102. long Ranging(int sys)
  103. {
  104.   Timing();
  105.   distance_cm = duration /29 / 2 ;
  106.   distance_inc = duration / 74 / 2;
  107.   if (sys)
  108.     return distance_cm;
  109.   else
  110.     return distance_inc;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement