CPP 251
Arduino code By arvand on 12th September 2023 08:07:22 AM
  1. #define encoderr 2
  2. #define encoderl 3
  3.  
  4. #define echo1 64
  5. #define echo2 66
  6. #define echo3 68
  7. #define trig1 65
  8. #define trig2 67
  9. #define trig3 69
  10. #define echo4 41
  11. #define echo5 31
  12. #define trig4 43
  13. #define trig5 33
  14.  
  15. #define motorr_p 7     // motor - and + conectors
  16. #define motorr_m 5
  17. #define motorl_p 8
  18. #define motorl_m 4
  19.  
  20.  long int dispulser = 0;  // enchoder pulses till now
  21.  long int dispulsel = 0;
  22.  
  23.  
  24. long duration, distance  ;
  25. int rightsensor, midsensor, leftsensor ,right2sensor, mid2sensor, left2sensor;
  26.  
  27.  void ultra_sonics(int trigPin, int echoPin)
  28. {
  29.   digitalWrite(trigPin, 0);
  30.   delayMicroseconds(2);
  31.  
  32.   digitalWrite(trigPin, 1);
  33.   delayMicroseconds(10);
  34.   digitalWrite(trigPin, 0);
  35.   duration = pulseIn(echoPin, 1 , 26000);
  36.   distance = distance;
  37.   distance = duration * 0.034/2;
  38. }
  39.  
  40.  
  41. void enr()
  42. {
  43.     dispulser ++;
  44. }
  45.  
  46. void enl()
  47. {
  48.     dispulsel ++ ;
  49. }
  50.  
  51. void check_ul_sen()
  52. {
  53.   ultra_sonics(trig1, echo1);
  54.   leftsensor = distance;
  55.  
  56.   ultra_sonics(trig2, echo2);
  57.   rightsensor = distance;
  58.  
  59.   ultra_sonics(trig3, echo3);
  60.   midsensor = distance;
  61.  
  62.   ultra_sonics(trig4, echo4);
  63.   right2sensor = distance;
  64.  
  65.   ultra_sonics(trig5, echo5);
  66.   left2sensor = distance;
  67.  
  68.  
  69.  
  70.  
  71.  
  72. }
  73.  
  74. void setup() {
  75.   // put your setup code here, to run once:
  76.   pinMode(motorl_p , OUTPUT);
  77.   pinMode(motorl_m , OUTPUT);
  78.   pinMode(motorr_p , OUTPUT);
  79.   pinMode(motorr_m , OUTPUT);
  80.  
  81.   pinMode(trig1, OUTPUT);
  82.   pinMode(trig2, OUTPUT);
  83.   pinMode(trig3, OUTPUT);
  84.   pinMode(trig4, OUTPUT);
  85.   pinMode(trig5, OUTPUT);
  86.   pinMode(echo1, INPUT);
  87.   pinMode(echo2, INPUT);
  88.   pinMode(echo3, INPUT);
  89.   pinMode(echo4, INPUT);
  90.   pinMode(echo5, INPUT);
  91.  
  92.   Serial.begin(115200);
  93.    
  94.   attachInterrupt(digitalPinToInterrupt(encoderr), enr, RISING);
  95.   attachInterrupt(digitalPinToInterrupt(encoderl), enl, RISING);
  96.  
  97. }
  98.  
  99. void loop() {
  100.   // put your main code here, to run repeatedly:
  101.   check_ul_sen();
  102.   //analogWrite(motorl_p , 255);
  103.   //analogWrite(motorl_m , 0);
  104.  
  105.   //analogWrite(motorr_p , 255);
  106.  // analogWrite(motorr_m , 0);
  107.  
  108.   Serial.print('L');
  109.   Serial.print(leftsensor);
  110.   Serial.print(" R ");
  111.   Serial.print(rightsensor);
  112.   Serial.print(" M ");
  113.   Serial.print(midsensor);
  114.   Serial.print(" L2 ");
  115.   Serial.print(left2sensor);
  116.   Serial.print(" R2 ");
  117.   Serial.println(right2sensor);
  118.   Serial.print(" enl ");
  119.   Serial.print(dispulsel);
  120.   Serial.print(" enr ");
  121.   Serial.println(dispulser);
  122.  
  123.   delay(300);
  124.  
  125.  
  126.  
  127.  
  128. }

Hightechrobo bin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.