Components and supplies
LED (generic)
Breadboard (generic)
Pan/Tilt kit with servos
HM-10 Bluetooth module
Arduino UNO
Apps and platforms
iRobbie-A
Project description
Code
Arduino Code
arduino
1#include <Servo.h> 2 3Servo servox; 4Servo servoy; 5Servo servoz; 6int 7 angle = 0; 8boolean forward1 = false ; 9String bluetoothRead, Str_x, Str_y, 10 Str_p, Str_s; 11int x ; 12int y ; 13int x1 ; 14int y1 ; 15int points; 16int 17 length; 18const int buzzer = 10; 19int s; 20int led1 = 3; // the pin 21 that the LED1 is attached to 22int led2 = 2; // the pin that the LED2 23 is attached to 24int brightness = 0; // how bright the LED is 25int fadeAmount 26 = 1; // how many points to fade the LED by 27const long interval = 1000; // 28 interval at which to blink (milliseconds 29const long interval_short = 100; 30int 31 ledState = LOW; // ledState used to set the LED 32unsigned long previousMillis 33 = 0; // will store last time LED was updated 34 35 36 37#define ENA 11 38#define 39 ENB 6 40#define IN1 5 41#define IN2 7 42#define IN3 8 43#define IN4 9 44#define 45 DELAY 20 46 47 48void setup() { 49 Serial.begin(9600); 50 Serial.flush(); 51 52 pinMode(IN1,OUTPUT); 53 pinMode(IN2,OUTPUT); 54 pinMode(IN3,OUTPUT); 55 pinMode(IN4,OUTPUT); 56 57 pinMode(ENA,OUTPUT); 58 pinMode(ENB,OUTPUT); 59 pinMode(buzzer, OUTPUT); 60 61 pinMode(led1, OUTPUT); 62 pinMode(led2, OUTPUT); 63 64 servox.attach(13); 65 66 servoy.attach(12); 67 servoz.attach(8); 68 servox.write(90); 69 servoy.write(90); 70 71 servoz.write(90); 72 73} 74 75void loop() { 76 int i=0; 77 char commandbuffer[200]; 78 79 80 81 if(Serial.available()){ 82 83 delay(15); 84 85 while( Serial.available() 86 && i< 199) { 87 commandbuffer[i++] = Serial.read(); 88 89 90 91 } 92 commandbuffer[i++]='\\0'; 93 bluetoothRead = (char*)commandbuffer; 94 95 length = bluetoothRead.length(); 96 Serial.println(bluetoothRead); 97 98 99 100 if(bluetoothRead.substring(0, 1).equals("x")){ 101 102 int 103 i=1; 104 while(bluetoothRead.substring(i, i+1) != ("y")){ 105 i++; 106 107 } 108 109 Str_x = bluetoothRead.substring(1, i); 110 x 111 = Str_x.toInt(); 112 113 114 115 Str_y = bluetoothRead.substring(i+1, 116 length - 2); 117 y = Str_y.toInt(); 118 119 120 Str_p = bluetoothRead.substring(length 121 - 2, length - 1); 122 points = Str_p.toInt(); 123 124 125 Str_s 126 = bluetoothRead.substring(length - 1, length); 127 s = Str_s.toInt(); 128 129 130 i = 1; 131 132 Serial.println(x); 133 Serial.println(y); 134 Serial.println(points); 135 136 Serial.println(s); 137 Serial.println(length ); 138 139 140 141} 142} 143 144 servomove(); 145 if (x1 != 0 && y1 != 0) { 146 ledblink(); 147 if 148 (s == 1) { 149 ledlaugh(); 150 servomouth(); 151 Serial.println("Smile!"); 152 153 } 154 } 155 else{ 156 ledState = LOW; 157 Serial.println("NotSmile"); 158 159 digitalWrite(led1, ledState); 160 digitalWrite(led2, ledState); 161 } 162 163 164} 165 166 167void servomove(){ 168 169 x1 = x/3; 170 y1 = y/4; 171 172 if (points == 4) { 173 174 servox.write(90-x1); 175 176 servoy.write(90-y1); 177 178 delay(10); 179 180 } 181 if (points == 3) 182 { 183 servox.write(90+x1); 184 185 servoy.write(90-y1); 186 delay(10); 187 } 188 189if (points == 7) 190 191 { 192 servox.write(90+x1); 193 servoy.write(y1+90); 194 195 delay(10); 196 } 197 198 if (points == 8) 199 { 200 201 servox.write(90-x1); 202 servoy.write(y1+90); 203 delay(10); 204 205 } 206 207 if (x1 == 0 && y1 == 0) 208 { 209 210 servox.write(90); 211 servoy.write(90); 212 delay(10); 213 } 214 215 216} 217void ledblink(){ 218 unsigned long currentMillis = millis(); 219 if 220 (currentMillis - previousMillis >= interval) { 221 // save the last time you 222 blinked the LED 223 previousMillis = currentMillis; 224 225 // if the LED 226 is off turn it on and vice-versa: 227 if (ledState == LOW) { 228 ledState 229 = HIGH; 230 } else { 231 ledState = LOW; 232 } 233 // set the LED 234 with the ledState of the variable: 235 digitalWrite(led1, ledState); 236 digitalWrite(led2, 237 ledState); 238 } 239} 240 241void ledlaugh(){ 242 unsigned long currentMillis 243 = millis(); 244 if (currentMillis - previousMillis >= interval_short) { 245 // 246 save the last time you blinked the LED 247 previousMillis = currentMillis; 248 249 250 // if the LED is off turn it on and vice-versa: 251 if (ledState == LOW) 252 { 253 ledState = HIGH; 254 } else { 255 ledState = LOW; 256 } 257 258 // set the LED with the ledState of the variable: 259 digitalWrite(led1, 260 ledState); 261 digitalWrite(led2, ledState); 262 } 263} 264 265void servomouth(){ 266 267 unsigned long currentMillis = millis(); 268 269 if (millis() - previousMillis 270 >= DELAY) 271 {previousMillis += DELAY; 272 if (forward1) 273 { 274 275 servoz.write(-- angle); 276 if(angle == 100) 277 forward1 = 278 false; 279 } 280 else 281 { 282 servoz.write(++ angle); 283 284 if (angle == 140) 285 forward1 = true; 286 } 287 } 288} 289
Arduino Code
arduino
1#include <Servo.h> 2 3Servo servox; 4Servo servoy; 5Servo servoz; 6int angle = 0; 7boolean forward1 = false ; 8String bluetoothRead, Str_x, Str_y, Str_p, Str_s; 9int x ; 10int y ; 11int x1 ; 12int y1 ; 13int points; 14int length; 15const int buzzer = 10; 16int s; 17int led1 = 3; // the pin that the LED1 is attached to 18int led2 = 2; // the pin that the LED2 is attached to 19int brightness = 0; // how bright the LED is 20int fadeAmount = 1; // how many points to fade the LED by 21const long interval = 1000; // interval at which to blink (milliseconds 22const long interval_short = 100; 23int ledState = LOW; // ledState used to set the LED 24unsigned long previousMillis = 0; // will store last time LED was updated 25 26 27 28#define ENA 11 29#define ENB 6 30#define IN1 5 31#define IN2 7 32#define IN3 8 33#define IN4 9 34#define DELAY 20 35 36 37void setup() { 38 Serial.begin(9600); 39 Serial.flush(); 40 pinMode(IN1,OUTPUT); 41 pinMode(IN2,OUTPUT); 42 pinMode(IN3,OUTPUT); 43 pinMode(IN4,OUTPUT); 44 pinMode(ENA,OUTPUT); 45 pinMode(ENB,OUTPUT); 46 pinMode(buzzer, OUTPUT); 47 pinMode(led1, OUTPUT); 48 pinMode(led2, OUTPUT); 49 50 servox.attach(13); 51 servoy.attach(12); 52 servoz.attach(8); 53 servox.write(90); 54 servoy.write(90); 55 servoz.write(90); 56 57} 58 59void loop() { 60 int i=0; 61 char commandbuffer[200]; 62 63 64 if(Serial.available()){ 65 66 delay(15); 67 68 while( Serial.available() && i< 199) { 69 commandbuffer[i++] = Serial.read(); 70 71 72 } 73 commandbuffer[i++]='\\0'; 74 bluetoothRead = (char*)commandbuffer; 75 length = bluetoothRead.length(); 76 Serial.println(bluetoothRead); 77 78 79 if(bluetoothRead.substring(0, 1).equals("x")){ 80 81 int i=1; 82 while(bluetoothRead.substring(i, i+1) != ("y")){ 83 i++; 84 } 85 86 Str_x = bluetoothRead.substring(1, i); 87 x = Str_x.toInt(); 88 89 90 91 Str_y = bluetoothRead.substring(i+1, length - 2); 92 y = Str_y.toInt(); 93 94 95 Str_p = bluetoothRead.substring(length - 2, length - 1); 96 points = Str_p.toInt(); 97 98 99 Str_s = bluetoothRead.substring(length - 1, length); 100 s = Str_s.toInt(); 101 102 i = 1; 103 104 Serial.println(x); 105 Serial.println(y); 106 Serial.println(points); 107 Serial.println(s); 108 Serial.println(length ); 109 110 111 112} 113} 114 servomove(); 115 if (x1 != 0 && y1 != 0) { 116 ledblink(); 117 if (s == 1) { 118 ledlaugh(); 119 servomouth(); 120 Serial.println("Smile!"); 121 } 122 } 123 else{ 124 ledState = LOW; 125 Serial.println("NotSmile"); 126 digitalWrite(led1, ledState); 127 digitalWrite(led2, ledState); 128 } 129 130} 131 132 133void servomove(){ 134 135 x1 = x/3; 136 y1 = y/4; 137 if (points == 4) { 138 139 servox.write(90-x1); 140 servoy.write(90-y1); 141 142 delay(10); 143 } 144 if (points == 3) 145 { 146 servox.write(90+x1); 147 servoy.write(90-y1); 148 delay(10); 149 } 150 151if (points == 7) 152 { 153 servox.write(90+x1); 154 servoy.write(y1+90); 155 delay(10); 156 } 157 158 if (points == 8) 159 { 160 servox.write(90-x1); 161 servoy.write(y1+90); 162 delay(10); 163 } 164 165 if (x1 == 0 && y1 == 0) 166 { 167 servox.write(90); 168 servoy.write(90); 169 delay(10); 170 } 171 172} 173void ledblink(){ 174 unsigned long currentMillis = millis(); 175 if (currentMillis - previousMillis >= interval) { 176 // save the last time you blinked the LED 177 previousMillis = currentMillis; 178 179 // if the LED is off turn it on and vice-versa: 180 if (ledState == LOW) { 181 ledState = HIGH; 182 } else { 183 ledState = LOW; 184 } 185 // set the LED with the ledState of the variable: 186 digitalWrite(led1, ledState); 187 digitalWrite(led2, ledState); 188 } 189} 190 191void ledlaugh(){ 192 unsigned long currentMillis = millis(); 193 if (currentMillis - previousMillis >= interval_short) { 194 // save the last time you blinked the LED 195 previousMillis = currentMillis; 196 197 // if the LED is off turn it on and vice-versa: 198 if (ledState == LOW) { 199 ledState = HIGH; 200 } else { 201 ledState = LOW; 202 } 203 // set the LED with the ledState of the variable: 204 digitalWrite(led1, ledState); 205 digitalWrite(led2, ledState); 206 } 207} 208 209void servomouth(){ 210 unsigned long currentMillis = millis(); 211 212 if (millis() - previousMillis >= DELAY) 213 {previousMillis += DELAY; 214 if (forward1) 215 { 216 servoz.write(-- angle); 217 if(angle == 100) 218 forward1 = false; 219 } 220 else 221 { 222 servoz.write(++ angle); 223 if (angle == 140) 224 forward1 = true; 225 } 226 } 227} 228
Downloadable files
Schematic
Schematic
Comments
Only logged in users can leave comments