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
| #include <Servo.h>
int sensor_pin[4] = {A0,A4,A2,A3}; int sensor_value[4] = {0,0,0,0}; const int LEFT = 0;//normal left const int RIGHT = 1;//normal right const int LEFT_RightAngle = 2;//right angle left const int RIGHT_RightAngle = 3;//right angle right const int STRAIGHT = 4; const int BlackLimit_ML = 680; const int BlackLimit_MR = 550; const int BlackLimit_L = 500; const int BlackLimit_R = 500; const int HIGH_RIGHT = 234; Servo myservo; boolean isobstacle = 0;
void setup(){ pinMode(5,OUTPUT);//motor out pinMode(6,OUTPUT); pinMode(9,OUTPUT);//motor out pinMode(10,OUTPUT); pinMode(8,INPUT);//touch sensor myservo.attach(11); Serial.begin(9600); }
//judge whether an obstacle in front of us,return true when it is boolean is_obstacle(){ isobstacle = digitalRead(8); return isobstacle; }
//read sensor value void read_value(){ for(int i = 0;i < 4;i++){ sensor_value[i] = analogRead(sensor_pin[i]); } }
//turn to a direction void turn(const int direction_){ switch(direction_){ case LEFT: analogWrite(5,LOW); digitalWrite(6,HIGH_RIGHT);//right wheel digitalWrite(9,LOW); digitalWrite(10,LOW);//left wheel while(sensor_value[2] > BlackLimit_MR){//sensor_value[1] > BlackLimit_M read_value(); delay(5); } break; case RIGHT: digitalWrite(5,LOW); analogWrite(6,LOW);//right wheel digitalWrite(9,LOW); digitalWrite(10,HIGH);//left wheel while(sensor_value[1] > BlackLimit_ML){//sensor_value[1] > BlackLimit_M read_value(); delay(5); } break; case LEFT_RightAngle: digitalWrite(5,LOW); analogWrite(6,HIGH_RIGHT);//right wheel digitalWrite(9,HIGH); digitalWrite(10,LOW);//left wheel while(sensor_value[1] > BlackLimit_ML){//sensor_value[1] > BlackLimit_M read_value(); if(sensor_value[3] < BlackLimit_R) break; delay(5); } break; case RIGHT_RightAngle: digitalWrite(5,HIGH_RIGHT); analogWrite(6,LOW);//right wheel digitalWrite(9,LOW); digitalWrite(10,HIGH);//left wheel while(sensor_value[2] > BlackLimit_MR){//sensor_value[1] > BlackLimit_M read_value(); if(sensor_value[0] < BlackLimit_L) break; delay(5); } break; case STRAIGHT: digitalWrite(5,LOW); analogWrite(6,HIGH_RIGHT);//right wheel digitalWrite(9,LOW); digitalWrite(10,HIGH);//left wheel break; default: digitalWrite(5,LOW); analogWrite(6,HIGH_RIGHT);//right wheel digitalWrite(9,LOW); digitalWrite(10,HIGH);//left wheel break; } }
//judge what direction to turn with sensor value void judge_turn(){ read_value(); if(sensor_value[0] < BlackLimit_L && sensor_value[3] > BlackLimit_R){//turn left right tangle Serial.println(LEFT_RightAngle); turn(LEFT_RightAngle); } else if(sensor_value[0] > BlackLimit_L && sensor_value[3] < BlackLimit_R){//turn right right tangle Serial.println(RIGHT_RightAngle); turn(RIGHT_RightAngle); } else if(sensor_value[1] > BlackLimit_ML && sensor_value[2] < BlackLimit_MR){ Serial.println(RIGHT); turn(RIGHT); } else if(sensor_value[1] < BlackLimit_ML && sensor_value[2] > BlackLimit_MR){ Serial.println(LEFT); turn(LEFT); } else{ Serial.println(STRAIGHT); turn(STRAIGHT); } }
void run(){ judge_turn(); delay(10); }
void loop(){ run(); }
|