Components and supplies
1
Capacitive Proximity Sensor, M30
1
SG90 Micro-servo motor
1
HC-06 Bluetooth Module
1
Jumper wires (generic)
1
Speaker: 0.25W, 8 ohms
1
Ultrasonic Sensor - HC-SR04 (Generic)
1
Development Board, Motor Control Shield
1
DC motor (generic)
Apps and platforms
1
Android Studio
1
Arduino IDE
Project description
Code
test_motor_bluetooth_servo_obsticalSensor.ino
arduino
1#include <AFMotor.h> //for motor sheild 2#include <Servo.h> // for servo motor 3#include <NewPing.h> //for obstical sensor 4#include <TinyGPS++.h> 5#include <SoftwareSerial.h> 6 7SoftwareSerial serial_connection(12, 11); //RX=pin 12, TX=pin 11 8TinyGPSPlus gps; 9#define TRIG_PIN A2 10#define ECHO_PIN A4 11#define MAX_DISTANCE 100 12 13//motors 14 AF_DCMotor motor1(3); 15 AF_DCMotor motor2(2); 16 17 //obstical sensor 18 NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); 19int distance = 0; 20 21//metal sensor 22float metalDetected; 23int monitoring; 24int metalDetection = 0; 25 26 //servo motor 27Servo servo; 28int servo_position =0 ; 29int pos=0; 30 31//test 32int ledPin = 13; 33int state = 0; 34int flag = 0; 35 36void setup() { 37 // GPS(); 38 39 pinMode(ledPin, OUTPUT); 40 digitalWrite(ledPin, LOW); 41 pinMode(8,OUTPUT);// for speaker 42 pinMode(5,OUTPUT);// for carLeds 43 pinMode(7,OUTPUT);// for carLeds 44 servo.attach (10);// Define the servo signal pins 45 Serial.begin(9600); // Default connection rate for my BT module 46 Serial.println("robot is srtarted"); 47 serial_connection.begin(9600);//This opens up communications to the GPS 48 49 Serial.println("GPS is srtarted"); 50 } 51 52int readPing() { //it is used for ultrasonic obstical sensor 53 delay(70); 54 int cm = sonar.ping_cm(); 55 if(cm==0) 56 { 57 cm = 250; 58 } 59 return cm; 60} 61 62void loop() { 63int last_servo_position=0; 64distance = readPing(); 65 66 67 if(Serial.available() > 0){ 68 state = Serial.read(); 69 Serial.write(state); 70 flag=0; 71 } 72 73//motor shield 74 if (state == 's') { 75 motor1.setSpeed(0); 76 motor2.setSpeed(0); 77if(pos !=last_servo_position) { 78 servo.write(pos); 79 //last_servo_position = pos; 80 servo.detach(); 81} 82 83 if(flag == 0){ 84 Serial.println("state: off"); 85 flag = 1; 86 } 87 } 88 89 else if (state == 'w') { //forward 90 motor2.run(FORWARD); 91 motor2.setSpeed(255); 92 if(flag == 0){ 93 Serial.println("state: on"); 94 flag = 1; 95 } 96 } 97 else if (state == 'x') { //back 98 motor2.run(BACKWARD); 99 motor2.setSpeed(255); 100 if(flag == 0){ 101 Serial.println("state: on"); 102 flag = 1; 103 } 104 } 105 else if (state == 'd') { // left 106 motor1.run(FORWARD); 107 motor1.setSpeed(255); 108 if(flag == 0){ 109 Serial.println("Left: on"); 110 flag = 1; 111 } 112 } 113 else if (state == 'a') { //right 114 motor1.run(BACKWARD); 115 motor1.setSpeed(255); 116 if(flag == 0){ 117 Serial.println("Right: on"); 118 flag = 1; 119 } 120 } 121 else if (state == 'y') { //stop motor1 for left and right 122 motor1.setSpeed(0); 123 if(flag == 0){ 124 Serial.println("motor1: on"); 125 flag = 1; 126 } 127 } 128 else if (state == 'p') { //stop motor2 for forward and backward 129 motor2.setSpeed(0); 130 if(flag == 0){ 131 Serial.println("motor2: of"); 132 flag = 1; 133 } 134 } 135 else if (state == 'e') { //stop motor 1 to turn right or left 136 motor1.setSpeed(0); 137 if(flag == 0){ 138 Serial.println("LED: of"); 139 flag = 1; 140 } 141 } 142// servo motor 143 else if (state == 'q') 144 { 145 servo.attach (10); 146 for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees 147 // in steps of 1 degree 148 servo.write(pos); 149 minesFinder(); 150 ultrasonicSensor(); 151 delay(15); // waits 15ms for the servo to reach the position 152 } 153 for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees 154 servo.write(pos); 155 minesFinder(); 156 ultrasonicSensor(); 157 delay(15); // waits 15ms for the servo to reach the position 158 } 159 } 160 ultrasonicSensor(); 161minesFinder(); 162delay(50); 163 //Serial.println(monitoring); 164 165} 166 167 168 169 170 171 172void minesFinder() 173{ 174 monitoring = analogRead(metalDetection); 175metalDetected = (float) monitoring*100/1024.0; 176 if (monitoring < 250) 177{ 178Serial.println("mine bomb is Detected"); 179//GPS(); 180digitalWrite(8,HIGH); 181digitalWrite(5,HIGH); 182digitalWrite(7,HIGH); 183delay(2000); 184} 185else{ 186//Serial.println("mine bomb is not Detected"); 187 digitalWrite(8, LOW); 188 digitalWrite(5,LOW ); 189 digitalWrite(7,LOW ); 190} 191} 192 193void ultrasonicSensor() // ultrasonic obstical sensor 194{ 195 196 distance = readPing(); 197 if(distance<=15) 198 { 199 state='s'; 200 // motor1.setSpeed(0); 201 //motor2.setSpeed(0); 202 203 Serial.println(distance); 204 } 205 } 206 207 void stopMotorServo() 208 { 209 int last_servo_position; 210 if(pos !=last_servo_position) { 211 servo.write(pos); 212 last_servo_position = pos; 213} 214 } 215 void GPS() 216 { 217 while(serial_connection.available())//While there are characters to come from the GPS 218 { 219 gps.encode(serial_connection.read());//This feeds the serial NMEA data into the library one char at a time 220 } 221 if(gps.location.isUpdated())//This will pretty much be fired all the time anyway but will at least reduce it to only after a package of NMEA data comes in 222 { 223 //Get the latest info from the gps object which it derived from the data sent by the GPS unit 224 Serial.println("Satellite Count:"); 225 Serial.println(gps.satellites.value()); 226 Serial.println("Latitude:"); 227 Serial.println(gps.location.lat(), 6); 228 Serial.println("Longitude:"); 229 Serial.println(gps.location.lng(), 6); 230 Serial.println("Speed MPH:"); 231 Serial.println(gps.speed.mph()); 232 Serial.println("Altitude Feet:"); 233 Serial.println(gps.altitude.feet()); 234 Serial.println(""); 235 } 236 } 237 238
Downloadable files
untitled_PP3tUO7TMn.png
untitled_PP3tUO7TMn.png

untitled_PP3tUO7TMn.png
untitled_PP3tUO7TMn.png

Comments
Only logged in users can leave comments