Components and supplies
Arduino Nano R3
Dual H-Bridge motor drivers L298
Linear Regulator (7805)
Infrared Remote Control RC5 SME002 Wallis Universal 21 key
Infrared Receiver Head Module LTM-8848-1 Liteon Semiconductors
Capacitor 100 µF
2 Ports Relay module
Tools and machines
10 Pc. Jumper Wire Kit, 5 cm Long
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Soldering iron (generic)
Solder Wire, Lead Free
PCB, For DMB-4775
Apps and platforms
Arduino IDE
Processing
Project description
Code
Sketch REMOTINO NANO V19
c_cpp
Compile with Arduino IDE and upload on Arduino NANO
1/* 2 3 remotino nano v19 4 5 sketch by Pautasso Luciano, Turin Italy lpautas@gmail.com 6 7 8*/ 9 10//Remote Control SME002 Wallis Universal key table 11 12 13#define key_0 0x700 14#define key_1 0x701 15#define key_2 0x702 16#define key_3 0x703 17#define key_4 0x704 18#define key_5 0x705 19#define key_6 0x706 20#define key_7 0x707 21#define key_8 0x708 22#define key_9 0x709 23 24#define key_a 0x72b 25#define key_b 0x72c 26#define key_c 0x72d 27#define key_d 0x72e 28 29#define key_go 0x738 30#define key_off 0x70C 31#define key_chp 0x720 32#define key_chm 0x721 33#define key_volp 0x710 34#define key_volm 0x711 35#define key_ok 0x717 36 37 38 39 40 41#define BOARD_NANO 42 43 44 45 46//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 47// 48// TIPO DI TELECOMANDO 49 50#define TLC_SME002 51//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 52 53 54 55 56 57#include <IRremote.h> 58 59#include <EEPROM.h> 60//indirizzi EEProm 61int EEPROMaddr = 10; // 10 = Mode 62 63 64 65 66 67 68#ifdef BOARD_NANO 69int RECV_PIN = 10; // UNO input IR pin 11 70#endif 71 72 73 74 75IRrecv irrecv(RECV_PIN); 76 77decode_results results; 78 79String inputString = ""; // a string to hold incoming data 80boolean stringComplete = false; // whether the string is complete 81 82int UltimaMarcia = 0; // vale 0, 25, 50, 75, 100 , 0 = no marce 83int CodeReceived; 84int KeyPressed = 255; 85int TempToggle; 86int Togglelast = 0; 87bool ProtocolOK; 88char Mode = 'a' ; // vale "a" "b" "c" "d", letto da eeprom, decide modalità di funzionamento 89bool Debug = false; 90bool DebugMillis = false ; 91bool FlagKeyPressed = false; 92bool FlagFirstRun = true; 93bool ChangeModeInProgress = false; 94unsigned long TimeLastKeyPressed; 95unsigned long TimeBlink; 96int Temp; 97bool CodiceIrValido = false ; // Si mette true quando un codice IR è ricevuto 98 99int Pin_PWM_Mot1 = 9; // canale pwm1 100unsigned char PWM1 = 0; // Valore attuale della PWM del motore1 101unsigned char PWM2 = 0; // Valore attuale della PWM del motore2 102bool DIREZIONE1 = true; // Direzione attuale Motore 1 False indietro, Trua Avanti 103bool DIREZIONE2 = true; // Direzione attuale Motore 2 False indietro, Trua Avanti 104bool MOTORON = false; // Stato del motor on 105 106/* 107 108 //accelerazione veloce 109 unsigned char Accelera = 20; // incremento di velocità 110 unsigned char Rallenta = 20; // decremeto per rallenta 111 unsigned char Fermata = 10; // decremento per 112*/ 113unsigned char Accelera = 5; // incremento di velocità 114unsigned char Rallenta = 5; // decremeto per rallenta 115unsigned char Fermata = 3; // decremento per 116 117 118int ContaIR = 0; // si conta ogni IR valido ricevuto e solo entro i primi 5 è possibile cambiare modo 119bool ModeCambiabile = true; // dopo due ir ricevuti diventa false 120 121 122int ContaFrames = 0; 123unsigned long Debug1 = 0; 124unsigned long Debug2 = 0; 125unsigned long Debug3 = 0; 126 127//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 128 129 130 131 132 133#ifdef BOARD_NANO 134// #define NumeroRele 8 135#define NumeroRele 9 136#endif 137 138 139 140 141 142int TimeOut = 140; 143 144bool ReleStatus[NumeroRele]; // ARRAY, Contiene lo stato del relè, True = Chiuso, False = Aperto 145 146bool ReleType[NumeroRele]; // ARRAY, Contiene il tipo del relè, True = Bistabile , False = Monostabile 147 148int RelePin[NumeroRele]; // ARRAY, Contiene i pin relativi ai vari relè 149 150bool ReleToggle[NumeroRele]; // ARRAY, Contiene il prossimo stato del relè che è oggetto di toggle 151 152 153 154 155 156//******************************************************************* 157 158 159 160//************************************************************************************** 161 162//Dichiarazione di funzione che punta all'indirizzo zero 163void(* Riavvia)(void) = 0; 164 165void setup() 166{ 167 168 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 169 // COLLEGA I RELE AI PIN 170 171 172 173#ifdef BOARD_NANO 174#define RELE1 2 175#define RELE2 3 176#define RELE3 4 177#define RELE4 5 178#define RELE5 6 179#define RELE6 7 180#define RELE7 8 181#define RELE8 11 182 183 RelePin[0] = RELE1; 184 RelePin[1] = RELE2; 185 RelePin[2] = RELE3; 186 RelePin[3] = RELE4; 187 RelePin[4] = RELE5; 188 RelePin[5] = RELE6; 189 RelePin[6] = RELE7; 190 RelePin[7] = RELE8; 191 192 193 194#endif 195 196 197 198 199 //riempie gli array 200 for (int indice = 0; indice < (NumeroRele); indice++) 201 { 202 ReleStatus[indice] = true; //true bistabili toggle 203 ReleType[indice] = true; // tutti bistabili toggle 204 ReleToggle[indice] = false; //decide se attiva o disattiva al keypress, invertito dal loop al timeout 205 206 207 } 208 209 210 211 212 213 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 214 // IMPOSTA I BIT RELE COME OUTPUT 215 216#ifdef BOARD_NANO 217 pinMode(RELE1, OUTPUT); 218 pinMode(RELE2, OUTPUT); 219 pinMode(RELE3, OUTPUT); 220 pinMode(RELE4, OUTPUT); 221 pinMode(RELE5, OUTPUT); 222 pinMode(RELE6, OUTPUT); 223 pinMode(RELE7, OUTPUT); 224 pinMode(RELE8, OUTPUT); 225 226 // IMPOSTA I BIT MOTORI COME OUTPUT 227 228#define MOT1_AVANTI 12 229#define MOT1_INDIETRO 13 230 231 232#endif 233 234 235 236 237 238 239 240 241 242 243 244 Serial.begin(115200); 245 246 // reserve 200 bytes for the inputString: 247 inputString.reserve(200); 248 249 irrecv.enableIRIn(); // Start the receiver 250 251 252 EEPROMaddr = 10; // 10 = Mode 253 Mode = EEPROM.read(EEPROMaddr); 254 255 256 257 258 259 Serial.print("Modehex "); 260 Serial.println(Mode, HEX); 261 262 #define ReleBistabile true 263 #define ReleMonostabile false 264 265 // cambia il mode scrivendo nell'array i valori 266 switch (Mode) { 267 case 'b': 268 // Modo B , 8 bistabili ON/OFF , ogni volta inverte lo stato 269 for (int indice = 0; indice < (NumeroRele); indice++) 270 { 271 ReleType[indice] = ReleBistabile; // tutti bistabili toggle 272 } 273 break; 274 275 case 'c': // 276 // Modo C - RELE 1,2 BISTABILE , RELE' 3,4,5,6,7,8 MONOSTABILI SCAMBIO TIMEOUT 140ms 277 for (int indice = 0; indice < (NumeroRele); indice++) 278 { 279 ReleType[indice] = ReleMonostabile; 280 } 281 ReleType[0] = ReleBistabile; 282 ReleType[1] = ReleBistabile; 283 break; 284 285 case 'd': // 286 // Modo D - RELE 1,2,3,4 BISTABILI, RELE' 5 6 7 8 MONOSTABILI SCAMBIO 287 for (int indice = 0; indice < (NumeroRele); indice++) 288 { 289 ReleType[indice] = ReleMonostabile; 290 } 291 292 for (int indice = 0; indice < (4); indice++) 293 { 294 ReleType[indice] = ReleBistabile; 295 } 296 297 298 299 300 301 //questi di seguito i monostabili, lascia solo 1 e 2 bistabili 302 303 304#ifdef BOARD_NANO 305#endif 306 307 308 break; 309 310 default: 311 // Modo A , 8 rele monostabili, 140msec , ReleMonostabile false 312 for (int indice = 0; indice < (NumeroRele); indice++) 313 { 314 ReleType[indice] = ReleMonostabile; //false monostabili 315 } 316 TimeOut = 140; 317 } 318 319 320 delay(500); 321 322 if (( Mode < 'a' ) || ( Mode > 'd' )) { 323 Serial.println("PASS-E2?"); 324 delay(200); 325 326 327 while (Serial.available() == 0) { 328 } 329 330 331 if (Serial.read() == 'p') { 332 EEPROMaddr = 10; // 10 = Mode 333 EEPROM.write(EEPROMaddr, 'a'); 334 } 335 else { 336 Serial.println("Reset!!"); 337 delay(1000); 338 Riavvia(); 339 } 340 341 342 } 343 344 345#ifdef BOARD_NANO 346 Serial.println("Board NANO"); 347#endif 348 349 350 351 352 353 354 355 PrintStatus(); 356 357 //AggiornaStatoRele(); 358 OpenAllRele(); 359} 360//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 361void PrintStatus3() { 362 363 364 365 366 Serial.print("ARRAY 012345678 -> ReleType "); 367 for (int indice = 0; indice < (NumeroRele); indice++) 368 { 369 if (ReleType[indice] == true) { 370 Serial.print("1"); 371 } else { 372 Serial.print("0"); 373 } 374 } 375 376 Serial.print(" ReleToggle "); 377 for (int indice = 0; indice < (NumeroRele); indice++) 378 { 379 if (ReleToggle[indice] == true) { 380 Serial.print("1"); 381 } else { 382 Serial.print("0"); 383 } 384 } 385 386 Serial.print(" ReleStatus "); 387 for (int indice = 0; indice < (NumeroRele); indice++) 388 { 389 if (ReleStatus[indice] == true) { 390 Serial.print("1"); 391 } else { 392 Serial.print("0"); 393 } 394 } 395 396 Serial.println(" "); 397 Serial.print("Pinout RelePin "); 398 for (int indice = 0; indice < (NumeroRele); indice++) 399 { 400 Serial.print(indice); 401 Serial.print(":r"); 402 Serial.print(RelePin[indice]); 403 Serial.print(", "); 404 } 405 406 AggiornaStatoMotori(); 407 408 409} 410 411 412void AggiornaStatoRele() { 413 414 Debug = true; 415 416 if (Debug == true) 417 { 418 //stampa lo stato dei rele 419 Serial.print("123456789.. STA:"); 420 for (int indice = 0; indice < (NumeroRele); indice++) 421 { 422 if (ReleStatus[indice]) { 423 Serial.print("1"); 424 } else { 425 Serial.print("0"); 426 } 427 } 428 429 Serial.print("; TYP:"); 430 for (int indice = 0; indice < (NumeroRele); indice++) 431 { 432 if (ReleType[indice]) { 433 Serial.print("1"); 434 } else { 435 Serial.print("0"); 436 } 437 } 438 439 Serial.print("; TGL:"); 440 for (int indice = 0; indice < (NumeroRele); indice++) 441 { 442 if (ReleToggle[indice]) { 443 Serial.print("1"); 444 } else { 445 Serial.print("0"); 446 } 447 } 448 449 Serial.println(""); 450 451 452 453 454 455 456 457 458 459 460 } 461 462 463// attua i comandi 464if (KeyPressed < NumeroRele) { //EVITA ERRORE FUORI ARRAY 465 for (int indice = 0; indice < (NumeroRele); indice++) 466 { 467 if (ReleStatus[indice]) { 468 digitalWrite(RelePin[indice], HIGH); 469 } else { 470 digitalWrite(RelePin[indice], LOW); 471 } 472 } 473} 474 475 476 477 478 479 480 481} 482 483//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 484void OpenAllRele() { 485 //Serial.println("open all rele "); 486 487 488 for (int indice = 0; indice < (NumeroRele); indice++) 489 { 490 ReleStatus[indice] = false; 491 } 492 493 AggiornaStatoRele(); 494} 495 496// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 497 498 499void PreProcessaTasto() { 500 if (CodiceIrValido == true) 501 { 502 CodiceIrValido == false; 503 504 ProcessaTasto(CodeReceived); 505 } 506} 507 508 509//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 510void ProcessaComandoSeriale() { 511 String a1, b1, c1; 512 long KeySerial; 513 514 515 inputString.trim(); //ELIMINA SPAZI PRIMA E DOPO 516 a1 = (inputString.substring(0, 1)); 517 b1 = (inputString.substring(1, 2)); 518 c1 = (inputString.substring(2, 3)); 519 520 521 522 if ((a1 == "R") || (a1 == "r")) { // se è un comando R 523 // if (isDigit(b1)) { // se è un numero (da 0 a 9 524 KeySerial = b1.toInt(); 525 526 Debug1 = millis(); 527 528 //decrementa di uno perchè i rele vanno da 1 a 8 per nano ma la matrice va da 0 a 7 529 KeySerial--; 530 531 if (c1 == "1") { 532 AttivaRele(KeySerial); 533 TimeLastKeyPressed = ( millis() + TimeOut ); 534 Serial.print("Serial ON RL"); 535 Serial.println(KeySerial); 536 } 537 538 if (c1 == "0") { 539 DisAttivaRele(KeySerial); 540 TimeLastKeyPressed = ( millis() + TimeOut ); 541 Serial.print("Serial OFF RL"); 542 Serial.println(KeySerial); 543 } 544 // } 545 } else if (a1 == "?") { 546 PrintStatus(); 547 PrintStatus3(); 548 } else if ((a1 == "M") || (a1 == "m")) { // se è un comando Motore 549 550 551 b1.toLowerCase(); 552 553 if (b1 == "a") { 554 Serial.println("Vel 100"); 555 ImpostaVelocita(100); 556 } 557 if (b1 == "b") { 558 Serial.println("Vel 150"); 559 ImpostaVelocita(150); 560 } 561 if (b1 == "c") { 562 Serial.println("Vel 200"); 563 ImpostaVelocita(200); 564 } 565 if (b1 == "d") { 566 Serial.println("Vel 255"); 567 ImpostaVelocita(255); 568 } 569 570 if (b1 == "8") { 571 Serial.println("Pwm1Up"); 572 Pwm1Up(); 573 } 574 if (b1 == "2") { 575 Serial.println("Pwm1Down"); 576 Pwm1Down(); 577 } 578 if (b1 == "4") { 579 Serial.println("Pwm2Down"); 580 Pwm2Down(); 581 } 582 if (b1 == "6") { 583 Serial.println("Pwm2Up"); 584 Pwm2Up(); 585 } 586 if (b1 == "0") { 587 Serial.println("Frenata Lenta"); 588 ImpostaVelocita(0); 589 } 590 if (b1 == "5") { 591 Serial.println("GoMotorOn"); 592 GoMotorOn(); 593 } 594 595 AggiornaStatoMotori(); 596 597 598 } else { 599 Serial.println("invalid CMD"); 600 } 601 602 603 604} 605//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 606void ChangeMode(int NewMode) { 607 // NewMode vale 10, 11, 12, 13 608 //Serial.print("Changing Mode "); 609 switch (NewMode) { 610 case 11: // 611 Mode = 'b'; 612 break; 613 614 case 12: // 615 Mode = 'c'; 616 break; 617 618 case 13: // 619 Mode = 'd'; 620 break; 621 622 default: 623 Mode = 'a'; 624 625 } 626 627 EEPROMaddr = 10; // 10 = Mode 628 EEPROM.write(EEPROMaddr, Mode); 629 setup(); 630 PrintStatus(); 631 632 //ora azzeraq tutti i rele 633 OpenAllRele(); 634 635 for (int i = 1; i > 0; i--) { 636 Serial.print("Riavvio tra "); 637 Serial.print(i); 638 Serial.println(" sec."); 639 delay(1000); 640 } 641 642 //reset via software 643 Serial.println("Reset!!"); 644 delay(1000); 645 Riavvia(); 646 647 648 649} 650 651 652 653//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 654void PrintStatus() { 655 // Serial.println("STATUS"); 656 Mode = EEPROM.read(EEPROMaddr); 657 658 Serial.print("Modehex "); 659 Serial.println(Mode, HEX); 660 661 Serial.println("ver: IrBoardRele_17-univ2"); 662 //Serial.print("Mode EEPROM cancellarlo "); 663 Serial.print("Mode "); 664 Serial.println(Mode); 665 666 Serial.print("Numero Rele "); 667 Serial.println(NumeroRele); 668 669#ifdef BOARD_NANO 670 Serial.println("Board NANO"); 671#endif 672 673 674 675 676 // Mode 677 // Modo A , 8 rele monostabili, 140msec , ReleMonostabile false TIMEOUT 140ms 678 // Modo B , 8 bistabili ON/OFF , ogni volta inverte lo stato 679 // Modo C - RELE 1,2 BISTABILE , RELE' 3,4,5,6,7,8 MONOSTABILI SCAMBIO 680 // Modo D - RELE 1,2,3,4 BISTABILI, RELE' 5 6 7 8 MONOSTABILI SCAMBIO 681 682 switch (Mode) { //stampa la descrizione del Mode 683 case 'a': 684 Serial.println("Mode A: 8 relais monostabili"); 685 break; 686 case 'b': 687 Serial.println("Mode B: 8 relais bistabili/toggle"); 688 break; 689 case 'c': 690 Serial.println("Mode C: relais 1,2 bistabili, relais 3,4,5,6,7,8 monostabili"); 691 break; 692 case 'd': 693 Serial.println("Mode D: relais 1,2,3,4 bistabili, relais 5,6,7,8 monostabili"); 694 break; 695 default: 696 Serial.println("Error in Mode selection"); 697 } 698 699 700 701} 702//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 703void DisAttivaRele(int KeyPressed) { 704 if (KeyPressed < NumeroRele) { //EVITA ERRORE FUORI ARRAY 705 706 ReleStatus[KeyPressed] = false ; 707 AggiornaStatoRele(); 708 Serial.print("RLOF="); 709 Serial.println(KeyPressed); 710 711 } 712} 713//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 714void AttivaRele(int KeyPressed) { 715 if (KeyPressed < NumeroRele) { //EVITA ERRORE FUORI ARRAY 716 717 ReleStatus[KeyPressed] = true ; 718 AggiornaStatoRele(); 719 Serial.print("RLON="); 720 Serial.println(KeyPressed); 721 } 722} 723 724//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 725// VelMax vale 25, 50, 75 e 100% della velocità max, ovvero 64, 128, 192, 255 726// il ciclo continua all'interno della procedura fino a che PWM1 vale MaxVel 727void ImpostaVelocita(int VelMax) 728{ 729 bool ExitLoopMaxVel = false ; // quando vale true si esce dal ciclo 730 int CicliMaxVel = 0; //solo per debug 731 int QuantoManca = 0; // differenza tra obiettivo e velocità attuale 732 int Correzione = 0; // valore della prosima correzione necessaria per raggiungere l'obiettivo 733 734#define DeltaVel 3 735 //DeltaVel è l'incremento/decremento di velocità da applicare ad ogni step 736 737 while (ExitLoopMaxVel == false) { 738 CicliMaxVel ++ ; 739 740 //se motoroff non fa nulla 741 if (MOTORON == true) { 742 743 // qui siamo in motoron, possiamo correggere la velocità 744 QuantoManca = VelMax - PWM1; 745 746 747 748 if (QuantoManca == 0) { 749 Serial.print("QuantoManca=0 - "); 750 Correzione = 0; 751 ExitLoopMaxVel = true; 752 } 753 754 // due casi 755 if (QuantoManca > 0) { 756 Serial.print("QuantoManca>0 - "); 757 // caso 1 valori positivi VelMax = 128, PWM1 = 100 , QuantoManca = 28 758 if (QuantoManca > DeltaVel) { 759 Correzione = DeltaVel; 760 // QuantoManca = 20 , DeltaVel = 3, Correggo di 3 761 } else { 762 Correzione = (VelMax - PWM1); // QuantoManca = 2 , DeltaVel = 3, Correggo di 2 763 ExitLoopMaxVel = true; 764 } 765 } else { 766 Serial.print("QuantoManca<0 - "); 767 // caso 2 valori negativi VelMax = 128, PWM1 = 140, QuantoManca = -12 768 QuantoManca = PWM1 - VelMax; // lavoro con segno positivo per evitare errori di programmazione 769 if (QuantoManca > DeltaVel) { 770 Correzione = -DeltaVel ; // QuantoManca = 20 , DeltaVel = 3, Correggo di -3 771 } else { 772 Correzione = -(PWM1 - VelMax); // QuantoManca = 2 , DeltaVel = 3, Correggo di -2 773 ExitLoopMaxVel = true; 774 } 775 } 776 777 778 // eseguo la correzione 779 PWM1 = PWM1 + Correzione; 780 781 782 delay(200); //ritardo di regolazione velocità 783 AggiornaStatoMotori(); 784 785 // controlla se è stato premuto lo 0 per uno stop immediato di emergenza 786 if (CheckCodiceIr() == true) { 787 if (CodeReceived == key_off) { 788 EmergencyStop(); 789 // PWM1 = 0; // fermata di emergenza 790 // Serial.println( "Emergency STOP"); 791 ExitLoopMaxVel = true; 792 } 793 794 } 795 796 } else { 797 Serial.println("NON ESEGUITO - Motor OFF"); 798 ExitLoopMaxVel = true; 799 } 800 801 // esce al ciclo di while 802 } 803 804} 805//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 806 807 808 809//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 810 811void ProcessaTasto(int FuCodeReceived) 812{ 813 // dato il tasto premuto scrive KeyPressed che è l'indice dell'array relè 814 815 816 817 FlagKeyPressed = true; 818 KeyPressed = 255; //valore di default per non fare altre azioni 819 switch (FuCodeReceived) 820 { 821 // caso mappa standard 822 // nano collego tasti da 1 a 8 ai rele nell'array da 0 a 7 823 824 case key_0: // 0 825 ImpostaVelocita(0); 826 break; 827 828 case key_1: // 1 829 KeyPressed = 0; 830 break; 831 832 case key_2: // 2 833 KeyPressed = 1; 834 break; 835 836 case key_3: // 3 837 KeyPressed = 2; 838 break; 839 840 case key_4: // 4 841 KeyPressed = 3; 842 break; 843 844 case key_5: // 5 845 KeyPressed = 4; 846 break; 847 848 case key_6: // 6 849 KeyPressed = 5; 850 break; 851 852 case key_7: // 7 853 KeyPressed = 6; 854 break; 855 856 case key_8: // 8 857 KeyPressed = 7; 858 break; 859 860 case key_9: // 9 861 break; 862 863 case key_a: // A 864 ImpostaVelocita(100); 865 ChangeMode2('a'); 866 break; 867 868 case key_b: // B 869 ChangeMode2('b'); 870 ImpostaVelocita(150); 871 break; 872 873 case key_c: // C 874 ImpostaVelocita(200); 875 ChangeMode2('c'); 876 break; 877 878 case key_d: // D 879 ImpostaVelocita(255); 880 ChangeMode2('d'); 881 break; 882 883 case key_go: // GO 884 GoMotorOn(); 885 break; 886 887 case key_off: // OFF 888 EmergencyStop(); 889 break; 890 891 case key_chp: // CH+ 892 Pwm1Up(); 893 break; 894 895 case key_chm: // CH- 896 Pwm1Down(); 897 break; 898 899 case key_volp: // VOL+ 900 Pwm2Up(); 901 break; 902 903 case key_volm: // VOL- 904 Pwm2Down(); 905 break; 906 907 case key_ok: // OK 908 Boost130(); 909 break; 910 911 default: // NESSUN TASTO VALIDO 912 FlagKeyPressed = false; 913 914 915 // QUI SI COMPATTA LA TASTIERA SE MANCANO OUTPUT 916 917 } 918 919 920 921 922 923 924 925 AggiornaStatoMotori(); 926 927 // controlla che l'indice (keypressed) sia entro i limiti della lunghezza array, altrimenti non fa niente 928 929 if (KeyPressed < NumeroRele) { 930 931 932 933 TimeLastKeyPressed = millis(); 934 Debug1 = TimeLastKeyPressed; 935 TimeLastKeyPressed = TimeLastKeyPressed + TimeOut; 936 Debug2 = TimeLastKeyPressed; 937 938 939 if (ReleType[KeyPressed] == false) 940 { 941 //Serial.println("#F "); 942 AttivaRele(KeyPressed); // Monostabile 943 } 944 else 945 { 946 947 if (ReleToggle[KeyPressed] == false) 948 { 949 //Serial.println("#G "); //Bistabile 950 AttivaRele(KeyPressed); 951 } 952 else 953 { 954 //Serial.println("#H "); //Bistabile 955 DisAttivaRele(KeyPressed); 956 } 957 958 959 960 } 961 } 962 963} 964 965//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 966 967 968void ChangeMode2(char NewMode){ 969 // qui si controlla se è richiesto un cambio di Mode, fattibile solo i primi 5 codici ricevuti 970 // if (ContaIR < 6){ 971 if (ModeCambiabile == true) { 972 //Serial.print("Changing2 Mode2 "); 973 switch (NewMode) { 974 case 'a': // A 975 Mode = 'a'; 976 break; 977 case 'b': // B 978 Mode = 'b'; 979 break; 980 case 'c': // C 981 Mode = 'c'; 982 break; 983 case 'd': // D 984 Mode = 'd'; 985 break; 986 default: 987 Mode = 'a'; 988 } 989 990 991 EEPROMaddr = 10; // 10 = Mode 992 EEPROM.write(EEPROMaddr, Mode); 993 setup(); 994 PrintStatus(); 995 996 //ora azzeraq tutti i rele 997 OpenAllRele(); 998 999 1000 for (int i = 1; i > 0; i--) { 1001 Serial.print("Riavvio tra "); 1002 Serial.print(i); 1003 Serial.println(" sec."); 1004 delay(1000); 1005 } 1006 1007 Serial.println("Reset!!"); 1008 delay(1000); 1009 Riavvia(); 1010 1011 1012 } 1013} 1014 1015 1016 1017//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 1018// un codice IR è stato ricevuto 1019// si filtra e se è valido RC5 si mette CodiceIrValido = True 1020 1021void dump(decode_results *results) { 1022 Debug = false; 1023 // Dumps out the decode_results structure. 1024 // Call this after IRrecv::decode() 1025 int count = results->rawlen; 1026 1027 ProtocolOK = false; 1028 CodiceIrValido = false; 1029 1030 if (results->decode_type == UNKNOWN) { 1031 Serial.print("IR Inval. "); 1032 } 1033 else if (results->decode_type == NEC) { 1034 Serial.print("Decoded NEC: "); 1035 } 1036 else if (results->decode_type == SONY) { 1037 Serial.print("Decoded SONY: "); 1038 } 1039 else if (results->decode_type == RC5) { 1040 ProtocolOK = true; 1041 1042 ContaIR = ContaIR + 1; 1043 if (ContaIR > 2) { 1044 ContaIR = 2; // satura il valore a 6 per evitare overflow 1045 ModeCambiabile = false; 1046 } 1047 1048 1049 1050 if (Debug == true) 1051 { 1052 Serial.print("IR RC5 "); 1053 } 1054 1055 1056 } 1057 else if (results->decode_type == RC6) { 1058 Serial.print("Decoded RC6: "); 1059 } 1060 else if (results->decode_type == PANASONIC) { 1061 Serial.print("Decoded PANASONIC - Address: "); 1062 Serial.print(results->address, HEX); 1063 Serial.print(" Value: "); 1064 } 1065 else if (results->decode_type == LG) { 1066 Serial.print("Decoded LG: "); 1067 } 1068 else if (results->decode_type == JVC) { 1069 Serial.print("Decoded JVC: "); 1070 } 1071 else if (results->decode_type == AIWA_RC_T501) { 1072 Serial.print("Decoded AIWA RC T501: "); 1073 } 1074 else if (results->decode_type == WHYNTER) { 1075 Serial.print("Decoded Whynter: "); 1076 } 1077 1078 1079 if (ProtocolOK == true) 1080 { 1081 1082 if (Debug == true) 1083 { 1084 //qui si decodifica il trasto premuto 1085 Serial.print(results->value, HEX); 1086 Serial.print(" ("); 1087 } 1088 1089 CodeReceived = results->value; 1090 //il toggle è il bit 7 (0x703 0xF03) 1091 bitClear(CodeReceived, 11); //azzera il bit Toggle 1092 CodiceIrValido = true; 1093 1094 if (Debug == true) 1095 { 1096 Serial.print(CodeReceived, HEX); 1097 Serial.println(" filt)"); 1098 } 1099 } 1100 else 1101 { 1102 Serial.println("prot-error"); 1103 } 1104 1105 ProtocolOK = false; 1106 results->decode_type = UNKNOWN; 1107} 1108// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 1109//verifica se è presente un codice IR 1110//Ritorna true se disponibile 1111bool CheckCodiceIr() { 1112 if (irrecv.decode(&results)) { 1113 dump(&results); 1114 irrecv.resume(); // Receive the next value 1115 return true; 1116 } else { 1117 return false; 1118 } 1119} 1120//--------------------------------------------------------------- 1121 1122void loop() { 1123 1124 if (DebugMillis) 1125 { 1126 if (Debug3 > 0) 1127 { 1128 Serial.print(" dbg1 "); 1129 Serial.println(Debug1); 1130 Serial.print(" dbg2 "); 1131 Serial.println(Debug2); 1132 Serial.print(" dbg3 "); 1133 Serial.println(Debug3); 1134 Debug1 = 0; 1135 Debug2 = 0; 1136 Debug3 = 0; 1137 } 1138 } 1139 1140 1141 // print the string when a newline arrives: 1142 if (stringComplete) { 1143 // Serial.println(inputString); 1144 ProcessaComandoSeriale(); 1145 // clear the string: 1146 inputString = ""; 1147 stringComplete = false; 1148 AggiornaStatoMotori(); 1149 } 1150 1151 if (FlagKeyPressed) { //FlagKeyPressed == true 1152 1153 1154 if (DebugMillis) 1155 { 1156 Serial.println(millis()); 1157 Serial.print(" dbg1 "); 1158 Serial.println(Debug1); 1159 Serial.print(" dbg2 "); 1160 Serial.println(Debug2); 1161 Serial.print(" dbg3 "); 1162 Serial.println(Debug3); 1163 } 1164 1165 1166 //------------- blinka built led per segnalare live 1167 if ( TimeBlink < millis() ) { 1168 TimeBlink = millis(); 1169 } 1170 1171 1172 1173 if ( millis() > TimeLastKeyPressed ) 1174 { // - IF ( (Millis + TimeoutB) > TimeLastKeyPressed ) 1175 Debug3 = millis(); 1176 FlagKeyPressed = false; 1177 1178 //Serial.println("#A "); 1179 1180 if (ReleType[KeyPressed] == true) //true bistabile toggle 1181 { 1182 //Serial.println("#B "); 1183 1184 // ReleType true = bistabile 1185 if (ReleToggle[KeyPressed] == true) 1186 { 1187 //Serial.println("#C "); 1188 ReleToggle[KeyPressed] = false; 1189 } 1190 else 1191 { 1192 //Serial.println("#D "); 1193 ReleToggle[KeyPressed] = true; 1194 } 1195 } 1196 else 1197 { 1198 //ReleType false = MONOSTABILE 1199 DisAttivaRele(KeyPressed); 1200 //Serial.println("#E "); 1201 1202 } 1203 } 1204 } 1205 1206 1207 1208 if (CheckCodiceIr() == true) { 1209 PreProcessaTasto(); 1210 } 1211 1212 1213} 1214 1215 1216 1217 1218//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 1219void AggiornaStatoMotori() { 1220 //si inizia stampando gli stati 1221 1222 if (DIREZIONE1) { 1223 digitalWrite(MOT1_AVANTI, HIGH); 1224 digitalWrite(MOT1_INDIETRO, LOW); 1225 } 1226 else { 1227 digitalWrite(MOT1_AVANTI, LOW); 1228 digitalWrite(MOT1_INDIETRO, HIGH); 1229 } 1230 analogWrite(Pin_PWM_Mot1, PWM1); 1231 1232 Serial.println(" "); 1233 Serial.print("PWM1="); 1234 Serial.println(PWM1); 1235 1236 Serial.print("DIR1="); 1237 Serial.println(DIREZIONE1); 1238 1239 Serial.print("PWM2="); 1240 Serial.println(PWM2); 1241 1242 Serial.print("DIR2="); 1243 Serial.println(DIREZIONE2); 1244 1245 Serial.print("MOTN="); 1246 Serial.println(MOTORON); 1247 1248} 1249 1250 1251//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 1252 1253 1254 1255 1256//ricevuto codice GO o seriale M5 1257void GoMotorOn() { 1258 1259 // SOLO SE è IN MOTOR OFF SI ESEGUE 1260 if (MOTORON == false) { 1261 PWM1 = 0; 1262 Serial.println( "!!! PWM1 = 0"); 1263 delay(100); 1264 MOTORON = true; 1265 } 1266 1267 1268} 1269 1270// * * * * * 1271 1272//ricevuto codice ^ o seriale M8 che aumenta velocità di PWM1 e DIREZIONE1 a true 1273void Pwm1Up() { 1274 1275 if (MOTORON) { 1276 //se pwm1 è 0 regola la direzione 1277 if ( PWM1 == 0 ) { 1278 DIREZIONE1 = true; 1279 } 1280 1281 if (DIREZIONE1) { 1282 //siamo già nella direzione giusta 1283 // controlla se PWM ha ancora capienza per sommare Accelera e non dare il giro oltre 255 1284 if ( (PWM1 + Accelera) > 255 ) { 1285 // da il giro, satura a 255 1286 PWM1 = 255; 1287 } 1288 else { 1289 //non da il giro, si somma 1290 PWM1 = PWM1 + Accelera; 1291 } 1292 1293 } 1294 else { 1295 //siamo nella direzione sbagliata 1296 // controlla se PWM puo ancora essere decrementato di Accelera e non dare il giro sotto lo 0 1297 if ( (PWM1 - Accelera) < 1 ) { 1298 // da il giro, si satura a 0 e si inverte la direzione 1299 PWM1 = 0; 1300 Serial.println( "!!! PWM1 = 0"); 1301 DIREZIONE1 = true; 1302 } 1303 else { 1304 // non da il giro, si sottrae 1305 PWM1 = PWM1 - Accelera; 1306 } 1307 1308 1309 } // DIREZIONE1 == false 1310 1311 } //)MOTORON) 1312 1313} 1314 1315 1316//ricevuto codice v o seriale M2 che diminuisce e inverte velocità di PWM1 e DIREZIONE1 a false 1317void Pwm1Down() { 1318 1319 if (MOTORON) { 1320 //se pwm1 è 0 regola la direzione 1321 if ( PWM1 == 0 ) { 1322 DIREZIONE1 = false; 1323 } 1324 1325 if (DIREZIONE1 == false) { 1326 //siamo già nella direzione giusta 1327 // controlla se PWM ha ancora capienza per sommare Accelera e non dare il giro oltre 255 1328 if ( (PWM1 + Accelera) > 255 ) { 1329 // da il giro, satura a 255 1330 PWM1 = 255; 1331 } 1332 else { 1333 //non da il giro, si somma 1334 PWM1 = PWM1 + Accelera; 1335 } 1336 1337 } 1338 else { 1339 //siamo nella direzione sbagliata 1340 // controlla se PWM puo ancora essere decrementato di Accelera e non dare il giro sotto lo 0 1341 if ( (PWM1 - Accelera) < 1 ) { 1342 // da il giro, si satura a 0 e si inverte la direzione 1343 PWM1 = 0; 1344 Serial.println( "!!! PWM1 = 0"); 1345 DIREZIONE1 = true; 1346 } 1347 else { 1348 // non da il giro, si sottrae 1349 PWM1 = PWM1 - Accelera; 1350 } 1351 1352 1353 } // DIREZIONE1 == false 1354 1355 } //)MOTORON) 1356 1357} 1358 1359 1360//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 1361 1362 1363void Pwm2Up() {}; 1364 1365//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 1366 1367 1368void Pwm2Down() {}; 1369 1370//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 1371 1372void Boost130() { 1373 //v19, boost solo se in drvon 1374 if (MOTORON == true) { 1375 int SavePWM; 1376 Serial.println( "Boost1");// il quinto carattere accende e soegne la spia 1377 SavePWM = PWM1; 1378 PWM1 = 255; 1379 AggiornaStatoMotori(); 1380 delay(100); 1381 PWM1 = SavePWM; 1382 AggiornaStatoMotori(); 1383 Serial.println( "Boost0"); 1384 } 1385} 1386 1387//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 1388 1389void EmergencyStop() { 1390 PWM1 = 0; // fermata di emergenza 1391 Serial.println( "Emergency STOP"); 1392 MOTORON = false; 1393 AggiornaStatoMotori(); 1394} 1395 1396//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 1397 1398/* 1399 SerialEvent occurs whenever a new data comes in the 1400 hardware serial RX. This routine is run between each 1401 time loop() runs, so using delay inside loop can delay 1402 response. Multiple bytes of data may be available. 1403*/ 1404void serialEvent() { 1405 while (Serial.available()) { 1406 // get the new byte: 1407 char inChar = (char)Serial.read(); 1408 // add it to the inputString: 1409 inputString += inChar; 1410 // if the incoming character is a newline, set a flag 1411 // so the main loop can do something about it: 1412 if (inChar == '\n') { 1413 stringComplete = true; 1414 } 1415 } 1416} 1417
Downloadable files
REMOTINO NANO Schematic Diagram
This is complete schematic of REMOTINO NANO
REMOTINO NANO Schematic Diagram
REMOTINO NANO Schematic Diagram
This is complete schematic of REMOTINO NANO
REMOTINO NANO Schematic Diagram
Comments
Only logged in users can leave comments