1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| int Left_motor=8; int Left_motor_pwm=9; int Right_motor_pwm=10; int Right_motor=11; const int Middle = 13; const int Left1 = 4; const int Right1 = 6; const int Left2 = 12; const int Right2 = 2; const int Left3 = 3; const int Right3 = 5; int beep=A3;
int M,L1,R1,L2,R2,L3,R3; int go=0; int t = 10; int t1 = 0; int t2 = 0;
void setup(){ Serial.begin(9600); pinMode(Left_motor,OUTPUT); pinMode(Left_motor_pwm,OUTPUT); pinMode(Right_motor_pwm,OUTPUT); pinMode(Right_motor,OUTPUT); pinMode(Middle, INPUT); pinMode(Left1, INPUT); pinMode(Right1, INPUT); pinMode(Left2, INPUT); pinMode(Right2, INPUT); pinMode(Left3, INPUT); pinMode(Right3, INPUT); pinMode(beep,OUTPUT); }
void a(){ digitalWrite(Left_motor,LOW); digitalWrite(Left_motor_pwm,HIGH); analogWrite(Left_motor_pwm,110); digitalWrite(Right_motor,LOW); digitalWrite(Right_motor_pwm,HIGH); analogWrite(Right_motor_pwm,110); } void left(){ digitalWrite(Left_motor,HIGH); digitalWrite(Left_motor_pwm,HIGH); analogWrite(Left_motor_pwm,5); digitalWrite(Right_motor,LOW); digitalWrite(Right_motor_pwm,HIGH); analogWrite(Right_motor_pwm,130); } void left2(){ digitalWrite(Left_motor,HIGH); digitalWrite(Left_motor_pwm,HIGH); analogWrite(Left_motor_pwm,20); digitalWrite(Right_motor,LOW); digitalWrite(Right_motor_pwm,HIGH); analogWrite(Right_motor_pwm,160); } void turnleft(){ digitalWrite(Left_motor,HIGH); digitalWrite(Left_motor_pwm,HIGH); analogWrite(Left_motor_pwm,150); digitalWrite(Right_motor,LOW); digitalWrite(Right_motor_pwm,HIGH); analogWrite(Right_motor_pwm,120); } void right(){ digitalWrite(Left_motor,LOW); digitalWrite(Left_motor_pwm,HIGH); analogWrite(Left_motor_pwm,130); digitalWrite(Right_motor,HIGH); digitalWrite(Right_motor_pwm,HIGH); analogWrite(Right_motor_pwm,5); } void right2(){ digitalWrite(Left_motor,LOW); digitalWrite(Left_motor_pwm,HIGH); analogWrite(Left_motor_pwm,160); digitalWrite(Right_motor,HIGH); digitalWrite(Right_motor_pwm,HIGH); analogWrite(Right_motor_pwm,20); } void turnright(){ digitalWrite(Left_motor,LOW); digitalWrite(Left_motor_pwm,HIGH); analogWrite(Left_motor_pwm,120); digitalWrite(Right_motor,HIGH); digitalWrite(Right_motor_pwm,HIGH); analogWrite(Right_motor_pwm,150); } void Beep(){ digitalWrite(beep,HIGH); delay(50); digitalWrite(beep,LOW); }
void loop(){
M = digitalRead(Middle); L1 = digitalRead(Left1); R1 = digitalRead(Right1); L2 = digitalRead(Left2); R2 = digitalRead(Right2); L3 = digitalRead(Left3); R3 = digitalRead(Right3);
if(M==HIGH||L1==HIGH||R1==HIGH){t=3;} if(L2==HIGH||R2==HIGH){t=3;} if(L3==HIGH){t1=0;} if(R3==HIGH){t2=0;}
if (M == HIGH || (L1 == HIGH && R1 == HIGH)){go=1;}else if (L1 == HIGH){go=2;}else if (R1 == HIGH){go=3;}else if (L2 == HIGH){go=4;}else if (R2 == HIGH){go=5;} if(t<0){ if(t1>t2&&t2<=100){ go=7; } if(t1<t2&&t1<=100){ go=6; } }
if(go==1){ a();} else if (go==2){ left();} else if (go==3){ right();} else if (go==4){ left2();} else if (go==5){ right2();} else if (go==6){ turnleft();} else if (go==7){ turnright();}
t1=t1+1; t2=t2+1; t=t-1;
Serial.print(" t "); Serial.print(t); Serial.print(" t1 "); Serial.print(t1); Serial.print(" t2 "); Serial.print(t2); Serial.print(" go "); Serial.print(go); Serial.print("\n"); }
|