Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.81 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3.  
  4. LiquidCrystal_I2C lcd(0x27, 16, 2);
  5. #include <SPI.h>
  6. #include <nRF24L01.h>
  7. #include <RF24.h>
  8. #include <Bounce2.h>
  9. #define button1 4
  10. #define button2 6
  11. #define osaY 14
  12. #define osaX 15
  13. byte upButton = 16;
  14. byte downButton = 17;
  15. byte selectButton = 5;
  16. byte menu = 1;
  17. byte sensorValue= 0;
  18. int period = 2000;
  19. unsigned long time_now = 0;
  20. byte LedRedPin = 4;
  21. byte counter = 0;
  22. byte motor1 = 0;
  23. const int LED1 = 3;
  24. const int LED2 = 2;
  25. const int LEDk = 4;
  26. int LED1_state = LOW;
  27. int LED2_state = LOW;
  28. Bounce debouncer1;
  29. Bounce debouncer2;
  30.  
  31.  
  32. const uint64_t my_radio_pipes[2] = {0xE8E8F0F0E1LL, 0xE8E8F0F0D2LL}; //Remember that this code should be the same for the receiver
  33.  
  34. RF24 radio(9, 10);
  35.  
  36. /**************struktura odesílaných dat************************************/
  37. // The sizeof this struct should not exceed 32 bytes
  38. struct Data_to_be_sent {
  39.   byte ch1; //osa Y
  40.   bool ch2; // LED1
  41.   byte ch3; //osa X
  42.   byte ch4; //LED2
  43.   byte ch5; //kontrolní, posílá 123
  44. };
  45. Data_to_be_sent sent_data;
  46.  
  47. /**************struktura přijímaných dat************************************/
  48. struct Data_to_be_received {
  49.   byte ch1;
  50.   byte ch2;
  51.   byte ch5; //kontrolní, posílá 123
  52. };
  53. Data_to_be_received received_data;
  54.  
  55. void setup()
  56. {
  57.   Serial.begin(9600);
  58.   pinMode(button1, INPUT_PULLUP);
  59.   pinMode(LED1, OUTPUT);
  60.   pinMode(button2, INPUT_PULLUP);
  61.   pinMode(LED2, OUTPUT);
  62.   pinMode(LEDk, OUTPUT);
  63.   lcd.begin();
  64.   lcd.backlight();
  65.   pinMode(upButton, INPUT_PULLUP);
  66.   pinMode(downButton, INPUT_PULLUP);
  67.   pinMode(selectButton, INPUT_PULLUP);
  68.   pinMode(LedRedPin, OUTPUT);
  69. /**************pro tlačítka************************************/
  70.   debouncer1.attach(button1);
  71.   debouncer1.interval(5);
  72.   debouncer2.attach(button2);
  73.   debouncer2.interval(5);
  74.  
  75.   radio.begin();
  76.   radio.setAutoAck(1);
  77.   radio.setDataRate(RF24_250KBPS);
  78.   radio.setPALevel(RF24_PA_MIN);
  79.  
  80.  
  81.   radio.openWritingPipe(my_radio_pipes[0]);
  82.   radio.openReadingPipe(1, my_radio_pipes[1]);
  83.   updateMenu();
  84. }
  85.  
  86. /**************************************************/
  87.  
  88.  
  89. void loop()
  90. {
  91.  
  92. //  Serial.begin(9600);
  93.   time_now = millis();
  94. /*************první řádek LCD***************************************************/  
  95.  {
  96.   lcd.setCursor(0, 0);
  97.   lcd.print(millis()/200);
  98. /*************tlačítka***************************************************/  
  99.  }  
  100.   if (!digitalRead(downButton)){
  101.     lcd.backlight();
  102.     menu++;
  103.     updateMenu();
  104.     while (!digitalRead(downButton));
  105.     while(millis() < time_now + period/10);
  106.   }
  107.   if (!digitalRead(upButton)){
  108.     lcd.backlight();
  109.     menu--;
  110.     updateMenu();
  111.     while(!digitalRead(upButton));
  112.     while(millis() < time_now + period/10);
  113.  
  114.   }
  115.   if (!digitalRead(selectButton)){
  116.     lcd.backlight();
  117.     lcd.clear();
  118.     executeAction();
  119.     updateMenu();
  120.     while (!digitalRead(selectButton));
  121.     while(millis() < time_now + period/2);
  122.   }
  123.  
  124.  
  125.  
  126. /***********************LED1 bounce2***********************/
  127. {
  128.   radio.stopListening();
  129.   debouncer1.update();
  130.   if (debouncer1.fell())
  131.     LED1_state = !LED1_state;
  132.  
  133. //  digitalWrite(LED1, LED1_state); //pro výstup na vysilači
  134. }
  135. /***********************LED1 bounce2***********************/
  136. {
  137.   debouncer2.update();
  138.   if (debouncer2.fell())
  139.     LED2_state = !LED2_state;
  140.  
  141. //  digitalWrite(LED2, LED2_state); //pro výstup na vysilači
  142. }
  143.  
  144. /***********************kontrolní na ch5***********************/
  145.  if (received_data.ch5 == 123) {
  146.     digitalWrite(LEDk, HIGH);
  147.   }
  148.   else {
  149.     digitalWrite(LEDk, LOW);
  150.   }
  151.  
  152.  
  153.  
  154. /***********************joystick***********************/
  155.   sent_data.ch1 = map( analogRead(osaY), 0, 1024, 0, 255);
  156.   sent_data.ch2 = !LED1_state;
  157.   sent_data.ch3 = map( analogRead(osaX), 0, 1024, 0, 255);
  158.   sent_data.ch4 = !LED2_state;
  159.   radio.write(&sent_data, sizeof(Data_to_be_sent));
  160.  
  161.  
  162. /***********************poslouchani***********************/
  163. radio.startListening();
  164. while (!radio.available());
  165. radio.read(&received_data, sizeof(Data_to_be_received));
  166. if (received_data.ch1 == 0) {
  167.     digitalWrite(LED2, LOW);
  168.   }
  169.   else {
  170.     digitalWrite(LED2, HIGH);
  171.   }
  172. if (received_data.ch2 == 1) {
  173.     digitalWrite(LED1, LOW);
  174.   }
  175.   else {
  176.     digitalWrite(LED1, HIGH);
  177.   }
  178. }
  179.  
  180. /**********************serial monitor*******************/  
  181. /*  Serial.print(received_data.ch1);
  182.   Serial.print(received_data.ch2);
  183.   Serial.print(sent_data.ch1);
  184.   Serial.print(sent_data.ch2);
  185.   Serial.print(sent_data.ch3);
  186.   Serial.print(sent_data.ch4);
  187.   Serial.println(sent_data.ch5);
  188.  
  189. }*/
  190. /**********************struktura menu*******************/
  191. void updateMenu() {
  192.   switch (menu) {
  193.     case 0:
  194.       menu = 1;
  195.       break;
  196.     case 1:
  197.       lcd.setCursor(0, 1);
  198.       lcd.print("                ");
  199.       lcd.setCursor(0, 1);
  200.       lcd.print("> MENU 1");
  201.       break;
  202.     case 2:
  203.       lcd.setCursor(0, 1);
  204.       lcd.print("                ");
  205.       lcd.setCursor(0, 1);
  206.       lcd.print("> MENU 2");
  207.       break;
  208.     case 3:
  209.       lcd.setCursor(0, 1);
  210.       lcd.print("                ");
  211.       lcd.setCursor(0, 1);
  212.       lcd.print("> Osvetleni  O P");
  213.       break;
  214.     case 4:
  215.       lcd.setCursor(0, 1);
  216.       lcd.print("                ");
  217.       lcd.setCursor(0, 1);
  218.       lcd.print("> Osvetleni  D V");
  219.       break;
  220.     case 5:
  221.       lcd.setCursor(0, 1);
  222.       lcd.print("                ");
  223.       lcd.setCursor(0, 1);
  224.       lcd.print("> Majaky");
  225.       break;  
  226.     case 6:
  227.       lcd.setCursor(0, 1);
  228.       lcd.print("                ");
  229.       lcd.setCursor(0, 1);
  230.       lcd.print("> Motor #1");
  231.       break;
  232.     case 7:
  233.       lcd.setCursor(0, 1);
  234.       lcd.print("                ");
  235.       lcd.setCursor(0, 1);
  236.       lcd.print("> Motor #2");
  237.       break;
  238.     case 8:
  239.       lcd.setCursor(0, 1);
  240.       lcd.print("                ");
  241.       lcd.setCursor(0, 1);
  242.       lcd.print("> Diferencial");
  243.       break;    
  244.     case 9:
  245.       lcd.setCursor(0, 1);
  246.       lcd.print("                ");
  247.       lcd.setCursor(0, 1);
  248.       lcd.print("> Volba RC model");
  249.       break;
  250.     case 10:
  251.       lcd.setCursor(0, 1);
  252.       lcd.print("                ");
  253.       lcd.setCursor(0, 1);
  254.       lcd.print("> VYPNOUT LCD");
  255.       break;
  256.     case 11:
  257.       menu = 10;
  258.       break;
  259.   }
  260. }
  261. /**********************akce menu*******************/
  262. void executeAction() {
  263.   switch (menu) {
  264.     case 1:
  265.       action1();
  266.       lcd.clear();
  267.       break;
  268.     case 2:
  269.       action2();
  270.       lcd.clear();
  271.       break;
  272.     case 3:
  273.       action3();
  274.       lcd.clear();
  275.       break;
  276.     case 4:
  277.       action4();
  278.       lcd.clear();
  279.       break;
  280.      case 5:
  281.       action5();
  282.       lcd.clear();
  283.       break;
  284.      case 6:
  285.       action6();
  286.       lcd.clear();
  287.       break;
  288.      case 7:
  289.       action7();
  290.       lcd.clear();
  291.       break;
  292.      case 8:
  293.       action8();
  294.       lcd.clear();
  295.       break;
  296.      case 9:
  297.       action9();
  298.       lcd.clear();
  299.       break;
  300.      case 10:
  301.       action10();
  302.       lcd.clear();
  303.       break;  
  304.   }
  305. }
  306. /**********************akce menu*******************/
  307. void action1() {
  308.   lcd.print("NEDOSTUPNE");
  309.   while(millis() < time_now + period);
  310. }
  311. void action2() {
  312.   while(millis() < time_now + period/5);
  313.   for(counter = 2; (!digitalRead(selectButton)) < 1;counter ++) { // zpět selectem
  314.   time_now = millis();
  315.   lcd.clear();
  316.   lcd.setCursor(0,0);
  317.   lcd.print("TX ");
  318.   lcd.print(counter/10); lcd.print("V "); lcd.print(counter/10); lcd.print("% "); lcd.print(counter/5);lcd.print((char)223);lcd.print("C");
  319.   lcd.setCursor(0,1);
  320.   lcd.print("RX ");
  321.   lcd.print(counter/8); lcd.print("V "); lcd.print(counter/4); lcd.print("% "); lcd.print(counter/3);lcd.print((char)223);lcd.print("C");
  322.   while(millis() < time_now + period/10);
  323.   }
  324. }
  325. void action3() {
  326.   while(millis() < time_now + period/5);
  327.   for(counter = 2; (!digitalRead(selectButton)) < 1;counter ++) { // zpět selectem
  328.   time_now = millis();
  329. //  lcd.clear();
  330.   lcd.setCursor(0,0);
  331.   lcd.print("+ OBRYSOVE");
  332.   lcd.setCursor(0,1);
  333.   lcd.print("- POTKAVACI");
  334.   {
  335.     if (digitalRead(upButton) == LOW) {
  336.     lcd.setCursor(0,0);
  337.     lcd.print("OBRYSOVE ON/OFF");  
  338.     digitalWrite(LedRedPin, HIGH);
  339.     while(millis() < time_now + period);
  340.     lcd.clear();
  341.     }
  342.     if (digitalRead(downButton) == LOW){
  343.       lcd.setCursor(0,1);
  344.       lcd.print("POTKAVACI ON/OFF");
  345.       while(millis() < time_now + period);
  346.       digitalWrite(LedRedPin, HIGH);
  347.       lcd.clear();
  348.       }
  349.     if ((digitalRead(downButton) == HIGH) && (digitalRead(upButton) == HIGH)){
  350.       digitalWrite(LedRedPin, LOW);
  351.       }
  352.       }
  353.   while(millis() < time_now + period/10);
  354.     Serial.println(digitalRead(upButton));  
  355.   }
  356. }
  357. void action4() {
  358.   while(millis() < time_now + period/5);
  359.   for(counter = 2; (!digitalRead(selectButton)) < 1;counter ++) { // zpět selectem
  360.   time_now = millis();
  361.   lcd.setCursor(0,0);
  362.   lcd.print("+ DALKOVE");
  363.   lcd.setCursor(0,1);
  364.   lcd.print("- VYSTRAZNE");
  365.   {
  366.     if (digitalRead(upButton) == LOW) {
  367.     lcd.setCursor(0,0);
  368.     lcd.print("DALKOVE ON/OFF");  
  369.     digitalWrite(LedRedPin, HIGH);
  370.     while(millis() < time_now + period);
  371.     lcd.clear();
  372.     }
  373.     if (digitalRead(downButton) == LOW){
  374.       lcd.setCursor(0,1);
  375.       lcd.print("VYSTRAZNE ON/OFF");
  376.       while(millis() < time_now + period);
  377.       digitalWrite(LedRedPin, HIGH);
  378.       lcd.clear();
  379.       }
  380.     if ((digitalRead(downButton) == HIGH) && (digitalRead(upButton) == HIGH)){
  381.       digitalWrite(LedRedPin, LOW);
  382.       }
  383.       }
  384.   while(millis() < time_now + period/10);
  385.     Serial.println(digitalRead(upButton));  
  386.   }
  387. }
  388. void action5() {
  389.   lcd.print(">Executing #5");
  390.   while(millis() < time_now + period);
  391.   lcd.clear();
  392. }
  393. void action6() {
  394.   while(millis() < time_now + period/5);
  395.   for(counter = 2; (!digitalRead(selectButton)) < 1;counter ++) { // zpět selectem
  396.   time_now = millis();
  397.   lcd.setCursor(0,0);
  398.   lcd.print("+ MOTOR#1 >");
  399.   lcd.setCursor(0,1);
  400.   lcd.print("- MOTOR#1 <");
  401.   {
  402.     if (digitalRead(upButton) == LOW) {
  403.     lcd.setCursor(0,0);
  404.     lcd.print("> MOTOR#1 >>>");
  405.     lcd.setCursor(0,1);
  406.     lcd.print("  v provozu");
  407.     motor1 = 1;
  408.     }
  409.     if (digitalRead(downButton) == LOW){  
  410.       lcd.setCursor(0,0);
  411.       lcd.print("< MOTOR#1 <<<");
  412.       lcd.setCursor(0,1);
  413.       lcd.print("  v provozu");
  414.       motor1 = 2;
  415.       }  
  416.      {
  417.     if ((digitalRead(downButton) == HIGH) && (digitalRead(upButton) == HIGH)){
  418.       lcd.setCursor(0,0);
  419.       lcd.print("+ MOTOR#1 >   ");
  420.       lcd.setCursor(0,1);
  421.       lcd.print("- MOTOR#1 <   ");
  422.       motor1 = 0;
  423.       }  
  424.      }
  425. }
  426.   while(millis() < time_now + period/10);
  427.     Serial.println(motor1);  
  428.   }
  429. }
  430. void action7() {
  431.   while(millis() < time_now + period/5);
  432.   for(counter = 2; (!digitalRead(selectButton)) < 1;counter ++) { // zpět selectem
  433.   time_now = millis();
  434.   lcd.setCursor(0,0);
  435.   lcd.print("+ MOTOR#2 >");
  436.   lcd.setCursor(0,1);
  437.   lcd.print("- MOTOR#2 <");
  438.   {
  439.     if (digitalRead(upButton) == LOW) {
  440. //    lcd.clear();
  441.     lcd.setCursor(0,0);
  442.     lcd.print("> MOTOR#2 >>>");
  443.     lcd.setCursor(0,1);
  444.     lcd.print("  v provozu");
  445.     motor1 = 1;
  446.     }
  447.     if (digitalRead(downButton) == LOW){
  448. //      lcd.clear();
  449.       lcd.setCursor(0,0);
  450.       lcd.print("< MOTOR#2 <<<");
  451.       lcd.setCursor(0,1);
  452.       lcd.print("  v provozu");
  453.       motor1 = 2;
  454.       }  
  455.      {
  456.     if ((digitalRead(downButton) == HIGH) && (digitalRead(upButton) == HIGH)){
  457.       motor1 = 0;
  458.       lcd.clear();
  459.       }  
  460.      }
  461. }
  462.   while(millis() < time_now + period/10);
  463.     Serial.println(motor1);  
  464.   }
  465. }
  466. void action8() {
  467.   while(millis() < time_now + period/5);
  468.   for(counter = 2; (!digitalRead(selectButton)) < 1;counter ++) { // zpět selectem
  469.   time_now = millis();
  470.   lcd.setCursor(0,0);
  471.   lcd.print("+ ZAPNOUT");
  472.   lcd.setCursor(0,1);
  473.   lcd.print("- VYPNOUT");
  474.   {
  475.     if (digitalRead(upButton) == LOW) {
  476.     lcd.clear();
  477.     lcd.setCursor(0,0);
  478.     lcd.print("UZAVERKA DIF.");
  479.     lcd.setCursor(0,1);
  480.     lcd.print("ZAPNUTA");  
  481.     digitalWrite(LedRedPin, HIGH);
  482.     while(millis() < time_now + period);
  483.     lcd.clear();
  484.     }
  485.     if (digitalRead(downButton) == LOW) {
  486.     lcd.clear();
  487.     lcd.setCursor(0,0);
  488.     lcd.print("UZAVERKA DIF.");
  489.     lcd.setCursor(0,1);
  490.     lcd.print("VYPNUTA");  
  491.     digitalWrite(LedRedPin, HIGH);
  492.     while(millis() < time_now + period);
  493.     lcd.clear();
  494.       }
  495.     if ((digitalRead(downButton) == HIGH) && (digitalRead(upButton) == HIGH)){
  496.       digitalWrite(LedRedPin, LOW);
  497.       }
  498.       }
  499.   while(millis() < time_now + period/10);
  500.     Serial.println(digitalRead(upButton));  
  501.   }
  502. }
  503. void action9() {
  504.   lcd.print("NEDOSTUPNE");
  505.   while(millis() < time_now + period);
  506.   lcd.clear();
  507. }
  508. void action10() {
  509.   lcd.clear();
  510.   lcd.noBacklight();
  511.     while(millis() < time_now + period);  
  512.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement