How to make Bluetooth Controlled Container Truck







Click here and download App 


              CODE 

#include <AFMotor.h>


#include <Servo.h> // Servo library




char pn;


int mot_speed = 200;




AF_DCMotor motor1(1);  // Extra motor


AF_DCMotor motor3(3);  // Left motor


AF_DCMotor motor4(4);  // Right motor




Servo myservo;         // Single servo


int currentServoPos = 90;  // Start at 90 degrees




void setup() {


  Serial.begin(9600);




  motor1.setSpeed(mot_speed);


  motor3.setSpeed(mot_speed);


  motor4.setSpeed(mot_speed);




  myservo.attach(10);         // Attach servo to pin 10


  myservo.write(currentServoPos);  // Initial position


}




void loop() {


  motor1.setSpeed(mot_speed);


  motor3.setSpeed(mot_speed);


  motor4.setSpeed(mot_speed);




  if (Serial.available() > 0) {


    pn = Serial.read();


    Serial.print(pn);




    // Motor control


    if (pn == 'F') {


      motor3.run(FORWARD);


      motor4.run(FORWARD);


    } 


    else if (pn == 'B') {


      motor3.run(BACKWARD);


      motor4.run(BACKWARD);


    } 


    else if (pn == 'S') {


      motor3.run(RELEASE);


      motor4.run(RELEASE);


      myservo.write(90);  // Servo return to center


      currentServoPos = 90;


    } 


    else if (pn == 'X') {


      motor1.run(BACKWARD);


      delay(100);


      motor1.run(RELEASE);


    } 


    else if (pn == 'Z') {


      motor1.run(RELEASE);


      motor3.run(RELEASE);


      motor4.run(RELEASE);


    }




    // Servo control


    else if (pn == 'R') {


      myservo.write(60);


      currentServoPos = 60;


    } 


    else if (pn == 'L') {


      myservo.write(120);


      currentServoPos = 120;


   }


  }


}

Post a Comment

Previous Post Next Post

Contact Form