Components and supplies
Arduino UNO
Relay Module (Generic)
HC-05 Bluetooth Module
Resistor 1k ohm
Apps and platforms
MIT App Inventor 2
Project description
Code
The Code
c_cpp
1#define enA 9 2#define enB 10 3#define in1 8 4#define in2 7 5#define 6 in3 6 7#define in4 5 8#define rly 2 9 10//infared pins 11#define lftIr 12 12#define 13 ctrIr 4 14#define rghIr 3 15 16String msg=""; 17int spd=150; 18bool swtch=0; 19 20void 21 setup() { 22 // put your setup code here, to run once: 23 pinMode(enA, OUTPUT); 24 25 pinMode(enB, OUTPUT); 26 pinMode(in1, OUTPUT); 27 pinMode(in2, OUTPUT); 28 29 pinMode(in3, OUTPUT); 30 pinMode(in4, OUTPUT); 31 pinMode(rly, OUTPUT); 32 33 34 Serial.begin(38400); 35} 36 37void loop() { 38 // put your main code 39 here, to run repeatedly: 40 if(Serial.available()){ 41 msg=Serial.readString(); 42 43 } 44 45 if (msg=="start cleaning"){ 46 swtch=1; 47 } 48 else if(msg=="stop 49 cleaning"){ 50 swtch=0; 51 } 52 if (swtch==1) { 53 moveBot(); 54 digitalWrite(rly,HIGH); 55 56 } 57 else{ 58 digitalWrite(rly,LOW); 59 stopAll(); 60 } 61} 62 63void 64 moveBot(){ 65 //delay(500); 66 // stopAll(); 67 //delay(2000); 68 analogWrite(enA, 69 100); 70 analogWrite(enB, 100); 71 int leftIr=digitalRead(lftIr); 72 int 73 centerIr=digitalRead(ctrIr); 74 int rightIr=digitalRead(rghIr); 75 76 77 78 if (leftIr==LOW && rightIr==HIGH){ 79 while(leftIr==LOW){ 80 rightMov(); 81 82 leftIr=digitalRead(lftIr); 83 } 84 stopAll(); 85 } 86 87 else if (leftIr==HIGH && rightIr==LOW){ 88 stopAll(); 89 while(rightIr==LOW){ 90 91 leftMov(); 92 rightIr=digitalRead(rghIr); 93 } 94 95 stopAll(); 96 } 97 else if (leftIr==LOW && centerIr==LOW && 98 rightIr==LOW){ 99 stopAll(); 100 while(leftIr==LOW || centerIr==LOW 101 || rightIr==LOW){ 102 backMov(); 103 leftIr=digitalRead(lftIr); 104 105 centerIr=digitalRead(ctrIr); 106 rightIr=digitalRead(rghIr); 107 108 } 109 stopAll(); 110 } 111 112 113 else if (leftIr==HIGH && centerIr==LOW && rightIr==HIGH){ 114 stopAll(); 115 116 while(centerIr==LOW){ 117 backMov(); 118 centerIr=digitalRead(ctrIr); 119 120 } 121 stopAll(); 122 delay(10); 123 leftMov(); 124 125 delay(500); 126 stopAll(); 127 } 128 else{ 129 fowardMov(); 130 131 132 } 133 } 134void fowardMov(){ 135 digitalWrite(in2,HIGH); 136 137 digitalWrite(in4,HIGH); 138 digitalWrite(in1, LOW); 139 digitalWrite(in3, LOW); 140} 141 142void 143 backMov(){ 144 digitalWrite(in1,HIGH); 145 digitalWrite(in3,HIGH); 146 digitalWrite(in2, 147 LOW); 148 digitalWrite(in4, LOW); 149} 150 151void rightMov(){ 152 digitalWrite(in1, 153 HIGH); 154 digitalWrite(in4, HIGH); 155 digitalWrite(in2, LOW); 156 digitalWrite(in3, 157 LOW); 158} 159 160void leftMov(){ 161 digitalWrite(in2, HIGH); 162 digitalWrite(in3, 163 HIGH); 164 digitalWrite(in1, LOW); 165 digitalWrite(in4, LOW); 166} 167 168void 169 stopAll(){ 170 digitalWrite(in1, LOW); 171 digitalWrite(in2, LOW); 172 digitalWrite(in3, 173 LOW); 174 digitalWrite(in4, LOW); 175} 176
The Code
c_cpp
1#define enA 9 2#define enB 10 3#define in1 8 4#define in2 7 5#define in3 6 6#define in4 5 7#define rly 2 8 9//infared pins 10#define lftIr 12 11#define ctrIr 4 12#define rghIr 3 13 14String msg=""; 15int spd=150; 16bool swtch=0; 17 18void setup() { 19 // put your setup code here, to run once: 20 pinMode(enA, OUTPUT); 21 pinMode(enB, OUTPUT); 22 pinMode(in1, OUTPUT); 23 pinMode(in2, OUTPUT); 24 pinMode(in3, OUTPUT); 25 pinMode(in4, OUTPUT); 26 pinMode(rly, OUTPUT); 27 28 Serial.begin(38400); 29} 30 31void loop() { 32 // put your main code here, to run repeatedly: 33 if(Serial.available()){ 34 msg=Serial.readString(); 35 } 36 37 if (msg=="start cleaning"){ 38 swtch=1; 39 } 40 else if(msg=="stop cleaning"){ 41 swtch=0; 42 } 43 if (swtch==1) { 44 moveBot(); 45 digitalWrite(rly,HIGH); 46 } 47 else{ 48 digitalWrite(rly,LOW); 49 stopAll(); 50 } 51} 52 53void moveBot(){ 54 //delay(500); 55 // stopAll(); 56 //delay(2000); 57 analogWrite(enA, 100); 58 analogWrite(enB, 100); 59 int leftIr=digitalRead(lftIr); 60 int centerIr=digitalRead(ctrIr); 61 int rightIr=digitalRead(rghIr); 62 63 64 if (leftIr==LOW && rightIr==HIGH){ 65 while(leftIr==LOW){ 66 rightMov(); 67 leftIr=digitalRead(lftIr); 68 } 69 stopAll(); 70 } 71 else if (leftIr==HIGH && rightIr==LOW){ 72 stopAll(); 73 while(rightIr==LOW){ 74 leftMov(); 75 rightIr=digitalRead(rghIr); 76 } 77 stopAll(); 78 } 79 else if (leftIr==LOW && centerIr==LOW && rightIr==LOW){ 80 stopAll(); 81 while(leftIr==LOW || centerIr==LOW || rightIr==LOW){ 82 backMov(); 83 leftIr=digitalRead(lftIr); 84 centerIr=digitalRead(ctrIr); 85 rightIr=digitalRead(rghIr); 86 } 87 stopAll(); 88 } 89 90 else if (leftIr==HIGH && centerIr==LOW && rightIr==HIGH){ 91 stopAll(); 92 while(centerIr==LOW){ 93 backMov(); 94 centerIr=digitalRead(ctrIr); 95 } 96 stopAll(); 97 delay(10); 98 leftMov(); 99 delay(500); 100 stopAll(); 101 } 102 else{ 103 fowardMov(); 104 105 } 106 } 107void fowardMov(){ 108 digitalWrite(in2,HIGH); 109 digitalWrite(in4,HIGH); 110 digitalWrite(in1, LOW); 111 digitalWrite(in3, LOW); 112} 113 114void backMov(){ 115 digitalWrite(in1,HIGH); 116 digitalWrite(in3,HIGH); 117 digitalWrite(in2, LOW); 118 digitalWrite(in4, LOW); 119} 120 121void rightMov(){ 122 digitalWrite(in1, HIGH); 123 digitalWrite(in4, HIGH); 124 digitalWrite(in2, LOW); 125 digitalWrite(in3, LOW); 126} 127 128void leftMov(){ 129 digitalWrite(in2, HIGH); 130 digitalWrite(in3, HIGH); 131 digitalWrite(in1, LOW); 132 digitalWrite(in4, LOW); 133} 134 135void stopAll(){ 136 digitalWrite(in1, LOW); 137 digitalWrite(in2, LOW); 138 digitalWrite(in3, LOW); 139 digitalWrite(in4, LOW); 140} 141
Downloadable files
The apk
The apk
The apk
The apk
The Circuit
I used 3 18650 Li ion batteries not 2.
The Circuit
Comments
Only logged in users can leave comments