My First Humanoid 22 DOF Robot
This is my first humanoid 22 DOF robot.
Components and supplies
1
Lipo battery 6250mAh 2S 7.4v 35C
1
17 DOF Biped Robot Mechanical Leg Claw Robot Servo Motor Bracket
17
Towerpro MG996R Servo
1
Servo Module
1
Arduino Mega 2560
Tools and machines
1
Soldering iron (generic)
Project description
Code
Servo testing
arduino
Servo GTesting,
1#include <SoftwareSerial.h>; 2 3SoftwareSerial usc(19,18); //defining usc32 and rx/tx pins 4 5#include <Vcc.h> 6#include <SPI.h> 7#include <Wire.h> 8#include <Adafruit_GFX.h> 9#include <Adafruit_SSD1306.h> 10#include <ChainableLED.h> 11//#include <p9813.h> 12#include <ADXL345.h> 13#include "ds3231.h" 14#include <inttypes.h> 15#include <lm75.h> 16 17//Battery Level Indicator Definitions 18const float VccMin = 0.0; // Minimum expected Vcc level, in Volts. 19const float VccMax = 5.0; // Maximum expected Vcc level, in Volts. 20const float VccCorrection = 1.0/1.0; // Measured Vcc by multimeter divided by reported Vcc 21Vcc vcc(VccCorrection); 22 23 24 25//Definitions for SSD1306 128x64 OLED Display 26 27#define OLED_RESET 7 28#define OLED_SA0 8 29 30//Definitions for P9813 RGB LED Driver 31 32#define no_of_LEDs 1 33 34#define data_pin 5 35#define clk_pin 6 36 37#define rgb_pwr 1 38 39//Definitions for Joystick 40 41#define KEY_UP A1 42#define KEY_DOWN A5 43#define KEY_LEFT A3 44#define KEY_RIGHT A2 45#define KEY_ENTER A4 46 47//Clock Stuffs 48 49#define CLOCK_CENTER_X 24 50#define CLOCK_CENTER_Y 40 51#define H_LENGTH 10 52#define M_LENGTH 14 53#define S_LENGTH 18 54 55#define buzzer 11 56 57#define rad_per_degree 0.0174556 58 59 60Adafruit_SSD1306 oled(OLED_RESET, OLED_SA0); 61ChainableLED RGB_LED(clk_pin, data_pin, rgb_pwr, no_of_LEDs); 62TempI2C_LM75 thermo = TempI2C_LM75(0x48, TempI2C_LM75::nine_bits); 63ADXL345 accelerometer; 64 65 66boolean tgl = false; 67boolean settings = false; 68 69signed char parameter = 0; 70unsigned char max_b = 127; 71 72float t_p = 0.0; 73float t_o = -255.0; 74 75 76struct ts T; 77 78 79void showBatteryLevel2() 80{ 81float p = vcc.Read_Perc(VccMin, VccMax); 82 83 oled.fillRect(2, 20, 50, 60, BLACK); 84 oled.setTextColor(WHITE); 85 oled.setCursor(2, 28); 86 oled.println("Battery"); 87 oled.setCursor(2, 40); 88 oled.print(p); //print the voltage to oled 89 oled.print(" %"); 90 if (p < 20) //set the voltage considered low battery here 91 { 92 oled.setCursor(25, 40); 93// oled.setTextColor(YELLOW); 94 oled.print("!!!"); 95 oled.setTextColor(WHITE); 96 } 97 98 99 100 Serial.print("VCC = "); 101 Serial.print(p); 102 Serial.println(" %"); 103 104} 105 106 107 108//read internal voltage 109long readVcc() { 110 long result; 111 // Read 1.1V reference against AVcc 112 ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); 113 delay(2); // Wait for Vref to settle 114 ADCSRA |= _BV(ADSC); // Convert 115 while (bit_is_set(ADCSRA, ADSC)); 116 result = ADCL; 117 result |= ADCH << 8; 118 result = 1126400L / result; // Back-calculate AVcc in mV 119 return result; 120} 121 122 void printVolts() 123{ 124 oled.fillRect(2, 20, 50, 60, BLACK); 125 oled.setTextColor(WHITE); 126 // int sensorValue = analogRead(A0); //read the A0 pin value 127 // float voltage = sensorValue * (5.00 / 1023.00) * 2; //convert the value to a true voltage. 128 double voltage = double( readVcc() ) / 1000; 129 //float voltage = readVcc(); 130 oled.setCursor(2, 28); 131 oled.println("Battery"); 132 oled.setCursor(2, 40); 133 oled.print("="); 134 oled.print(voltage); //print the voltage to oled 135 oled.print("v"); 136 if (voltage < 6.50) //set the voltage considered low battery here 137 { 138 // digitalWrite(led_pin, HIGH); 139 } 140} 141 142void draw_background() 143{ 144 unsigned char i = 0; 145 146/* oled.drawRect(2, 18, 44, 44, WHITE); 147 oled.drawLine(24, 19, 24, 20, WHITE); 148 oled.drawLine(24, 60, 24, 61, WHITE); 149 oled.drawLine(3, 40, 4, 40, WHITE); 150 oled.drawLine(44, 40, 45, 40, WHITE); 151 152 oled.fillCircle(106, 58, 4, WHITE); 153 oled.drawFastVLine(104, 18, 37, WHITE); 154 oled.drawFastVLine(108, 18, 37, WHITE); 155 oled.drawFastHLine(105, 17, 3, WHITE); 156*/ 157 for(i = 18; i <= 53; i += 5) 158 { 159 oled.drawFastHLine(111, i, 3, WHITE); 160 } 161 162 oled.setTextSize(1); 163 oled.setTextColor(WHITE); 164 for(i = 0; i < 40; i += 10) 165 { 166 oled.setCursor(116, (48 - i)); 167 oled.println(i + 10); 168 } 169 170 oled.setCursor(12, 0); 171 oled.println("Time"); 172 oled.setCursor(59, 0); 173 oled.println("Date"); 174 oled.setCursor(96, 0); 175 oled.println("Temp."); 176 177 oled.display(); 178} 179 180 181float get_T_avg() 182{ 183 unsigned char samples = 20; 184 float avg = 0.0; 185 186 while(samples > 0) 187 { 188 avg += thermo.getTemp(); 189 delayMicroseconds(100); 190 samples--; 191 } 192 193 avg /= 20.0; 194 return avg; 195} 196 197 198void show_temperature() 199{ 200 unsigned char bar_length = 0; 201 202 t_p = get_T_avg(); 203 204 if(t_p != t_o) 205 { 206 oled.fillRect(98, 8, 29, 8, BLACK); 207 oled.setTextColor(WHITE); 208 oled.setCursor(98, 9); 209 oled.println(t_p); 210 211 t_p = constrain(t_p, 0, 40.0); 212 bar_length = map(t_p, 0.0, 40.0, 0, 36); 213 oled.drawLine(106, 53, 106, 18, BLACK); 214 oled.drawLine(106, 53, 106, (53 - bar_length), WHITE); 215 t_o = t_p; 216 } 217} 218 219 220void show_time() 221{ 222 static unsigned long previous_time; 223 static unsigned long present_time; 224 225 oled.fillRect(0, 8, 50, 8, BLACK); 226 227 if(T.hour < 10) 228 { 229 oled.setCursor(0, 9); 230 oled.println("0"); 231 oled.setCursor(6, 9); 232 oled.println(T.hour); 233 } 234 else 235 { 236 oled.setCursor(0, 9); 237 oled.println(T.hour); 238 } 239 240 if(T.min < 10) 241 { 242 oled.setCursor(18, 9); 243 oled.println("0"); 244 oled.setCursor(24, 9); 245 oled.println(T.min); 246 } 247 else 248 { 249 oled.setCursor(18, 9); 250 oled.println(T.min); 251 } 252 253 if(T.sec < 10) 254 { 255 oled.setCursor(36, 9); 256 oled.println("0"); 257 oled.setCursor(42, 9); 258 oled.println(T.sec); 259 } 260 else 261 { 262 oled.setCursor(36, 9); 263 oled.println(T.sec); 264 } 265 266 present_time = millis(); 267 if((present_time - previous_time) > 999) 268 { 269 tgl ^= 1; 270 previous_time = present_time; 271 } 272 273 if(tgl) 274 { 275 oled.setCursor(12, 9); 276 oled.println(":"); 277 oled.setCursor(30, 9); 278 oled.println(":"); 279 } 280 else 281 { 282 oled.setCursor(12, 9); 283 oled.println(" "); 284 oled.setCursor(30, 9); 285 oled.println(" "); 286 } 287 288 // display_analog_clock(T.hour, T.min, T.sec); 289} 290 291 292void show_date() 293{ 294 oled.fillRect(48, 16, 54, 37, BLACK); 295 296 if(T.mday < 10) 297 { 298 oled.setCursor(60, 18); 299 oled.println("0"); 300 oled.setCursor(66, 18); 301 oled.println(T.mday); 302 } 303 else 304 { 305 oled.setCursor(60, 18); 306 oled.println(T.mday); 307 } 308 309 oled.setCursor(72, 18); 310 oled.println("/"); 311 312 if(T.mon < 10) 313 { 314 oled.setCursor(78, 18); 315 oled.println("0"); 316 oled.setCursor(84, 18); 317 oled.println(T.mon); 318 } 319 else 320 { 321 oled.setCursor(78, 18); 322 oled.println(T.mon); 323 } 324 325 oled.setCursor(62, 32); 326 oled.println(T.year); 327 328 oled.setCursor(49, 46); 329 switch(T.wday) 330 { 331 case 1: 332 { 333 oled.println(" Monday "); 334 break; 335 } 336 case 2: 337 { 338 oled.println(" Tuesday "); 339 break; 340 } 341 case 3: 342 { 343 oled.println("Wednesday"); 344 break; 345 } 346 case 4: 347 { 348 oled.println("Thursday "); 349 break; 350 } 351 case 5: 352 { 353 oled.println(" Friday "); 354 break; 355 } 356 case 6: 357 { 358 oled.println("Saturday "); 359 break; 360 } 361 default: 362 { 363 oled.println(" Sunday "); 364 break; 365 } 366 } 367} 368 369 370void set_time_and_date() 371{ 372 if(digitalRead(KEY_ENTER) == LOW) 373 { 374 digitalWrite(buzzer, HIGH); 375 delay(20); 376 digitalWrite(buzzer, LOW); 377 while(digitalRead(KEY_ENTER) == LOW); 378 settings = true; 379 parameter = 1; 380 } 381 382 if(settings == true) 383 { 384 switch(parameter) 385 { 386 case 1: 387 { 388 RGB_LED.setColorRGB(0, 255, 0, 0); 389 T.hour = inc_dec(T.hour, 23, 0); 390 break; 391 } 392 case 2: 393 { 394 RGB_LED.setColorRGB(0, 0, 255, 0); 395 T.min = inc_dec(T.min, 59, 0); 396 break; 397 } 398 case 3: 399 { 400 RGB_LED.setColorRGB(0, 0, 0, 255); 401 T.sec = inc_dec(T.sec, 59, 0); 402 break; 403 } 404 case 4: 405 { 406 RGB_LED.setColorRGB(0, 127, 127, 0); 407 T.mday = inc_dec(T.mday, 31, 1); 408 break; 409 } 410 case 5: 411 { 412 RGB_LED.setColorRGB(0, 0, 127, 127); 413 T.mon = inc_dec(T.mon, 12, 1); 414 break; 415 } 416 case 6: 417 { 418 RGB_LED.setColorRGB(0, 127, 0, 127); 419 T.year = inc_dec(T.year, 2100, 1980); 420 break; 421 } 422 case 7: 423 { 424 RGB_LED.setColorRGB(0, 127, 127, 127); 425 T.wday = inc_dec(T.wday, 6, 0); 426 break; 427 } 428 default: 429 { 430 DS3231_set(T); 431 settings = false; 432 break; 433 } 434 } 435 } 436} 437 438 439void clock_stuffs() 440{ 441 if(settings == false) 442 { 443 DS3231_get(&T); 444 } 445 show_time(); 446 show_date(); 447 set_time_and_date(); 448} 449 450 451void display_analog_clock(signed int h, signed int m, signed int s) 452{ 453 float midHours = 0; 454 static signed char hx; 455 static signed char hy; 456 static signed char mx; 457 static signed char my; 458 static signed char sx; 459 static signed char sy; 460 461 h -= 3; 462 m -= 15; 463 s -= 15; 464 465 if(h <= 0) 466 { 467 h += 12; 468 } 469 if(m < 0) 470 { 471 m += 60; 472 } 473 if(s < 0) 474 { 475 s += 60; 476 } 477 478 oled.drawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, (CLOCK_CENTER_X + sx), (CLOCK_CENTER_Y + sy), BLACK); 479 oled.drawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, (CLOCK_CENTER_X + mx), (CLOCK_CENTER_Y + my), BLACK); 480 oled.drawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, (CLOCK_CENTER_X + hx), (CLOCK_CENTER_Y + hy), BLACK); 481 482 s *= 6; 483 sx = (S_LENGTH * cos(s * rad_per_degree)); 484 sy = (S_LENGTH * sin(s * rad_per_degree)); 485 oled.drawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, (CLOCK_CENTER_X + sx), (CLOCK_CENTER_Y + sy), WHITE); 486 487 m *= 6; 488 mx = (M_LENGTH * cos(m * rad_per_degree)); 489 my = (M_LENGTH * sin(m * rad_per_degree)); 490 oled.drawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, (CLOCK_CENTER_X + mx), (CLOCK_CENTER_Y + my), WHITE); 491 492 midHours = (T.min / 12); 493 h *= 5; 494 h += midHours; 495 h *= 6; 496 hx = (H_LENGTH * cos(h * rad_per_degree)); 497 hy = (H_LENGTH * sin(h * rad_per_degree)); 498 oled.drawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, (CLOCK_CENTER_X + hx), (CLOCK_CENTER_Y + hy), WHITE); 499} 500 501 502void accelerometer_and_RGB_LED() 503{ 504 unsigned char r = 0x00; 505 unsigned char g = 0x00; 506 unsigned char b = 0x00; 507 508 Vector norm = accelerometer.readNormalize(); 509 r = map(norm.XAxis, -11, 11, 0, max_b); 510 g = map(norm.YAxis, -11, 11, 0, max_b); 511 b = map(norm.ZAxis, -11, 11, 0, max_b); 512 513 if(settings == false) 514 { 515 RGB_LED.setColorRGB(0, r, g, b); 516 } 517} 518 519 520signed int inc_dec(signed int value, signed int max_value, signed int min_value) 521{ 522 if(digitalRead(KEY_UP) == LOW) 523 { 524 digitalWrite(buzzer, HIGH); 525 delay(20); 526 digitalWrite(buzzer, LOW); 527 value++; 528 } 529 if(value > max_value) 530 { 531 value = min_value; 532 } 533 534 if(digitalRead(KEY_DOWN) == LOW) 535 { 536 digitalWrite(buzzer, HIGH); 537 delay(20); 538 digitalWrite(buzzer, LOW); 539 value--; 540 } 541 if(value < min_value) 542 { 543 value = max_value; 544 } 545 546 if(digitalRead(KEY_RIGHT) == LOW) 547 { 548 digitalWrite(buzzer, HIGH); 549 delay(20); 550 digitalWrite(buzzer, LOW); 551 while(digitalRead(KEY_RIGHT) == LOW); 552 parameter++; 553 } 554 555 if(digitalRead(KEY_LEFT) == LOW) 556 { 557 digitalWrite(buzzer, HIGH); 558 delay(20); 559 digitalWrite(buzzer, LOW); 560 while(digitalRead(KEY_LEFT) == LOW); 561 parameter--; 562 } 563 564 if((parameter > 7) || (parameter < 1)) 565 { 566 parameter = 0; 567 } 568 569 return value; 570} 571 572 573void set_RGB_LED_max_brightness() 574{ 575 unsigned int avg = 0; 576 unsigned char samples = 16; 577 578 while(samples > 0) 579 { 580 avg += analogRead(A0); 581 delayMicroseconds(10); 582 samples--; 583 }; 584 max_b = (avg >> 6); 585} 586 587 588void transmit_data() 589{ 590 if(tgl) 591 { 592 Serial.print(T.mday); 593 Serial.print("/"); 594 Serial.print(T.mon); 595 Serial.print("."); 596 Serial.print(T.year); 597 Serial.print(" "); 598 Serial.print(T.hour); 599 Serial.print("."); 600 Serial.print(T.min); 601 Serial.print("."); 602 Serial.print(T.sec); 603 Serial.print(" "); 604 Serial.print("T/'C: "); 605 Serial.println(t_p); 606 } 607} 608 609 610void setup() 611{ 612 unsigned char i = 0; 613 614 Serial.begin(9600); 615 Serial.flush(); 616 617 oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); 618 oled.clearDisplay(); 619 620 RGB_LED.pwr_set(PWR_ENABLE); 621 pinMode(buzzer, OUTPUT); 622 digitalWrite(buzzer, LOW); 623 624 DS3231_init(DS3231_INTCN); 625 626 for(i = A1; i <= A5; i++) 627 { 628 pinMode(i, INPUT_PULLUP); 629 } 630 631 draw_background(); 632 633 usc.begin(9600); //usc32 initialization 634 usc.println("#12P1800T2000"); // move servo 12 at position 510 in 2 secs 635 delay(2000); 636} 637 638void updateLcd() 639{ 640 set_RGB_LED_max_brightness(); 641 show_temperature(); 642 printVolts(); 643 showBatteryLevel2(); 644 clock_stuffs(); 645 accelerometer_and_RGB_LED(); 646 transmit_data(); 647 oled.display(); 648} 649 650void loop() 651{ 652 updateLcd (); usc.println("#12P1800T2000"); // move servo 12 at position 510 in 2 secs 653 delay(1000); 654 updateLcd(); 655 usc.println("#12P1000T1000"); // move servo 12 at position 510 in 2 secs 656 delay(2000); 657 updateLcd(); 658 usc.println("#12P1800T1000"); // move servo 12 at position 2400 in 2 secs 659 delay(1000); 660 661 662}
Servo testing
arduino
Servo GTesting,
1#include <SoftwareSerial.h>; 2 3SoftwareSerial usc(19,18); //defining usc32 and rx/tx pins 4 5#include <Vcc.h> 6#include <SPI.h> 7#include <Wire.h> 8#include <Adafruit_GFX.h> 9#include <Adafruit_SSD1306.h> 10#include <ChainableLED.h> 11//#include <p9813.h> 12#include <ADXL345.h> 13#include "ds3231.h" 14#include <inttypes.h> 15#include <lm75.h> 16 17//Battery Level Indicator Definitions 18const float VccMin = 0.0; // Minimum expected Vcc level, in Volts. 19const float VccMax = 5.0; // Maximum expected Vcc level, in Volts. 20const float VccCorrection = 1.0/1.0; // Measured Vcc by multimeter divided by reported Vcc 21Vcc vcc(VccCorrection); 22 23 24 25//Definitions for SSD1306 128x64 OLED Display 26 27#define OLED_RESET 7 28#define OLED_SA0 8 29 30//Definitions for P9813 RGB LED Driver 31 32#define no_of_LEDs 1 33 34#define data_pin 5 35#define clk_pin 6 36 37#define rgb_pwr 1 38 39//Definitions for Joystick 40 41#define KEY_UP A1 42#define KEY_DOWN A5 43#define KEY_LEFT A3 44#define KEY_RIGHT A2 45#define KEY_ENTER A4 46 47//Clock Stuffs 48 49#define CLOCK_CENTER_X 24 50#define CLOCK_CENTER_Y 40 51#define H_LENGTH 10 52#define M_LENGTH 14 53#define S_LENGTH 18 54 55#define buzzer 11 56 57#define rad_per_degree 0.0174556 58 59 60Adafruit_SSD1306 oled(OLED_RESET, OLED_SA0); 61ChainableLED RGB_LED(clk_pin, data_pin, rgb_pwr, no_of_LEDs); 62TempI2C_LM75 thermo = TempI2C_LM75(0x48, TempI2C_LM75::nine_bits); 63ADXL345 accelerometer; 64 65 66boolean tgl = false; 67boolean settings = false; 68 69signed char parameter = 0; 70unsigned char max_b = 127; 71 72float t_p = 0.0; 73float t_o = -255.0; 74 75 76struct ts T; 77 78 79void showBatteryLevel2() 80{ 81float p = vcc.Read_Perc(VccMin, VccMax); 82 83 oled.fillRect(2, 20, 50, 60, BLACK); 84 oled.setTextColor(WHITE); 85 oled.setCursor(2, 28); 86 oled.println("Battery"); 87 oled.setCursor(2, 40); 88 oled.print(p); //print the voltage to oled 89 oled.print(" %"); 90 if (p < 20) //set the voltage considered low battery here 91 { 92 oled.setCursor(25, 40); 93// oled.setTextColor(YELLOW); 94 oled.print("!!!"); 95 oled.setTextColor(WHITE); 96 } 97 98 99 100 Serial.print("VCC = "); 101 Serial.print(p); 102 Serial.println(" %"); 103 104} 105 106 107 108//read internal voltage 109long readVcc() { 110 long result; 111 // Read 1.1V reference against AVcc 112 ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); 113 delay(2); // Wait for Vref to settle 114 ADCSRA |= _BV(ADSC); // Convert 115 while (bit_is_set(ADCSRA, ADSC)); 116 result = ADCL; 117 result |= ADCH << 8; 118 result = 1126400L / result; // Back-calculate AVcc in mV 119 return result; 120} 121 122 void printVolts() 123{ 124 oled.fillRect(2, 20, 50, 60, BLACK); 125 oled.setTextColor(WHITE); 126 // int sensorValue = analogRead(A0); //read the A0 pin value 127 // float voltage = sensorValue * (5.00 / 1023.00) * 2; //convert the value to a true voltage. 128 double voltage = double( readVcc() ) / 1000; 129 //float voltage = readVcc(); 130 oled.setCursor(2, 28); 131 oled.println("Battery"); 132 oled.setCursor(2, 40); 133 oled.print("="); 134 oled.print(voltage); //print the voltage to oled 135 oled.print("v"); 136 if (voltage < 6.50) //set the voltage considered low battery here 137 { 138 // digitalWrite(led_pin, HIGH); 139 } 140} 141 142void draw_background() 143{ 144 unsigned char i = 0; 145 146/* oled.drawRect(2, 18, 44, 44, WHITE); 147 oled.drawLine(24, 19, 24, 20, WHITE); 148 oled.drawLine(24, 60, 24, 61, WHITE); 149 oled.drawLine(3, 40, 4, 40, WHITE); 150 oled.drawLine(44, 40, 45, 40, WHITE); 151 152 oled.fillCircle(106, 58, 4, WHITE); 153 oled.drawFastVLine(104, 18, 37, WHITE); 154 oled.drawFastVLine(108, 18, 37, WHITE); 155 oled.drawFastHLine(105, 17, 3, WHITE); 156*/ 157 for(i = 18; i <= 53; i += 5) 158 { 159 oled.drawFastHLine(111, i, 3, WHITE); 160 } 161 162 oled.setTextSize(1); 163 oled.setTextColor(WHITE); 164 for(i = 0; i < 40; i += 10) 165 { 166 oled.setCursor(116, (48 - i)); 167 oled.println(i + 10); 168 } 169 170 oled.setCursor(12, 0); 171 oled.println("Time"); 172 oled.setCursor(59, 0); 173 oled.println("Date"); 174 oled.setCursor(96, 0); 175 oled.println("Temp."); 176 177 oled.display(); 178} 179 180 181float get_T_avg() 182{ 183 unsigned char samples = 20; 184 float avg = 0.0; 185 186 while(samples > 0) 187 { 188 avg += thermo.getTemp(); 189 delayMicroseconds(100); 190 samples--; 191 } 192 193 avg /= 20.0; 194 return avg; 195} 196 197 198void show_temperature() 199{ 200 unsigned char bar_length = 0; 201 202 t_p = get_T_avg(); 203 204 if(t_p != t_o) 205 { 206 oled.fillRect(98, 8, 29, 8, BLACK); 207 oled.setTextColor(WHITE); 208 oled.setCursor(98, 9); 209 oled.println(t_p); 210 211 t_p = constrain(t_p, 0, 40.0); 212 bar_length = map(t_p, 0.0, 40.0, 0, 36); 213 oled.drawLine(106, 53, 106, 18, BLACK); 214 oled.drawLine(106, 53, 106, (53 - bar_length), WHITE); 215 t_o = t_p; 216 } 217} 218 219 220void show_time() 221{ 222 static unsigned long previous_time; 223 static unsigned long present_time; 224 225 oled.fillRect(0, 8, 50, 8, BLACK); 226 227 if(T.hour < 10) 228 { 229 oled.setCursor(0, 9); 230 oled.println("0"); 231 oled.setCursor(6, 9); 232 oled.println(T.hour); 233 } 234 else 235 { 236 oled.setCursor(0, 9); 237 oled.println(T.hour); 238 } 239 240 if(T.min < 10) 241 { 242 oled.setCursor(18, 9); 243 oled.println("0"); 244 oled.setCursor(24, 9); 245 oled.println(T.min); 246 } 247 else 248 { 249 oled.setCursor(18, 9); 250 oled.println(T.min); 251 } 252 253 if(T.sec < 10) 254 { 255 oled.setCursor(36, 9); 256 oled.println("0"); 257 oled.setCursor(42, 9); 258 oled.println(T.sec); 259 } 260 else 261 { 262 oled.setCursor(36, 9); 263 oled.println(T.sec); 264 } 265 266 present_time = millis(); 267 if((present_time - previous_time) > 999) 268 { 269 tgl ^= 1; 270 previous_time = present_time; 271 } 272 273 if(tgl) 274 { 275 oled.setCursor(12, 9); 276 oled.println(":"); 277 oled.setCursor(30, 9); 278 oled.println(":"); 279 } 280 else 281 { 282 oled.setCursor(12, 9); 283 oled.println(" "); 284 oled.setCursor(30, 9); 285 oled.println(" "); 286 } 287 288 // display_analog_clock(T.hour, T.min, T.sec); 289} 290 291 292void show_date() 293{ 294 oled.fillRect(48, 16, 54, 37, BLACK); 295 296 if(T.mday < 10) 297 { 298 oled.setCursor(60, 18); 299 oled.println("0"); 300 oled.setCursor(66, 18); 301 oled.println(T.mday); 302 } 303 else 304 { 305 oled.setCursor(60, 18); 306 oled.println(T.mday); 307 } 308 309 oled.setCursor(72, 18); 310 oled.println("/"); 311 312 if(T.mon < 10) 313 { 314 oled.setCursor(78, 18); 315 oled.println("0"); 316 oled.setCursor(84, 18); 317 oled.println(T.mon); 318 } 319 else 320 { 321 oled.setCursor(78, 18); 322 oled.println(T.mon); 323 } 324 325 oled.setCursor(62, 32); 326 oled.println(T.year); 327 328 oled.setCursor(49, 46); 329 switch(T.wday) 330 { 331 case 1: 332 { 333 oled.println(" Monday "); 334 break; 335 } 336 case 2: 337 { 338 oled.println(" Tuesday "); 339 break; 340 } 341 case 3: 342 { 343 oled.println("Wednesday"); 344 break; 345 } 346 case 4: 347 { 348 oled.println("Thursday "); 349 break; 350 } 351 case 5: 352 { 353 oled.println(" Friday "); 354 break; 355 } 356 case 6: 357 { 358 oled.println("Saturday "); 359 break; 360 } 361 default: 362 { 363 oled.println(" Sunday "); 364 break; 365 } 366 } 367} 368 369 370void set_time_and_date() 371{ 372 if(digitalRead(KEY_ENTER) == LOW) 373 { 374 digitalWrite(buzzer, HIGH); 375 delay(20); 376 digitalWrite(buzzer, LOW); 377 while(digitalRead(KEY_ENTER) == LOW); 378 settings = true; 379 parameter = 1; 380 } 381 382 if(settings == true) 383 { 384 switch(parameter) 385 { 386 case 1: 387 { 388 RGB_LED.setColorRGB(0, 255, 0, 0); 389 T.hour = inc_dec(T.hour, 23, 0); 390 break; 391 } 392 case 2: 393 { 394 RGB_LED.setColorRGB(0, 0, 255, 0); 395 T.min = inc_dec(T.min, 59, 0); 396 break; 397 } 398 case 3: 399 { 400 RGB_LED.setColorRGB(0, 0, 0, 255); 401 T.sec = inc_dec(T.sec, 59, 0); 402 break; 403 } 404 case 4: 405 { 406 RGB_LED.setColorRGB(0, 127, 127, 0); 407 T.mday = inc_dec(T.mday, 31, 1); 408 break; 409 } 410 case 5: 411 { 412 RGB_LED.setColorRGB(0, 0, 127, 127); 413 T.mon = inc_dec(T.mon, 12, 1); 414 break; 415 } 416 case 6: 417 { 418 RGB_LED.setColorRGB(0, 127, 0, 127); 419 T.year = inc_dec(T.year, 2100, 1980); 420 break; 421 } 422 case 7: 423 { 424 RGB_LED.setColorRGB(0, 127, 127, 127); 425 T.wday = inc_dec(T.wday, 6, 0); 426 break; 427 } 428 default: 429 { 430 DS3231_set(T); 431 settings = false; 432 break; 433 } 434 } 435 } 436} 437 438 439void clock_stuffs() 440{ 441 if(settings == false) 442 { 443 DS3231_get(&T); 444 } 445 show_time(); 446 show_date(); 447 set_time_and_date(); 448} 449 450 451void display_analog_clock(signed int h, signed int m, signed int s) 452{ 453 float midHours = 0; 454 static signed char hx; 455 static signed char hy; 456 static signed char mx; 457 static signed char my; 458 static signed char sx; 459 static signed char sy; 460 461 h -= 3; 462 m -= 15; 463 s -= 15; 464 465 if(h <= 0) 466 { 467 h += 12; 468 } 469 if(m < 0) 470 { 471 m += 60; 472 } 473 if(s < 0) 474 { 475 s += 60; 476 } 477 478 oled.drawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, (CLOCK_CENTER_X + sx), (CLOCK_CENTER_Y + sy), BLACK); 479 oled.drawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, (CLOCK_CENTER_X + mx), (CLOCK_CENTER_Y + my), BLACK); 480 oled.drawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, (CLOCK_CENTER_X + hx), (CLOCK_CENTER_Y + hy), BLACK); 481 482 s *= 6; 483 sx = (S_LENGTH * cos(s * rad_per_degree)); 484 sy = (S_LENGTH * sin(s * rad_per_degree)); 485 oled.drawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, (CLOCK_CENTER_X + sx), (CLOCK_CENTER_Y + sy), WHITE); 486 487 m *= 6; 488 mx = (M_LENGTH * cos(m * rad_per_degree)); 489 my = (M_LENGTH * sin(m * rad_per_degree)); 490 oled.drawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, (CLOCK_CENTER_X + mx), (CLOCK_CENTER_Y + my), WHITE); 491 492 midHours = (T.min / 12); 493 h *= 5; 494 h += midHours; 495 h *= 6; 496 hx = (H_LENGTH * cos(h * rad_per_degree)); 497 hy = (H_LENGTH * sin(h * rad_per_degree)); 498 oled.drawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, (CLOCK_CENTER_X + hx), (CLOCK_CENTER_Y + hy), WHITE); 499} 500 501 502void accelerometer_and_RGB_LED() 503{ 504 unsigned char r = 0x00; 505 unsigned char g = 0x00; 506 unsigned char b = 0x00; 507 508 Vector norm = accelerometer.readNormalize(); 509 r = map(norm.XAxis, -11, 11, 0, max_b); 510 g = map(norm.YAxis, -11, 11, 0, max_b); 511 b = map(norm.ZAxis, -11, 11, 0, max_b); 512 513 if(settings == false) 514 { 515 RGB_LED.setColorRGB(0, r, g, b); 516 } 517} 518 519 520signed int inc_dec(signed int value, signed int max_value, signed int min_value) 521{ 522 if(digitalRead(KEY_UP) == LOW) 523 { 524 digitalWrite(buzzer, HIGH); 525 delay(20); 526 digitalWrite(buzzer, LOW); 527 value++; 528 } 529 if(value > max_value) 530 { 531 value = min_value; 532 } 533 534 if(digitalRead(KEY_DOWN) == LOW) 535 { 536 digitalWrite(buzzer, HIGH); 537 delay(20); 538 digitalWrite(buzzer, LOW); 539 value--; 540 } 541 if(value < min_value) 542 { 543 value = max_value; 544 } 545 546 if(digitalRead(KEY_RIGHT) == LOW) 547 { 548 digitalWrite(buzzer, HIGH); 549 delay(20); 550 digitalWrite(buzzer, LOW); 551 while(digitalRead(KEY_RIGHT) == LOW); 552 parameter++; 553 } 554 555 if(digitalRead(KEY_LEFT) == LOW) 556 { 557 digitalWrite(buzzer, HIGH); 558 delay(20); 559 digitalWrite(buzzer, LOW); 560 while(digitalRead(KEY_LEFT) == LOW); 561 parameter--; 562 } 563 564 if((parameter > 7) || (parameter < 1)) 565 { 566 parameter = 0; 567 } 568 569 return value; 570} 571 572 573void set_RGB_LED_max_brightness() 574{ 575 unsigned int avg = 0; 576 unsigned char samples = 16; 577 578 while(samples > 0) 579 { 580 avg += analogRead(A0); 581 delayMicroseconds(10); 582 samples--; 583 }; 584 max_b = (avg >> 6); 585} 586 587 588void transmit_data() 589{ 590 if(tgl) 591 { 592 Serial.print(T.mday); 593 Serial.print("/"); 594 Serial.print(T.mon); 595 Serial.print("."); 596 Serial.print(T.year); 597 Serial.print(" "); 598 Serial.print(T.hour); 599 Serial.print("."); 600 Serial.print(T.min); 601 Serial.print("."); 602 Serial.print(T.sec); 603 Serial.print(" "); 604 Serial.print("T/'C: "); 605 Serial.println(t_p); 606 } 607} 608 609 610void setup() 611{ 612 unsigned char i = 0; 613 614 Serial.begin(9600); 615 Serial.flush(); 616 617 oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); 618 oled.clearDisplay(); 619 620 RGB_LED.pwr_set(PWR_ENABLE); 621 pinMode(buzzer, OUTPUT); 622 digitalWrite(buzzer, LOW); 623 624 DS3231_init(DS3231_INTCN); 625 626 for(i = A1; i <= A5; i++) 627 { 628 pinMode(i, INPUT_PULLUP); 629 } 630 631 draw_background(); 632 633 usc.begin(9600); //usc32 initialization 634 usc.println("#12P1800T2000"); // move servo 12 at position 510 in 2 secs 635 delay(2000); 636} 637 638void updateLcd() 639{ 640 set_RGB_LED_max_brightness(); 641 show_temperature(); 642 printVolts(); 643 showBatteryLevel2(); 644 clock_stuffs(); 645 accelerometer_and_RGB_LED(); 646 transmit_data(); 647 oled.display(); 648} 649 650void loop() 651{ 652 updateLcd (); usc.println("#12P1800T2000"); // move servo 12 at position 510 in 2 secs 653 delay(1000); 654 updateLcd(); 655 usc.println("#12P1000T1000"); // move servo 12 at position 510 in 2 secs 656 delay(2000); 657 updateLcd(); 658 usc.println("#12P1800T1000"); // move servo 12 at position 2400 in 2 secs 659 delay(1000); 660 661 662}
Downloadable files
robo zoom 2
robo zoom 1
robo zoom 2

testing servos
testing servos
testing servos
robo back
robo back
robo back

robo zoom 1
robo zoom 1

testing servos
testing servos
testing servos
robo back
robo back
robo back

robo zoom 2
robo zoom 1
robo zoom 2

robo zoom 1
robo zoom 1

Close photo 1
Close photo 1
Close photo 1

Comments
Only logged in users can leave comments