วันศุกร์ที่ 28 กันยายน พ.ศ. 2561

Arduino 15 Building an Arduino Bluetooth RC car

    

Building an Arduino Bluetooth RC car



 อุปกรณ์
  •    โค้ดงาน

    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
    //speed of motors betwen 0 and 255, if you like you can change it
    int pwm_speedA = 255;
    int pwm_speedB = 240;
    char command;
     
    void setup() {
    Serial.begin(9600);
    //pins for motor controller
    pinMode(11, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(3, OUTPUT);
     
    }
     
    void loop() {
     
    if(Serial.available() > 0){
    command = Serial.read();
    motors_stop();
    switch(command){
    case 'F':
    forward();
    break;
    case 'B':
    backward();
    break;
    case 'L':
    left();
    break;
    case 'R':
    right();
    break;
    }
    }
     
    }

     // function for driving straight
    void forward(){
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
     
    digitalWrite(9, HIGH);
    digitalWrite(6, LOW);
     
    analogWrite(5, pwm_speedA);
    analogWrite(3, pwm_speedB);
    }
     
    //function for reversing
    void backward(){
     
    digitalWrite(10, LOW);
    digitalWrite(11, HIGH);
     
    digitalWrite(9, LOW);
    digitalWrite(6, HIGH);
     
    analogWrite(5, pwm_speedA);
    analogWrite(3, pwm_speedB);
    }
     
    //function for turning left
    void left(){
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
     
    digitalWrite(9, HIGH);
    digitalWrite(6, LOW);
     
    analogWrite(3, 0);
    analogWrite(5, pwm_speedA);
    }
     
    //function for turning right
    void right(){
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
     
     
    digitalWrite(9, LOW);
    digitalWrite(6, LOW);
     
    analogWrite(3, pwm_speedB);
    analogWrite(5, 0);
     
    }
     
    //function for stopping motors
    void motors_stop(){
     
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
     
    digitalWrite(9,LOW);
    digitalWrite(6, LOW);
     
    analogWrite(5, 0);
    analogWrite(3, 0);
    }

ไม่มีความคิดเห็น:

แสดงความคิดเห็น