bangnaga

Radar360 Stepper Ultrasonic

Oct 2nd, 2013
70
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Stepper.h>
  2. #include <NewPing.h>
  3.  
  4. #define STEPS 100 // number of steps per revolution
  5.  
  6.  
  7.  
  8. const int pingPin = 13;
  9. int inPin = 12;
  10. int safeZone = 5;
  11.  
  12.  
  13. Stepper motor(STEPS, 8, 9, 10, 11);
  14. //NewPing sodar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
  15. int ping_delay = 50;
  16. long last_ping_time = 0;
  17. long duration; // duration of return pulse
  18.  
  19. boolean new_reading = false;
  20. const int max_write_delay = 50;
  21. long last_write_time = 0;
  22.  
  23.  
  24.  
  25. boolean stopMotor = false;
  26. int rpm = 45; // default RPM
  27.  
  28. void setup() {
  29.   motor.setSpeed(rpm);
  30.   // initialize serial communication
  31.   Serial.begin(9600);
  32. }
  33.  
  34. void loop()
  35. {
  36.   long duration, cm;
  37.   pinMode(pingPin, OUTPUT);
  38.   digitalWrite(pingPin, LOW);
  39.   delayMicroseconds(2);
  40.   digitalWrite(pingPin, HIGH);
  41.   delayMicroseconds(5);
  42.   digitalWrite(pingPin, LOW);
  43.  
  44.   pinMode(inPin, INPUT);
  45.   duration = pulseIn(inPin, HIGH);
  46.   cm = microsecondsToCentimeters(duration);
  47.  
  48.   if(millis() - last_ping_time >= ping_delay) {
  49.     last_ping_time = millis();
  50.  
  51.     // trigger the pulse and interrupt on response
  52.   //  sodar.ping_timer(checkPing);
  53.   }
  54.  
  55.   if(new_reading) {
  56.     new_reading = false;
  57.     last_write_time = millis();
  58.    
  59.     // convert the time into a distance
  60.    // cm = microsecondsToCentimeters(duration);
  61.    // cm = random(1, 300);
  62.    
  63.     writeData(cm, motor.getAngle());
  64.   } else if(millis() - last_write_time >= max_write_delay) {
  65.     last_write_time = millis();
  66.    
  67.    // cm = 0;
  68.     writeData(cm, motor.getAngle());
  69.   }
  70.  
  71.   if(!stopMotor) {
  72.     motor.step(1);
  73.   }
  74. }
  75.  
  76. void writeData(float cm, float angle)
  77. {
  78.  
  79.  Serial.print(cm);
  80. Serial.print(" cm @ ");
  81.  
  82.  
  83.   Serial.print(angle);
  84.   Serial.println(" degrees");
  85. }
  86.  
  87.  
  88.  
  89. long microsecondsToCentimeters(long microseconds)
  90. {
  91.   return microseconds / 29 / 2;
  92. }
RAW Paste Data