TEXT 234
Ultra3halate_no soft start Abbasi Guest on 11th November 2023 08:28:29 AM
  1. #define trig 9
  2. #define echo 10
  3. #define motor1 5
  4. #define motor2 6
  5.  
  6. int speed = 0;
  7. int ultra(int trg, int ech){
  8.   while(1){
  9.   digitalWrite(trg, LOW);
  10.   delayMicroseconds(2);
  11.   digitalWrite(trg, HIGH);
  12.   delayMicroseconds(10);
  13.   digitalWrite(trg, LOW);+
  14.   int duration = pulseIn(ech,HIGH,5000);
  15.   int distance = duration * 0.034/2;
  16.   if(distance != 0){
  17.   return distance;}
  18.   }
  19. }
  20.  
  21. void setup() {
  22.   pinMode(trig,OUTPUT);
  23.   pinMode(echo,INPUT);
  24.   pinMode(motor1, OUTPUT);
  25.   pinMode(motor2, OUTPUT);
  26.   Serial.begin(9600);
  27.  
  28. }
  29.  
  30. void loop() {
  31.  
  32.   int a = ultra(trig,echo);
  33.   Serial.println(a);
  34.   if (a > 40){
  35.   analogWrite(motor1, 255);
  36.   digitalWrite(motor2, LOW);
  37.   int a = ultra(trig,echo);
  38.   }
  39. else if (a > 35 and a < 40){
  40.   analogWrite(motor1, 125);
  41.   analogWrite(motor2, 0);
  42.   int a = ultra(trig,echo);
  43. }
  44.  
  45. else if (a > 30 and a < 35){
  46.   analogWrite(motor1, 63);
  47.   analogWrite(motor2, 0);
  48.   int a = ultra(trig,echo);
  49. }
  50.  
  51. else if (a < 30 and a > 20){
  52.   digitalWrite(motor1, 0);
  53.   analogWrite(motor2, 0);
  54.   int a = ultra(trig,echo);
  55. }
  56.  
  57.  
  58.  
  59. else if (a < 20){
  60.   digitalWrite(motor1, 0);
  61.   digitalWrite(motor2, 1);
  62. }
  63.  
  64. }

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.