Advertisement
MrLunk

Arduino Sonar sensor + sensor scanner 8 dir. to serial array

Mar 5th, 2017
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.93 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_MotorShield.h>
  3. #include "utility/Adafruit_MS_PWMServoDriver.h"
  4. #include <Servo.h>
  5.  
  6. Adafruit_MotorShield AFMS = Adafruit_MotorShield();
  7. Servo servo1;
  8.  
  9. const int trigPinOne = 3;
  10. const int echoPinOne = 4;
  11. const int trigPinTwo = 5;
  12. const int echoPinTwo = 6;
  13.  
  14. int SensorReading[] = {
  15.   0,0,0,0,0,0,0,0};
  16. long duration;
  17. int distance;
  18. int i;
  19. int ii;
  20.  
  21. //---------------------------------------------------------------------
  22.  
  23. void setup() {
  24.   pinMode(trigPinOne, OUTPUT); // Sets the trigPin as an Output
  25.   pinMode(echoPinOne, INPUT); // Sets the echoPin as an Input
  26.   pinMode(trigPinTwo, OUTPUT); // Sets the trigPin as an Output
  27.   pinMode(echoPinTwo, INPUT); // Sets the echoPin as an Input
  28.  
  29.   Serial.begin(9600);
  30.   servo1.attach(10);
  31. }
  32.  
  33. //---------------------------------------------------------------------
  34.  
  35. void loop() {
  36.  
  37.   for (i = 0; i < 4; i = i + 1) {
  38.     servo1.write((i*45));
  39.     delay(250);
  40.     DoScans();
  41.     //delay(500);
  42.   }
  43.  
  44.   for (i = 0; i < 8; i = i + 1) {
  45.     Serial.print(SensorReading[i]);
  46.     Serial.print(",");
  47.   }
  48.  
  49.   Serial.println("");
  50.  
  51.   i = 0;
  52.  
  53. }
  54.  
  55. //---------------------------------------------------------------------
  56.  
  57. void DoScans(){
  58.  
  59.   digitalWrite(trigPinOne, LOW);
  60.   digitalWrite(trigPinTwo, LOW);
  61.   delayMicroseconds(2);
  62.  
  63.   digitalWrite(trigPinOne, HIGH);
  64.   delayMicroseconds(10);
  65.   digitalWrite(trigPinOne, LOW);
  66.   duration = pulseIn(echoPinOne, HIGH);
  67.   distance= duration*0.034/2;
  68.   if (distance > 200){distance = 200;}
  69.   SensorReading[i] = distance;
  70.   delay(100);
  71.  
  72.   //--------------------------------------------------------------------
  73.  
  74.   digitalWrite(trigPinTwo, HIGH);
  75.   delayMicroseconds(10);
  76.   digitalWrite(trigPinTwo, LOW);
  77.   duration = pulseIn(echoPinTwo, HIGH);
  78.   distance= duration*0.034/2;
  79.   if (distance > 200){distance = 200;}
  80.   ii = i+4;
  81.   SensorReading[ii] = distance;
  82.   delay(100);
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement