//import your motor shield library #include // set up motors. AF_DCMotor motor1(1, MOTOR12_1KHZ); AF_DCMotor motor2(2, MOTOR12_1KHZ); // define the pins of your sensor #define right_sensor_pin A0 #define left_sensor_pin A1 // sets speed of DC motors, between 0-255 #define MOTOR_SPEED 150 void setup() { // begin serial communication Serial.begin(9600); pinMode(left_sensor_pin, INPUT); // set the left_sensor pin to input pinMode(right_sensor_pin, INPUT); // set the right_sensor pin to input //set the speed of the motors motor1.setSpeed(MOTOR_SPEED); motor2.setSpeed (MOTOR_SPEED); } void loop() { int left_sensor = analogRead(left_sensor_pin); int right_sensor = analogRead(right_sensor_pin); Serial.print ("left : " ); Serial.println (left_sensor ); Serial.print ("right : " ); Serial.println (right_sensor ); if ( left_sensor 36) { // Black line delected by both sensors //turn right Serial.println ("Black line delected by both sensors" ); moveStop(); } } //Move forward Function void moveForward() { motor1.run(FORWARD); motor1.setSpeed(MOTOR_SPEED); motor2.run(FORWARD); motor2.setSpeed(MOTOR_SPEED); } //release movment Function void moveStop() { motor1.run(RELEASE); motor2.run(RELEASE); } //Turn Right Function void turnRight() { motor1.run(FORWARD); motor1.setSpeed(MOTOR_SPEED+50); motor2.run(BACKWARD); motor2.setSpeed(MOTOR_SPEED+50); } //Turn Left Function void turnLeft() { motor1.run(BACKWARD); motor1.setSpeed(MOTOR_SPEED+50); motor2.run(FORWARD); motor2.setSpeed(MOTOR_SPEED+50); }
الافصل هوه اثنان وفي بعض الاحيان نضع ٣ لاجل دقه سير السياره على المسار وكذلك عند خروجها عن المسار فعند وضع اكثر من سينسور تكون القرائه افضل اذا النتيجه افضل وذلك ايضا يؤثر على حركه السياره فعند وضع اكثر من سينسور تكون حركتها سلسه اكثر