- #define trig 9
- #define echo 10
- #define motor1 5
- #define motor2 6
- int speed = 0;
- int ultra(int trg, int ech){
- digitalWrite(trg, LOW);
- delayMicroseconds(2);
- digitalWrite(trg, HIGH);
- delayMicroseconds(10);
- digitalWrite(trg, LOW);
- int duration = pulseIn(ech,HIGH);
- int distance = duration * 0.034/2;
- return distance;
- }
- void setup() {
- pinMode(trig,OUTPUT);
- pinMode(echo,INPUT);
- pinMode(motor1, OUTPUT);
- pinMode(motor2, OUTPUT);
- Serial.begin(9600);
- }
- void loop() {
- int a = ultra(trig,echo);
- if (a > 40){
- for (int speed = 0; speed <= 255; speed++) {
- analogWrite(motor1, speed);
- digitalWrite(motor2, LOW);
- delay(10);
- int a = ultra(trig,echo);
- if(a < 30){
- break;
- }
- Serial.println(a);
- Serial.println("kumalala");
- while(speed == 255){
- digitalWrite(motor1, HIGH);
- digitalWrite(motor2, LOW);
- Serial.println(a);
- Serial.println("kumalala");
- int a = ultra(trig,echo);
- if(a < 30){
- break;
- }
- }}
- }
- if (a < 30){
- digitalWrite(motor1, 0);
- analogWrite(motor2, 0);
- }
- }
Recent Pastes