Components and supplies
Adafuit Motor shield
Arduino Nano R3
433mhz rf transmitter and receiver
Arduino UNO
GY-521 MPU-6050 3 Axis Gyroscope + Accelerometer Module For Arduino
Ultrasonic Sensor - HC-SR04 (Generic)
Tools and machines
car chassis
Project description
Code
receiver (RC Car)
arduino
this code is for rc car to receive signals
1#include <VirtualWire.h> //VirtualWire library declaration 2#include <AFMotor.h> 3byte msg[VW_MAX_MESSAGE_LEN];//Data flow is received 4byte msgLen = VW_MAX_MESSAGE_LEN; 5char forward[1] = {1}; 6 7AF_DCMotor motor1(1, MOTOR12_1KHZ); 8AF_DCMotor motor2(2, MOTOR12_1KHZ); 9AF_DCMotor motor3(3, MOTOR34_1KHZ); 10AF_DCMotor motor4(4, MOTOR34_1KHZ); 11 12void setup() 13{ 14 Serial.begin(9600); 15 Serial.println("READY.........."); 16 vw_setup(2048); // set the baud rate 17 vw_set_rx_pin(2);// Set up digital pins to receive signals 18 vw_rx_start();// started receiving the signal 19} 20 21 22void loop() 23{ 24 if (vw_get_message(msg, &msgLen)) // if the signal is transmitted 25 { 26 Serial.print("got transmition"); 27 motor1.setSpeed(255); 28 motor2.setSpeed(255); 29 motor3.setSpeed(255); 30 motor4.setSpeed(255); 31 switch (msg[0]) 32 { 33 case '0': 34 { 35 motor1.run(RELEASE); 36 motor2.run(RELEASE); 37 motor3.run(RELEASE); 38 motor4.run(RELEASE); 39 break; 40 } 41 42 case '1': 43 { 44 motor1.run(FORWARD); 45 motor2.run(FORWARD); 46 motor3.run(FORWARD); 47 motor4.run(FORWARD); 48 break; 49 } 50 51 case '2': 52 { 53 motor1.setSpeed(150); 54 motor2.setSpeed(150); 55 motor3.setSpeed(150); 56 motor4.setSpeed(150); 57 motor1.run(FORWARD); 58 motor2.run(FORWARD); 59 motor3.run(BACKWARD); 60 motor4.run(BACKWARD); 61 break; 62 } 63 64 case '3': 65 { 66 motor1.setSpeed(150); 67 motor2.setSpeed(150); 68 motor3.setSpeed(150); 69 motor4.setSpeed(150); 70 motor1.run(BACKWARD); 71 motor2.run(BACKWARD); 72 motor3.run(FORWARD); 73 motor4.run(FORWARD); 74 break; 75 } 76 77 case '4': 78 { 79 motor1.run(BACKWARD); 80 motor2.run(BACKWARD); 81 motor3.run(BACKWARD); 82 motor4.run(BACKWARD); 83 break; 84 } 85 86 case '5': 87 { 88 motor3.setSpeed(70); 89 motor4.setSpeed(70); 90 motor1.run(FORWARD); 91 motor2.run(FORWARD); 92 motor3.run(FORWARD); 93 motor4.run(FORWARD); 94 break; 95 } 96 97 case '6': 98 { 99 motor1.setSpeed(70); 100 motor2.setSpeed(70); 101 motor1.run(FORWARD); 102 motor2.run(FORWARD); 103 motor3.run(FORWARD); 104 motor4.run(FORWARD); 105 break; 106 } 107 } 108 109 110 111}
RCtransmitter (glove)
arduino
sends signals to rc
1#include <VirtualWire.h> // VirtualWire library declaration 2#include "I2Cdev.h" 3#include "MPU6050.h" 4 5#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE 6#include "Wire.h" 7#endif 8MPU6050 accelgyro; 9int16_t ax, ay, az;//used for making programs cross platform 10int16_t gx, gy, gz;//used for making programs cross platform 11//#define OUTPUT_READABLE_ACCELGYRO 12#define OUTPUT_BINARY_ACCELGYRO 13#define LED_PIN 13 14bool blinkState = false; 15char text[5] = "";// array array declaration 16byte i = 0; 17 18void setup() 19{ 20#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE//communicacate between devices 21 Wire.begin(); 22#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE 23 Fastwire::setup(400, true); 24#endif 25 Serial.begin(9600);// Serial communication with baudrate 9600 26 accelgyro.initialize(); 27 Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); 28 Serial.println("Ready........."); 29 vw_set_ptt_inverted(true);// Requirements for RF link modules 30 vw_setup(2048);// set the baud rate 31 vw_set_tx_pin(8);// Set up digital pins to signal 32 pinMode(LED_PIN, OUTPUT); 33 34} 35 36void loop() 37{ 38 accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); 39#ifdef OUTPUT_READABLE_ACCELGYRO 40 // display tab-separated accel/gyro x/y/z values 41 Serial.print("a/g:\ "); 42 Serial.print(ax); Serial.print("\ "); 43 Serial.print(ay); Serial.print("\ "); 44 Serial.print(az); Serial.print("\ "); 45 Serial.print(gx); Serial.print("\ "); 46 Serial.print(gy); Serial.print("\ "); //-32000 47 Serial.println(gz); 48 //delay(250); 49#endif 50 51#ifdef OUTPUT_BINARY_ACCELGYRO 52 /* Serial.write((uint8_t)(ax >> 8)); Serial.write((uint8_t)(ax & 0xFF)); 53 Serial.write((uint8_t)(ay >> 8)); Serial.write((uint8_t)(ay & 0xFF)); 54 Serial.write((uint8_t)(az >> 8)); Serial.write((uint8_t)(az & 0xFF)); 55 Serial.write((uint8_t)(gx >> 8)); Serial.write((uint8_t)(gx & 0xFF)); 56 Serial.write((uint8_t)(gy >> 8)); Serial.write((uint8_t)(gy & 0xFF)); 57 Serial.write((uint8_t)(gz >> 8)); Serial.write((uint8_t)(gz & 0xFF)); */ 58#endif 59 60 61 if ((ay < 9000) and (ay > -9000) and (ax < 12000) and (ax > -3000)) 62 { 63 Serial.println("Ready........."); 64 text[i] = '0'; 65 vw_send((byte *)text, sizeof(text));// send the signal away 66 vw_wait_tx(); 67 68 } 69 70 if (ax < -3800) 71 { 72 if (ay < -13000) 73 { 74 Serial.println("to the right"); 75 text[i] = '5'; // to the right 76 vw_send((byte *)text, sizeof(text));// send the signal away 77 vw_wait_tx(); 78 } 79 else if (ay > 8000) 80 { 81 Serial.println("to the left"); 82 text[i] = '6'; // to the left 83 vw_send((byte *)text, sizeof(text));// send the signal away 84 vw_wait_tx(); 85 } 86 else 87 { 88 Serial.println("to the left"); 89 text[i] = '1'; //tới 90 vw_send((byte *)text, sizeof(text));// send the signal away 91 vw_wait_tx(); 92 } 93 } 94 95 if (ay < -13000) 96 { 97 if (ax < -3800) 98 { 99 Serial.println("to the right"); 100 text[i] = '5'; // to the right 101 vw_send((byte *)text, sizeof(text));// send the signal away 102 vw_wait_tx(); 103 } 104 else 105 { 106 Serial.println("to the right"); 107 text[i] = '2'; // right 108 vw_send((byte *)text, sizeof(text));// send the signal away 109 vw_wait_tx(); 110 } 111 } 112 113 if (ay > 7000) 114 { 115 if (ax < -3800) 116 { 117 Serial.println("to the left"); 118 text[i] = '6'; // to the left 119 vw_send((byte *)text, sizeof(text));// send the signal away 120 vw_wait_tx(); 121 } 122 else 123 { 124 Serial.println("to the left"); 125 text[i] = '3'; // to the left 126 vw_send((byte *)text, sizeof(text));// send the signal away 127 vw_wait_tx(); 128 } 129 } 130 131 if (ax > 14000) 132 { 133 text[i] = '4'; //backward 134 vw_send((byte *)text, sizeof(text));// send the signal away 135 vw_wait_tx(); 136 } 137 138 139 140 blinkState = !blinkState; 141 digitalWrite(LED_PIN, blinkState); 142 143}
RCtransmitter (glove)
arduino
sends signals to rc
1#include <VirtualWire.h> // VirtualWire library declaration 2#include "I2Cdev.h" 3#include "MPU6050.h" 4 5#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE 6#include "Wire.h" 7#endif 8MPU6050 accelgyro; 9int16_t ax, ay, az;//used for making programs cross platform 10int16_t gx, gy, gz;//used for making programs cross platform 11//#define OUTPUT_READABLE_ACCELGYRO 12#define OUTPUT_BINARY_ACCELGYRO 13#define LED_PIN 13 14bool blinkState = false; 15char text[5] = "";// array array declaration 16byte i = 0; 17 18void setup() 19{ 20#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE//communicacate between devices 21 Wire.begin(); 22#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE 23 Fastwire::setup(400, true); 24#endif 25 Serial.begin(9600);// Serial communication with baudrate 9600 26 accelgyro.initialize(); 27 Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); 28 Serial.println("Ready........."); 29 vw_set_ptt_inverted(true);// Requirements for RF link modules 30 vw_setup(2048);// set the baud rate 31 vw_set_tx_pin(8);// Set up digital pins to signal 32 pinMode(LED_PIN, OUTPUT); 33 34} 35 36void loop() 37{ 38 accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); 39#ifdef OUTPUT_READABLE_ACCELGYRO 40 // display tab-separated accel/gyro x/y/z values 41 Serial.print("a/g:\ "); 42 Serial.print(ax); Serial.print("\ "); 43 Serial.print(ay); Serial.print("\ "); 44 Serial.print(az); Serial.print("\ "); 45 Serial.print(gx); Serial.print("\ "); 46 Serial.print(gy); Serial.print("\ "); //-32000 47 Serial.println(gz); 48 //delay(250); 49#endif 50 51#ifdef OUTPUT_BINARY_ACCELGYRO 52 /* Serial.write((uint8_t)(ax >> 8)); Serial.write((uint8_t)(ax & 0xFF)); 53 Serial.write((uint8_t)(ay >> 8)); Serial.write((uint8_t)(ay & 0xFF)); 54 Serial.write((uint8_t)(az >> 8)); Serial.write((uint8_t)(az & 0xFF)); 55 Serial.write((uint8_t)(gx >> 8)); Serial.write((uint8_t)(gx & 0xFF)); 56 Serial.write((uint8_t)(gy >> 8)); Serial.write((uint8_t)(gy & 0xFF)); 57 Serial.write((uint8_t)(gz >> 8)); Serial.write((uint8_t)(gz & 0xFF)); */ 58#endif 59 60 61 if ((ay < 9000) and (ay > -9000) and (ax < 12000) and (ax > -3000)) 62 { 63 Serial.println("Ready........."); 64 text[i] = '0'; 65 vw_send((byte *)text, sizeof(text));// send the signal away 66 vw_wait_tx(); 67 68 } 69 70 if (ax < -3800) 71 { 72 if (ay < -13000) 73 { 74 Serial.println("to the right"); 75 text[i] = '5'; // to the right 76 vw_send((byte *)text, sizeof(text));// send the signal away 77 vw_wait_tx(); 78 } 79 else if (ay > 8000) 80 { 81 Serial.println("to the left"); 82 text[i] = '6'; // to the left 83 vw_send((byte *)text, sizeof(text));// send the signal away 84 vw_wait_tx(); 85 } 86 else 87 { 88 Serial.println("to the left"); 89 text[i] = '1'; //tới 90 vw_send((byte *)text, sizeof(text));// send the signal away 91 vw_wait_tx(); 92 } 93 } 94 95 if (ay < -13000) 96 { 97 if (ax < -3800) 98 { 99 Serial.println("to the right"); 100 text[i] = '5'; // to the right 101 vw_send((byte *)text, sizeof(text));// send the signal away 102 vw_wait_tx(); 103 } 104 else 105 { 106 Serial.println("to the right"); 107 text[i] = '2'; // right 108 vw_send((byte *)text, sizeof(text));// send the signal away 109 vw_wait_tx(); 110 } 111 } 112 113 if (ay > 7000) 114 { 115 if (ax < -3800) 116 { 117 Serial.println("to the left"); 118 text[i] = '6'; // to the left 119 vw_send((byte *)text, sizeof(text));// send the signal away 120 vw_wait_tx(); 121 } 122 else 123 { 124 Serial.println("to the left"); 125 text[i] = '3'; // to the left 126 vw_send((byte *)text, sizeof(text));// send the signal away 127 vw_wait_tx(); 128 } 129 } 130 131 if (ax > 14000) 132 { 133 text[i] = '4'; //backward 134 vw_send((byte *)text, sizeof(text));// send the signal away 135 vw_wait_tx(); 136 } 137 138 139 140 blinkState = !blinkState; 141 digitalWrite(LED_PIN, blinkState); 142 143}
Downloadable files
Transmitter glove
Transmitter glove
RC car Flowchart
RC car Flowchart
RC car Flowchart
receiver (RC Car)
receiver (RC Car)
RC car Flowchart
RC car Flowchart
RC car Flowchart
receiver (RC Car)
receiver (RC Car)
Transmitter glove
Transmitter glove
Comments
Only logged in users can leave comments
mcharrison500
0 Followers
•0 Projects
Table of contents
Intro
5
0