Components and supplies
74HC595
LED (generic)
Arduino Nano R3
RTC Real Time Clock
4xAA battery holder
Rotary Encoder with Push-Button
Main Power Switch
Rotary potentiometer (generic)
1602A LCD Display
Tools and machines
Drill / Driver, Cordless
Solder Wire, Lead Free
Soldering iron (generic)
Project description
Code
Biorithm_with_clock.ino
arduino
It’s the Arduino code for the Biorhythm Clock
1// Sketch biorhythm with clock 2// Place your default birth date in the sketch 3 4 5#include <DS3232RTC.h> 6#include <LiquidCrystal.h> 7#include <OneButton.h> 8#include <RotaryEncoder.h> 9 10// ----- Rotary settings here ---- 11#define ROTARYSTEPS 1 12#define ROTARYMIN 0 13#define ROTARYMAX 10 14int lastPos = 0; 15int exitFlag = 0; 16// Setup a RoraryEncoder for pins A2 and A3: 17RotaryEncoder encoder(A2, A3); 18// Last known rotary position. 19 20DS3232RTC myRTC; 21const int rs = 8, en =7, d4 = 5, d5 = 4, d6 = 3, d7 = 2; 22LiquidCrystal lcd(rs, en, d4, d5, d6, d7); 23// Setup a new OneButton on pin A1. 24OneButton button1(A1, true); 25 26//Pin connected to ST_CP of 74HC595 27int latchPin = 9; 28//Pin connected to SH_CP of 74HC595 29int clockPin = 12; 30////Pin connected to DS of 74HC595 31int dataPin = 11; 32 33// Place your default birth date below 34int dayBirth = 8; 35int monthBirth = 4; 36int yearBirth = 1968; 37// NOT TO BE CHANGED 38int anno = 1950; 39int mese = 1; 40int giorno = 1; 41 42void setup() { 43 Serial.begin(115200); 44 45 encoder.setPosition(0 / ROTARYSTEPS); // start with the value of 0. 46 47 // link the button 1 functions. 48 button1.attachClick(click1); 49 button1.attachDoubleClick(doubleclick1); 50 button1.attachLongPressStart(longPressStart1); 51 button1.attachLongPressStop(longPressStop1); 52 button1.attachDuringLongPress(longPress1); 53 delay(200); 54 55 56 //set pins to output so you can control the shift register 57 pinMode(latchPin, OUTPUT); 58 pinMode(clockPin, OUTPUT); 59 pinMode(dataPin, OUTPUT); 60 pinMode(6, OUTPUT); 61 pinMode(10, OUTPUT); 62 63 64 lcd.begin(16, 2); 65 66 setSyncProvider(RTC.get); // the function to get the time from the RTC 67 if(timeStatus() != timeSet) 68 Serial.println("Unable to sync with the RTC"); 69 else 70 Serial.println("RTC has set the system time"); 71 72 myRTC.begin(); 73 74 75} 76 77void loop() { 78 79 button1.tick(); 80 int daysPassed; 81 82 display(); 83// delay(5000); 84 char buf[40]; 85 time_t t = myRTC.get(); 86 87 daysPassed = dateDiff(yearBirth, monthBirth, dayBirth, year(t), month(t), day(t)); 88 Serial.print("DAYS PASSED: "); 89 Serial.println(daysPassed); 90 ledBioritmo(daysPassed); 91 92} 93 94// Display Ora a data 95void display() 96{ 97 char buf[40]; 98 time_t t = myRTC.get(); 99 100 sprintf(buf, "%.2d:%.2d %.2d/%.2d/%d ", 101 hour(t), minute(t), day(t), month(t), year(t)); 102 Serial.println(buf); 103 lcd.setCursor(0, 0); 104 lcd.print(buf); 105} 106 107 108 109 110void ledBioritmo(int daysPassed) 111{ 112int fisico; 113int emotivo; 114int intellettuale; 115 116 117 fisico = bioFisico(daysPassed); 118 Serial.print("Physical: "); 119 Serial.println(fisico); 120 lcd.setCursor(0, 1); 121 lcd.print(" PHYSICAL "); 122 123if (fisico < 0x1ff) 124 { 125 digitalWrite(10, LOW); 126 digitalWrite(6, LOW); 127 digitalWrite(latchPin, LOW); 128 // shift out the bits: 129 shiftOut(dataPin, clockPin, MSBFIRST, fisico); 130 //take the latch pin high so the LEDs will light up: 131 digitalWrite(latchPin, HIGH); 132 delay (3000); 133 } 134 else 135 if (fisico == 0x1FF) 136 { 137 digitalWrite(10, LOW); 138 digitalWrite(6, LOW); 139 digitalWrite(latchPin, LOW); 140 // shift out the bits: 141 shiftOut(dataPin, clockPin, MSBFIRST, 0xFF); 142 //take the latch pin high so the LEDs will light up: 143 digitalWrite(latchPin, HIGH); 144 digitalWrite(10, HIGH); 145 delay (3000); 146 } 147 else 148 if (fisico == 0x2FF) 149 { 150 digitalWrite(10, LOW); 151 digitalWrite(6, LOW); 152 digitalWrite(latchPin, LOW); 153 // shift out the bits: 154 shiftOut(dataPin, clockPin, MSBFIRST, 0xFF); 155 //take the latch pin high so the LEDs will light up: 156 digitalWrite(latchPin, HIGH); 157 digitalWrite(10, HIGH); 158 digitalWrite(6, HIGH); 159 delay (3000); 160 } 161 162 emotivo = bioEmotivo(daysPassed); 163 Serial.print("Emotional: "); 164 Serial.println(emotivo); 165 lcd.setCursor(0, 1); 166 lcd.print(" EMOTIONAL "); 167 168if (emotivo < 0x1ff) 169 { 170 digitalWrite(10, LOW); 171 digitalWrite(6, LOW); 172 digitalWrite(latchPin, LOW); 173 // shift out the bits: 174 shiftOut(dataPin, clockPin, MSBFIRST, emotivo); 175 //take the latch pin high so the LEDs will light up: 176 digitalWrite(latchPin, HIGH); 177 delay (3000); 178 } 179 else 180 if (emotivo == 0x1FF) 181 { 182 digitalWrite(10, LOW); 183 digitalWrite(6, LOW); 184 digitalWrite(latchPin, LOW); 185 // shift out the bits: 186 shiftOut(dataPin, clockPin, MSBFIRST, 0xFF); 187 //take the latch pin high so the LEDs will light up: 188 digitalWrite(latchPin, HIGH); 189 digitalWrite(10, HIGH); 190 delay (3000); 191 } 192 else 193 if (emotivo == 0x2FF) 194 { 195 digitalWrite(10, LOW); 196 digitalWrite(6, LOW); 197 digitalWrite(latchPin, LOW); 198 // shift out the bits: 199 shiftOut(dataPin, clockPin, MSBFIRST, 0xFF); 200 //take the latch pin high so the LEDs will light up: 201 digitalWrite(latchPin, HIGH); 202 digitalWrite(10, HIGH); 203 digitalWrite(6, HIGH); 204 delay (3000); 205 } 206 207 intellettuale = bioIntellettuale(daysPassed); 208 Serial.print("Intellectual: "); 209 Serial.println(intellettuale); 210 lcd.setCursor(0, 1); 211 lcd.print(" INTELLECTUAL "); 212 213if (intellettuale < 0x1ff) 214 { 215 digitalWrite(10, LOW); 216 digitalWrite(6, LOW); 217 digitalWrite(latchPin, LOW); 218 // shift out the bits: 219 shiftOut(dataPin, clockPin, MSBFIRST, intellettuale); 220 //take the latch pin high so the LEDs will light up: 221 digitalWrite(latchPin, HIGH); 222 delay (3000); 223 } 224 else 225 if (intellettuale == 0x1FF) 226 { 227 digitalWrite(10, LOW); 228 digitalWrite(6, LOW); 229 digitalWrite(latchPin, LOW); 230 // shift out the bits: 231 shiftOut(dataPin, clockPin, MSBFIRST, 0xFF); 232 //take the latch pin high so the LEDs will light up: 233 digitalWrite(latchPin, HIGH); 234 digitalWrite(10, HIGH); 235 delay (3000); 236 } 237 else 238 if (intellettuale == 0x2FF) 239 { 240 digitalWrite(10, LOW); 241 digitalWrite(6, LOW); 242 digitalWrite(latchPin, LOW); 243 // shift out the bits: 244 shiftOut(dataPin, clockPin, MSBFIRST, 0xFF); 245 //take the latch pin high so the LEDs will light up: 246 digitalWrite(latchPin, HIGH); 247 digitalWrite(10, HIGH); 248 digitalWrite(6, HIGH); 249 delay (3000); 250 } 251 252} 253 254int bioFisico(int dayPassed) 255{ 256/* 257 * Positive = 0x1 - (led 1) 258 * Negative = 0x2 - (led 2) 259 * Critical Negative = 0x4 - (led 3) 260 * Critical Positive = 0x8 - (led 4) 261 * Semicritic = 0x10 - (led 5) 262 * 263 */ 264 button1.tick(); 265 int i; 266 int fisicoValue; 267 fisicoValue = (dayPassed)/23; 268 i = fisicoValue * 23; 269 fisicoValue = dayPassed - i; 270 271 if (fisicoValue > -1 and fisicoValue < 3) 272 fisicoValue = 0x3; 273 else 274 if (fisicoValue > 2 and fisicoValue < 6) 275 fisicoValue = 0x7; 276 else 277 if (fisicoValue > 5 and fisicoValue < 9) 278 fisicoValue = 0xF; 279 else 280 if (fisicoValue > 8 and fisicoValue < 12) 281 fisicoValue = 0x1F; 282 else 283 if (fisicoValue > 11 and fisicoValue < 15) 284 fisicoValue = 0x3F; 285 else 286 if (fisicoValue > 14 and fisicoValue < 18) 287 fisicoValue = 0x7F; 288 else 289 if (fisicoValue > 17 and fisicoValue < 21) 290 fisicoValue = 0xFF; 291 else 292 if (fisicoValue > 20 and fisicoValue < 22) 293 fisicoValue = 0x1FF; 294 else 295 fisicoValue = 0x2FF; 296 297 return (fisicoValue); 298 299} 300 301int bioEmotivo(int dayPassed) 302{ 303/* 304 * Positive = 0x1 - (led 1) 305 * Negative = 0x2 - (led 2) 306 * Critical Negative = 0x4 - (led 3) 307 * Critical Positive = 0x8 - (led 4) 308 * Semicritic = 0x10 - (led 5) 309 * 310 */ 311 button1.tick(); 312 int i; 313 int fisicoValue; 314 fisicoValue = (dayPassed)/28; 315 i = fisicoValue * 28; 316 fisicoValue = dayPassed - i; 317 318 if (fisicoValue > -1 and fisicoValue < 3) 319 fisicoValue = 0x3; 320 else 321 if (fisicoValue > 2 and fisicoValue < 7) 322 fisicoValue = 0x7; 323 else 324 if (fisicoValue > 6 and fisicoValue < 10) 325 fisicoValue = 0xF; 326 else 327 if (fisicoValue > 9 and fisicoValue < 14) 328 fisicoValue = 0x1F; 329 else 330 if (fisicoValue > 13 and fisicoValue < 18) 331 fisicoValue = 0x3F; 332 else 333 if (fisicoValue > 17 and fisicoValue < 21) 334 fisicoValue = 0x7F; 335 else 336 if (fisicoValue > 20 and fisicoValue < 25) 337 fisicoValue = 0xFF; 338 else 339 if (fisicoValue > 24 and fisicoValue < 27) 340 fisicoValue = 0x1FF; 341 else 342 fisicoValue = 0x2FF; 343 344 return (fisicoValue); 345 346} 347 348int bioIntellettuale(int dayPassed) 349{ 350/* 351 * Positive = 0x1 - (led 1) 352 * Negative = 0x2 - (led 2) 353 * Critical Negative = 0x4 - (led 3) 354 * Critical Positive = 0x8 - (led 4) 355 * Semicritic = 0x10 - (led 5) 356 * 357 */ 358 button1.tick(); 359 int i; 360 int fisicoValue; 361 fisicoValue = (dayPassed)/33; 362 i = fisicoValue * 33; 363 fisicoValue = dayPassed - i; 364 365 if (fisicoValue > -1 and fisicoValue < 4) 366 fisicoValue = 0x3; 367 else 368 if (fisicoValue > 3 and fisicoValue < 8) 369 fisicoValue = 0x7; 370 else 371 if (fisicoValue > 7 and fisicoValue < 12) 372 fisicoValue = 0xF; 373 else 374 if (fisicoValue > 11 and fisicoValue < 16) 375 fisicoValue = 0x1F; 376 else 377 if (fisicoValue > 15 and fisicoValue < 20) 378 fisicoValue = 0x3F; 379 else 380 if (fisicoValue > 19 and fisicoValue < 24) 381 fisicoValue = 0x7F; 382 else 383 if (fisicoValue > 23 and fisicoValue < 28) 384 fisicoValue = 0xFF; 385 else 386 if (fisicoValue > 27 and fisicoValue < 32) 387 fisicoValue = 0x1FF; 388 else 389 fisicoValue = 0x2FF; 390 391 return (fisicoValue); 392 393} 394 395int dateDiff(int year1, int mon1, int day1, int year2, int mon2, int day2) 396{ 397 int ref,dd1,dd2,i; 398 ref = year1; 399 if(year2<year1) 400 ref = year2; 401 dd1=0; 402 dd1=dater(mon1); 403 for(i=ref;i<year1;i++) 404 { 405 if(i%4==0) 406 dd1+=1; 407 } 408 dd1=dd1+day1+(year1-ref)*365; 409 dd2=0; 410 for(i=ref;i<year2;i++) 411 { 412 if(i%4==0) 413 dd2+=1; 414 } 415 dd2=dater(mon2)+dd2+day2+((year2-ref)*365); 416 return dd2-dd1; 417} 418 419int dater(int x) 420{ const int dr[]= { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; 421 return dr[x-1]; 422} 423 424void illuminaLed(int val) 425{ 426 Serial.print("VAL: "); 427 Serial.println(val); 428 int i=0; 429 int led=0; 430 if (val == 2) 431 led = 9; 432 else if (val == 4) 433 led = 10; 434 else if (val == 8) 435 led = 11; 436 else if (val == 1) 437 led = 6; 438 else if (val == 16) 439 led = 11; 440 441 442 Serial.print("LED: "); 443 Serial.println(led); 444 445 for (i=0;i<20;i++) 446 { 447 analogWrite(led,i); 448 delay (50); 449 } 450 451 for (i=20;i>-1;i--) 452 { 453 analogWrite(led,i); 454 delay (50); 455 } 456 457} 458 459 460void click1() { 461 Serial.println("Button 1 click."); 462 463} 464// This function will be called when the button1 was pressed 2 times in a short timeframe. 465 466 467void doubleclick1() { 468 Serial.println("Button 1 doubleclick."); 469 lcd.noBlink(); 470 if (exitFlag == 0) 471 { 472 yearBirth = anno+lastPos; 473 exitFlag = 1; 474 exit; 475 } 476 else 477 if (exitFlag == 1) 478 { 479 monthBirth = mese+lastPos; 480 exitFlag = 2; 481 exit; 482 } 483 else 484 if (exitFlag == 2) 485 { 486 dayBirth = giorno+lastPos; 487 exitFlag = 3; 488 exit; 489 } 490 491 492} // doubleclick1 493 494 495// This function will be called once, when the button1 is pressed for a long time. 496void longPressStart1() { 497 Serial.println("Button 1 longPress start"); 498 499 500 lcd.setCursor(0, 0); 501 lcd.print(" SET BIRTH DATE "); 502 lcd.setCursor(0,1); 503 lcd.print("Year: "); 504 lcd.setCursor(6,1); 505 lcd.blink(); 506 lcd.print(anno+lastPos); 507 lcd.setCursor(6,1); 508 509 510 exitFlag = 0; 511while(exitFlag < 1) 512 { 513 encoder.tick(); 514 button1.tick(); 515 // Insert year 516 #define ROTARYMAX 70 517 int newPos = encoder.getPosition() * ROTARYSTEPS; 518 519 if (newPos < ROTARYMIN) { 520 encoder.setPosition(ROTARYMIN / ROTARYSTEPS); 521 newPos = ROTARYMIN; 522 523 } else if (newPos > ROTARYMAX) { 524 encoder.setPosition(ROTARYMAX / ROTARYSTEPS); 525 newPos = ROTARYMAX; 526 } 527 528 if (lastPos != newPos) { 529 Serial.print(newPos+anno); 530 Serial.println(); 531 lcd.print(newPos+anno); 532 lcd.setCursor(6,1); 533 lastPos = newPos; 534 } // if 535 } 536 537 lastPos = 0; 538 lcd.setCursor(0, 0); 539 lcd.print(" SET BIRTH DATE "); 540 lcd.setCursor(0,1); 541 lcd.print("Month: "); 542 lcd.setCursor(7,1); 543 lcd.blink(); 544 lcd.print(mese+lastPos, DEC); 545 lcd.setCursor(7,1); 546 encoder.setPosition(0 / ROTARYSTEPS); // start with the value of 1. 547 548while(exitFlag < 2) 549 { 550 encoder.tick(); 551 button1.tick(); 552 // Insert month 553 #define ROTARYMAX 11 554 int newPos = encoder.getPosition() * ROTARYSTEPS; 555 556 if (newPos < ROTARYMIN) { 557 encoder.setPosition(ROTARYMIN / ROTARYSTEPS); 558 newPos = ROTARYMIN; 559 560 } else if (newPos > ROTARYMAX) { 561 encoder.setPosition(ROTARYMAX / ROTARYSTEPS); 562 newPos = ROTARYMAX; 563 } 564 565 if (lastPos != newPos) { 566 Serial.print(newPos+mese); 567 Serial.println(); 568 lcd.setCursor(0,1); 569 lcd.print("Month: "); 570 lcd.setCursor(7,1); 571 lcd.print(newPos+mese,DEC); 572 lcd.setCursor(7,1); 573 lastPos = newPos; 574 } 575 } 576 577 lastPos = 0; 578 lcd.setCursor(0, 0); 579 lcd.print(" SET BIRTH DATE "); 580 lcd.setCursor(0,1); 581 lcd.print("Day: "); 582 lcd.setCursor(5,1); 583 lcd.blink(); 584 lcd.print(mese+lastPos); 585 lcd.setCursor(5,1); 586 encoder.setPosition(0 / ROTARYSTEPS); // start with the value of 1. 587 588while(exitFlag < 3) 589 { 590 encoder.tick(); 591 button1.tick(); 592 // Insert day 593 #define ROTARYMAX 30 594 int newPos = encoder.getPosition() * ROTARYSTEPS; 595 596 if (newPos < ROTARYMIN) { 597 encoder.setPosition(ROTARYMIN / ROTARYSTEPS); 598 newPos = ROTARYMIN; 599 600 } else if (newPos > ROTARYMAX) { 601 encoder.setPosition(ROTARYMAX / ROTARYSTEPS); 602 newPos = ROTARYMAX; 603 } // if 604 605 if (lastPos != newPos) { 606 Serial.print(newPos+giorno); 607 Serial.println(); 608 lcd.setCursor(0,1); 609 lcd.print("Day: "); 610 lcd.setCursor(5,1); 611 lcd.print(newPos+giorno); 612 lcd.setCursor(5,1); 613 lastPos = newPos; 614 } 615 } 616 617 618 619} // longPressStart1 620 621 622// This function will be called often, while the button1 is pressed for a long time. 623void longPress1() { 624 Serial.println("Button 1 longPress..."); 625 626} // longPress1 627 628 629// This function will be called once, when the button1 is released after beeing pressed for a long time. 630void longPressStop1() { 631 Serial.println("Button 1 longPress stop"); 632 633} // longPressStop1 634
Downloadable files
Schematics for Biorhythm Clock
Schematics for Biorhythm Clock
Schematics for Biorhythm Clock
Schematics for Biorhythm Clock
Comments
Only logged in users can leave comments
cstram
0 Followers
•0 Projects
Table of contents
Intro
2
0