ÞThis is the 14CORE Design light finder robot which is capable of detecting, finding and following light source path. This robot uses LDR (Photodiodes) to detect light from the surroundings and follow where the light source. As you can see the diagram below there are two Motor driver option you can use ether L298X or L293D which is used to drive the 2 wheel or 4 wheel motor. If you’re new to L298N and L293D please follow this link. L293D / L298N.
Required Component
- Arduino Microcontroller / Teensy (Please refer to the pinout diagram)
- L298N / l293D Motor Controller
- Smar Car Chassis Kit / Build your own kit
- 2 or 4 Gear Head Motor or Stepper motor (for stepper motor please follow this link)
- Micro / Standard Servo
- 3x LDR (Light Dependent Resistor)
- 3x 10k Resistor
- Jumper Wires / DuPont Wires
- Solder Less Breadboard
Wiring Diagram
Source Code
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
#include <MsTimer2.h> #include <Servo.h> #include "hc595.h" unsigned char code_buf[] = {0, 0, 0, 0};//Light Intensity unsigned char code_val[] = {0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff}; Servo myservo; #define ENA_PIN 5 // ENA attach to pin5 #define ENB_PIN 6 // ENB attach to pin6 #define ML_1 8 // ML_1 attach to pin8 #define ML_2 9 // ML_1 attach to pin9 #define MR_1 10 // MR_1 attach to pin10 #define MR_2 11 // MR_1 attach to pin11 #define FORWARD 0 //define forward=0,car move forward #define BACK 1 //define back=1,car move back #define LDR_1_PIN A0 //LDR attach to pin A0 #define LDR_2_PIN A1 //LDR attach to pin A1 #define LDR_3_PIN A2 //LDR attach to pin A2 #define LDR_4_PIN A3 //LDR attach to pin A3 #define SPEED 220 //define SPEED=180,it is the rotate speed of motor #define ANGLE_RIGHT_MAX 115 //Define the servo on the right side of the maximum rotation Angle is 120 #define ANGLE_RIGHT_HALF 110 #define ANGLE_LEFT_MAX 65 //Define the servo on the left side of the maximum rotation Angle is 120 #define ANGLE_LEFT_HALF 70 #define ANGLE_MIDDLE 90 int val_L_Dir = 0; int val_L_Init = 0; int val_L_Temp = 0; int val_M_Dir = 0; int val_M_Init = 0; int val_M_Temp = 0; int val_B_Dir = 0; int val_B_Init = 0; int val_B_Temp = 0; int val_R_Init = 0; int val_R_Dir = 0; int val_R_Temp = 0; int val_Light_threshold = 0; unsigned char state = 0; unsigned char speed = 0; unsigned char duration = 90; unsigned char dir = 0; unsigned char duration_lastest = 0; unsigned char angle = 0; void setup() { HC595_Init(); pinMode(ENA_PIN, OUTPUT); pinMode(ENB_PIN, OUTPUT); pinMode(ML_1, OUTPUT); pinMode(ML_2, OUTPUT); pinMode(MR_1, OUTPUT); pinMode(MR_2, OUTPUT); val_L_Init = average(LDR_1_PIN, 1024); val_M_Init = average(LDR_2_PIN, 1024); val_R_Init = average(LDR_3_PIN, 1024); val_B_Init = average(LDR_4_PIN, 1024); Serial.begin(9600); /*Serial.print(val_L_Init); Serial.print(","); Serial.print(val_M_Init); Serial.print(","); Serial.print(val_R_Init); Serial.print(","); Serial.println(val_B_Init);*/ myservo.attach(2);//servo attach to pin2 myservo.write(90); //the initial angle of servo is 90 MsTimer2::set(10, SERVO_Write); //10 periods,then run SERVO_Write() MsTimer2::start();//start timing } void loop() { val_Light_threshold = average(A4,10); Serial.println( val_Light_threshold); val_L_Dir = average(LDR_1_PIN, 50); val_M_Dir = average(LDR_2_PIN, 50); val_R_Dir = average(LDR_3_PIN, 50); val_B_Dir = average(LDR_4_PIN, 50); // DEBUG /*Serial.print(val_L_Dir); Serial.print(","); Serial.print(val_M_Dir); Serial.print(","); Serial.print(val_R_Dir); Serial.print(","); Serial.println(val_B_Dir);*/ state = Light_Max(val_L_Dir, val_M_Dir, val_R_Dir, val_B_Dir); switch(state) { case 0: { speed = SPEED; duration = ANGLE_LEFT_MAX; dir = FORWARD; break; } case 1: { speed = SPEED; duration = ANGLE_MIDDLE; dir = FORWARD; break; } case 2: { speed = SPEED; duration = ANGLE_RIGHT_MAX; dir = FORWARD; break; } case 3: { speed = SPEED; duration = ANGLE_MIDDLE; dir = BACK; break; } case 4: { speed = 0; duration = duration; dir = FORWARD; break; } default: { speed = 0; duration = 75; dir = FORWARD; } } val_L_Temp = map(val_L_Temp, 0, 1023, 0, 8); val_M_Temp = map(val_M_Temp, 0, 1023, 0, 8); val_R_Temp = map(val_R_Temp, 0, 1023, 0, 8); val_B_Temp = map(val_B_Temp, 0, 1023, 0, 8); code_buf[0] = code_val[val_L_Temp]; code_buf[1] = code_val[val_M_Temp]; code_buf[2] = code_val[val_R_Temp]; code_buf[3] = code_val[val_B_Temp]; LEDBAR_V_Dis(code_buf); CAR_move(dir,speed,speed); } unsigned char Light_Max(int x, int y, int z, int u) { int x_tmp, y_tmp, z_tmp, u_tmp; val_L_Temp = abs(x - val_L_Init); val_M_Temp = abs(y - val_M_Init); val_R_Temp = abs(z - val_R_Init); val_B_Temp = abs(u - val_B_Init); // DEBUG /*Serial.print(x); Serial.print(","); Serial.print(y); Serial.print(","); Serial.print(z); Serial.print(","); Serial.println(u);*/ int maxValue,maxValue_tmp; maxValue = val_Max(x,y,z,u); maxValue_tmp = val_Max(x_tmp, y_tmp, z_tmp, u_tmp); //Serial.println(maxValue); //Serial.println(maxValue_tmp); if(maxValue < val_Light_threshold) return 4; //else if(maxValue_tmp == x_tmp) return 0; else if(maxValue == x) return 0; //else if(maxValue_tmp == y_tmp) return 1; else if(maxValue == y) return 1; //else if(maxValue_tmp == z_tmp) return 2; else if(maxValue == z) return 2; //else if(maxValue_tmp == u_tmp) return 3; else if(maxValue == u) return 3; //else return 4; } void CAR_move(unsigned char direction, unsigned char speed_left, unsigned char speed_right) { switch(direction) { case 0: digitalWrite(ML_1,HIGH);digitalWrite(ML_2,LOW); digitalWrite(MR_1,HIGH);digitalWrite(MR_2,LOW); break; case 1: digitalWrite(ML_1,LOW);digitalWrite(ML_2,HIGH); digitalWrite(MR_1,LOW);digitalWrite(MR_2,HIGH); break; default: break; } analogWrite(ENA_PIN,speed_left); analogWrite(ENB_PIN,speed_right); } int average(unsigned char pin, int num) { unsigned long value = 0; for(int i = 0; i < num - 1; i++) value = value + analogRead(pin); return(value/num); } int val_Max(int val_1,int val_2,int val_3,int val_4) { int max_val = 0; if(max_val < val_1) max_val = val_1; if(max_val < val_2) max_val = val_2; if(max_val < val_3) max_val = val_3; if(max_val < val_4) max_val = val_4; return(max_val); } void SERVO_Write(void) { //angle = duration_lastest; if(angle < duration) angle++; else if(angle > duration) angle --; myservo.write(angle); duration_lastest = duration; } |
Dowloads
Download MSTIME2 Library | Zip
14CORE Light Tracking/Finder BOT on L298N / L293D with Microcontroller