Arduino Kod:
int ENABLE_A = 11;
int pin_a1 = 9;
int pin_a2 = 8;
int ENABLE_B = 5;
int pin_b1 = 6;
int pin_b2 = 7;
int Sol = A0;
int Orta = A1;
int Sag = A2;
int SolDeger;
int OrtaDeger;
int SagDeger;
int eDeger=750;
int SonDonus=0;
void setup() {
//Serial.begin(9600);
//pin mode for the DC motors
pinMode (ENABLE_A, OUTPUT);
pinMode (pin_a1, OUTPUT);
pinMode (pin_a2, OUTPUT);
pinMode (ENABLE_B, OUTPUT);
pinMode (pin_b1, OUTPUT);
pinMode (pin_b2, OUTPUT);
}
void loop() {
SolDeger=analogRead(Sol);
OrtaDeger=analogRead(Orta);
SagDeger=analogRead(Sag);
//Serial.print("Sol: ");
//Serial.print(SolDeger); //Analog değeri yazdırıyoruz.
//Serial.print(" ");
//Serial.print("Orta: ");
//Serial.print(OrtaDeger); //Analog değeri yazdırıyoruz.
//Serial.print(" ");
//Serial.print("Sag: ");
//Serial.println(SagDeger); //Analog değeri yazdırıyoruz.
if (OrtaDeger>eDeger) {
goForward();
}
else if (SolDeger<eDeger && OrtaDeger<eDeger && SagDeger>eDeger) {
while(analogRead(Orta)<eDeger) {
goRight();
}
SonDonus=1;
}
else if (SolDeger>eDeger && OrtaDeger<eDeger && SagDeger<eDeger) {
while(analogRead(Orta)<eDeger) {
goLeft();
}
SonDonus=2;
}
else if (SolDeger<eDeger && OrtaDeger<eDeger && SagDeger<eDeger) {
if (SonDonus==1) {
goRight();
SonDonus=1;
}
else if (SonDonus==2) {
goLeft();
SonDonus=2;
}
}
delay(11);
}
void goForward(){
analogWrite (ENABLE_A, 180);
digitalWrite (pin_a1, HIGH);
digitalWrite (pin_a2, LOW);
analogWrite (ENABLE_B, 180);
digitalWrite (pin_b1, HIGH);
digitalWrite (pin_b2, LOW);
}
void goRight(){
analogWrite (ENABLE_A, 130);
digitalWrite (pin_a1, HIGH);
digitalWrite (pin_a2, LOW);
analogWrite (ENABLE_B, 130);
digitalWrite (pin_b1, LOW);
digitalWrite (pin_b2, HIGH);
}
void goLeft(){
analogWrite (ENABLE_A, 130);
digitalWrite (pin_a1, LOW);
digitalWrite (pin_a2, HIGH);
analogWrite (ENABLE_B, 130);
digitalWrite (pin_b1, HIGH);
digitalWrite (pin_b2, LOW);
}
Hiç yorum yok:
Yorum Gönder