Telekommunikatsoya texnologiyalari fakulteti 416 20 – guruh talabasi
Loyihalaning С++ dasturidagi kodi
Download 0.55 Mb.
|
10-12 17-24
Loyihalaning С++ dasturidagi kodi:#define ENA 5 #define ENB 6 #define IN1 7 #define IN2 8 #define IN3 9 #define IN4 11 #define sright 10 #define sleft 2 #define smiddle 4 //set car speed #define CAR_SPEED 100 typedef unsigned char u8; //Change the name of unsigned char to u8 int rightsensor, leftsensor, middlesensor; void forward(u8 car_speed){ analogWrite(ENA, car_speed); analogWrite(ENB, car_speed); digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH);} void back(u8 car_speed){ analogWrite(ENA, car_speed); analogWrite(ENB, car_speed); digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);} void left(u8 car_speed){ analogWrite(ENA, 250); analogWrite(ENB, 250); digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH);} void right(u8 car_speed){ analogWrite(ENA, 250); analogWrite(ENB, 250); digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);} void stop(){ digitalWrite(ENA, LOW); digitalWrite(ENB, LOW);} int x = 0; long readUltrasonicDistance(int pin){ pinMode(pin, OUTPUT); // Clear the trigger digitalWrite(pin, LOW); delayMicroseconds(2); // Sets the pin on HIGH state for 10 micro seconds digitalWrite(pin, HIGH); delayMicroseconds(10); digitalWrite(pin, LOW); pinMode(pin, INPUT); // Reads the pin, and returns the sound wave travel time in microseconds return pulseIn(pin, HIGH);} void setup() { pinMode(IN1,OUTPUT); pinMode(IN2,OUTPUT); pinMode(IN3,OUTPUT); pinMode(IN4,OUTPUT); pinMode(ENA,OUTPUT); pinMode(ENB,OUTPUT); pinMode(sright,INPUT); pinMode(sleft,INPUT); pinMode(smiddle,INPUT); Serial.begin(9600); pinMode(15, INPUT); pinMode(13, OUTPUT); pinMode(A0, INPUT); digitalWrite(13, LOW);} void loop() { int joystick=analogRead(A0); rightsensor=digitalRead(sright); leftsensor=digitalRead(sleft); middlesensor=digitalRead(smiddle); int distance=0.01723 * readUltrasonicDistance(15); Serial.println(joystick); Serial.print(" "); Serial.println(rightsensor); Serial.print(" "); Serial.print(middlesensor); Serial.print(" "); Serial.print(leftsensor); Serial.print(" "); Serial.print("Distance :"+distance); //detect white l if (distance>10) { digitalWrite(13, LOW); if ((rightsensor==0)(joystick<349)) {right(CAR_SPEED);} //detection white on right else if ((leftsensor==0)(joystick>699)) {left(CAR_SPEED);} //detection white on left else if ((middlesensor==0)||((joystick>349) && (joystick<698))) {forward(CAR_SPEED); } //detection white on middle } else{ digitalWrite(ENA, LOW); digitalWrite(ENB, LOW); digitalWrite(13, HIGH);} } Download 0.55 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling