TEXT 98
Motor and ultra ABBASI Guest on 11th November 2023 07:56:02 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.   digitalWrite(trg, LOW);
  9.   delayMicroseconds(2);
  10.   digitalWrite(trg, HIGH);
  11.   delayMicroseconds(10);
  12.   digitalWrite(trg, LOW);
  13. int duration = pulseIn(ech,HIGH);
  14.   int distance = duration * 0.034/2;
  15.   return distance;
  16. }
  17.  
  18. void setup() {
  19.   pinMode(trig,OUTPUT);
  20.   pinMode(echo,INPUT);
  21.   pinMode(motor1, OUTPUT);
  22.   pinMode(motor2, OUTPUT);
  23.   Serial.begin(9600);
  24.  
  25. }
  26.  
  27. void loop() {
  28.   int a = ultra(trig,echo);
  29.   if (a > 40){
  30.   for (int speed = 0; speed <= 255; speed++) {
  31.   analogWrite(motor1, speed);
  32.   digitalWrite(motor2, LOW);
  33.   delay(10);
  34.   int a = ultra(trig,echo);
  35.   if(a < 30){
  36.   break;
  37.   }
  38.   Serial.println(a);
  39.   Serial.println("kumalala");
  40.   while(speed == 255){
  41.   digitalWrite(motor1, HIGH);
  42.   digitalWrite(motor2, LOW);
  43.   Serial.println(a);
  44.   Serial.println("kumalala");
  45.   int a = ultra(trig,echo);
  46.   if(a < 30){
  47.     break;
  48.   }
  49.   }}
  50. }
  51.  
  52. if (a < 30){
  53.   digitalWrite(motor1, 0);
  54.   analogWrite(motor2, 0);
  55. }
  56.  
  57. }

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.