Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- #include <SPI.h>
- #include <nRF24L01.h>
- #include <RF24.h>
- #include <Bounce2.h>
- #define button1 4
- #define button2 6
- #define osaY 14
- #define osaX 15
- byte upButton = 16;
- byte downButton = 17;
- byte selectButton = 5;
- byte menu = 1;
- byte sensorValue= 0;
- int period = 2000;
- unsigned long time_now = 0;
- byte LedRedPin = 4;
- byte counter = 0;
- byte motor1 = 0;
- const int LED1 = 3;
- const int LED2 = 2;
- const int LEDk = 4;
- int LED1_state = LOW;
- int LED2_state = LOW;
- Bounce debouncer1;
- Bounce debouncer2;
- const uint64_t my_radio_pipes[2] = {0xE8E8F0F0E1LL, 0xE8E8F0F0D2LL}; //Remember that this code should be the same for the receiver
- RF24 radio(9, 10);
- /**************struktura odesílaných dat************************************/
- // The sizeof this struct should not exceed 32 bytes
- struct Data_to_be_sent {
- byte ch1; //osa Y
- bool ch2; // LED1
- byte ch3; //osa X
- byte ch4; //LED2
- byte ch5; //kontrolní, posílá 123
- };
- Data_to_be_sent sent_data;
- /**************struktura přijímaných dat************************************/
- struct Data_to_be_received {
- byte ch1;
- byte ch2;
- byte ch5; //kontrolní, posílá 123
- };
- Data_to_be_received received_data;
- void setup()
- {
- Serial.begin(9600);
- pinMode(button1, INPUT_PULLUP);
- pinMode(LED1, OUTPUT);
- pinMode(button2, INPUT_PULLUP);
- pinMode(LED2, OUTPUT);
- pinMode(LEDk, OUTPUT);
- lcd.begin();
- lcd.backlight();
- pinMode(upButton, INPUT_PULLUP);
- pinMode(downButton, INPUT_PULLUP);
- pinMode(selectButton, INPUT_PULLUP);
- pinMode(LedRedPin, OUTPUT);
- /**************pro tlačítka************************************/
- debouncer1.attach(button1);
- debouncer1.interval(5);
- debouncer2.attach(button2);
- debouncer2.interval(5);
- radio.begin();
- radio.setAutoAck(1);
- radio.setDataRate(RF24_250KBPS);
- radio.setPALevel(RF24_PA_MIN);
- radio.openWritingPipe(my_radio_pipes[0]);
- radio.openReadingPipe(1, my_radio_pipes[1]);
- updateMenu();
- }
- /**************************************************/
- void loop()
- {
- // Serial.begin(9600);
- time_now = millis();
- /*************první řádek LCD***************************************************/
- {
- lcd.setCursor(0, 0);
- lcd.print(millis()/200);
- /*************tlačítka***************************************************/
- }
- if (!digitalRead(downButton)){
- lcd.backlight();
- menu++;
- updateMenu();
- while (!digitalRead(downButton));
- while(millis() < time_now + period/10);
- }
- if (!digitalRead(upButton)){
- lcd.backlight();
- menu--;
- updateMenu();
- while(!digitalRead(upButton));
- while(millis() < time_now + period/10);
- }
- if (!digitalRead(selectButton)){
- lcd.backlight();
- lcd.clear();
- executeAction();
- updateMenu();
- while (!digitalRead(selectButton));
- while(millis() < time_now + period/2);
- }
- /***********************LED1 bounce2***********************/
- {
- radio.stopListening();
- debouncer1.update();
- if (debouncer1.fell())
- LED1_state = !LED1_state;
- // digitalWrite(LED1, LED1_state); //pro výstup na vysilači
- }
- /***********************LED1 bounce2***********************/
- {
- debouncer2.update();
- if (debouncer2.fell())
- LED2_state = !LED2_state;
- // digitalWrite(LED2, LED2_state); //pro výstup na vysilači
- }
- /***********************kontrolní na ch5***********************/
- if (received_data.ch5 == 123) {
- digitalWrite(LEDk, HIGH);
- }
- else {
- digitalWrite(LEDk, LOW);
- }
- /***********************joystick***********************/
- sent_data.ch1 = map( analogRead(osaY), 0, 1024, 0, 255);
- sent_data.ch2 = !LED1_state;
- sent_data.ch3 = map( analogRead(osaX), 0, 1024, 0, 255);
- sent_data.ch4 = !LED2_state;
- radio.write(&sent_data, sizeof(Data_to_be_sent));
- /***********************poslouchani***********************/
- radio.startListening();
- while (!radio.available());
- radio.read(&received_data, sizeof(Data_to_be_received));
- if (received_data.ch1 == 0) {
- digitalWrite(LED2, LOW);
- }
- else {
- digitalWrite(LED2, HIGH);
- }
- if (received_data.ch2 == 1) {
- digitalWrite(LED1, LOW);
- }
- else {
- digitalWrite(LED1, HIGH);
- }
- }
- /**********************serial monitor*******************/
- /* Serial.print(received_data.ch1);
- Serial.print(received_data.ch2);
- Serial.print(sent_data.ch1);
- Serial.print(sent_data.ch2);
- Serial.print(sent_data.ch3);
- Serial.print(sent_data.ch4);
- Serial.println(sent_data.ch5);
- }*/
- /**********************struktura menu*******************/
- void updateMenu() {
- switch (menu) {
- case 0:
- menu = 1;
- break;
- case 1:
- lcd.setCursor(0, 1);
- lcd.print(" ");
- lcd.setCursor(0, 1);
- lcd.print("> MENU 1");
- break;
- case 2:
- lcd.setCursor(0, 1);
- lcd.print(" ");
- lcd.setCursor(0, 1);
- lcd.print("> MENU 2");
- break;
- case 3:
- lcd.setCursor(0, 1);
- lcd.print(" ");
- lcd.setCursor(0, 1);
- lcd.print("> Osvetleni O P");
- break;
- case 4:
- lcd.setCursor(0, 1);
- lcd.print(" ");
- lcd.setCursor(0, 1);
- lcd.print("> Osvetleni D V");
- break;
- case 5:
- lcd.setCursor(0, 1);
- lcd.print(" ");
- lcd.setCursor(0, 1);
- lcd.print("> Majaky");
- break;
- case 6:
- lcd.setCursor(0, 1);
- lcd.print(" ");
- lcd.setCursor(0, 1);
- lcd.print("> Motor #1");
- break;
- case 7:
- lcd.setCursor(0, 1);
- lcd.print(" ");
- lcd.setCursor(0, 1);
- lcd.print("> Motor #2");
- break;
- case 8:
- lcd.setCursor(0, 1);
- lcd.print(" ");
- lcd.setCursor(0, 1);
- lcd.print("> Diferencial");
- break;
- case 9:
- lcd.setCursor(0, 1);
- lcd.print(" ");
- lcd.setCursor(0, 1);
- lcd.print("> Volba RC model");
- break;
- case 10:
- lcd.setCursor(0, 1);
- lcd.print(" ");
- lcd.setCursor(0, 1);
- lcd.print("> VYPNOUT LCD");
- break;
- case 11:
- menu = 10;
- break;
- }
- }
- /**********************akce menu*******************/
- void executeAction() {
- switch (menu) {
- case 1:
- action1();
- lcd.clear();
- break;
- case 2:
- action2();
- lcd.clear();
- break;
- case 3:
- action3();
- lcd.clear();
- break;
- case 4:
- action4();
- lcd.clear();
- break;
- case 5:
- action5();
- lcd.clear();
- break;
- case 6:
- action6();
- lcd.clear();
- break;
- case 7:
- action7();
- lcd.clear();
- break;
- case 8:
- action8();
- lcd.clear();
- break;
- case 9:
- action9();
- lcd.clear();
- break;
- case 10:
- action10();
- lcd.clear();
- break;
- }
- }
- /**********************akce menu*******************/
- void action1() {
- lcd.print("NEDOSTUPNE");
- while(millis() < time_now + period);
- }
- void action2() {
- while(millis() < time_now + period/5);
- for(counter = 2; (!digitalRead(selectButton)) < 1;counter ++) { // zpět selectem
- time_now = millis();
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("TX ");
- lcd.print(counter/10); lcd.print("V "); lcd.print(counter/10); lcd.print("% "); lcd.print(counter/5);lcd.print((char)223);lcd.print("C");
- lcd.setCursor(0,1);
- lcd.print("RX ");
- lcd.print(counter/8); lcd.print("V "); lcd.print(counter/4); lcd.print("% "); lcd.print(counter/3);lcd.print((char)223);lcd.print("C");
- while(millis() < time_now + period/10);
- }
- }
- void action3() {
- while(millis() < time_now + period/5);
- for(counter = 2; (!digitalRead(selectButton)) < 1;counter ++) { // zpět selectem
- time_now = millis();
- // lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("+ OBRYSOVE");
- lcd.setCursor(0,1);
- lcd.print("- POTKAVACI");
- {
- if (digitalRead(upButton) == LOW) {
- lcd.setCursor(0,0);
- lcd.print("OBRYSOVE ON/OFF");
- digitalWrite(LedRedPin, HIGH);
- while(millis() < time_now + period);
- lcd.clear();
- }
- if (digitalRead(downButton) == LOW){
- lcd.setCursor(0,1);
- lcd.print("POTKAVACI ON/OFF");
- while(millis() < time_now + period);
- digitalWrite(LedRedPin, HIGH);
- lcd.clear();
- }
- if ((digitalRead(downButton) == HIGH) && (digitalRead(upButton) == HIGH)){
- digitalWrite(LedRedPin, LOW);
- }
- }
- while(millis() < time_now + period/10);
- Serial.println(digitalRead(upButton));
- }
- }
- void action4() {
- while(millis() < time_now + period/5);
- for(counter = 2; (!digitalRead(selectButton)) < 1;counter ++) { // zpět selectem
- time_now = millis();
- lcd.setCursor(0,0);
- lcd.print("+ DALKOVE");
- lcd.setCursor(0,1);
- lcd.print("- VYSTRAZNE");
- {
- if (digitalRead(upButton) == LOW) {
- lcd.setCursor(0,0);
- lcd.print("DALKOVE ON/OFF");
- digitalWrite(LedRedPin, HIGH);
- while(millis() < time_now + period);
- lcd.clear();
- }
- if (digitalRead(downButton) == LOW){
- lcd.setCursor(0,1);
- lcd.print("VYSTRAZNE ON/OFF");
- while(millis() < time_now + period);
- digitalWrite(LedRedPin, HIGH);
- lcd.clear();
- }
- if ((digitalRead(downButton) == HIGH) && (digitalRead(upButton) == HIGH)){
- digitalWrite(LedRedPin, LOW);
- }
- }
- while(millis() < time_now + period/10);
- Serial.println(digitalRead(upButton));
- }
- }
- void action5() {
- lcd.print(">Executing #5");
- while(millis() < time_now + period);
- lcd.clear();
- }
- void action6() {
- while(millis() < time_now + period/5);
- for(counter = 2; (!digitalRead(selectButton)) < 1;counter ++) { // zpět selectem
- time_now = millis();
- lcd.setCursor(0,0);
- lcd.print("+ MOTOR#1 >");
- lcd.setCursor(0,1);
- lcd.print("- MOTOR#1 <");
- {
- if (digitalRead(upButton) == LOW) {
- lcd.setCursor(0,0);
- lcd.print("> MOTOR#1 >>>");
- lcd.setCursor(0,1);
- lcd.print(" v provozu");
- motor1 = 1;
- }
- if (digitalRead(downButton) == LOW){
- lcd.setCursor(0,0);
- lcd.print("< MOTOR#1 <<<");
- lcd.setCursor(0,1);
- lcd.print(" v provozu");
- motor1 = 2;
- }
- {
- if ((digitalRead(downButton) == HIGH) && (digitalRead(upButton) == HIGH)){
- lcd.setCursor(0,0);
- lcd.print("+ MOTOR#1 > ");
- lcd.setCursor(0,1);
- lcd.print("- MOTOR#1 < ");
- motor1 = 0;
- }
- }
- }
- while(millis() < time_now + period/10);
- Serial.println(motor1);
- }
- }
- void action7() {
- while(millis() < time_now + period/5);
- for(counter = 2; (!digitalRead(selectButton)) < 1;counter ++) { // zpět selectem
- time_now = millis();
- lcd.setCursor(0,0);
- lcd.print("+ MOTOR#2 >");
- lcd.setCursor(0,1);
- lcd.print("- MOTOR#2 <");
- {
- if (digitalRead(upButton) == LOW) {
- // lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("> MOTOR#2 >>>");
- lcd.setCursor(0,1);
- lcd.print(" v provozu");
- motor1 = 1;
- }
- if (digitalRead(downButton) == LOW){
- // lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("< MOTOR#2 <<<");
- lcd.setCursor(0,1);
- lcd.print(" v provozu");
- motor1 = 2;
- }
- {
- if ((digitalRead(downButton) == HIGH) && (digitalRead(upButton) == HIGH)){
- motor1 = 0;
- lcd.clear();
- }
- }
- }
- while(millis() < time_now + period/10);
- Serial.println(motor1);
- }
- }
- void action8() {
- while(millis() < time_now + period/5);
- for(counter = 2; (!digitalRead(selectButton)) < 1;counter ++) { // zpět selectem
- time_now = millis();
- lcd.setCursor(0,0);
- lcd.print("+ ZAPNOUT");
- lcd.setCursor(0,1);
- lcd.print("- VYPNOUT");
- {
- if (digitalRead(upButton) == LOW) {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("UZAVERKA DIF.");
- lcd.setCursor(0,1);
- lcd.print("ZAPNUTA");
- digitalWrite(LedRedPin, HIGH);
- while(millis() < time_now + period);
- lcd.clear();
- }
- if (digitalRead(downButton) == LOW) {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("UZAVERKA DIF.");
- lcd.setCursor(0,1);
- lcd.print("VYPNUTA");
- digitalWrite(LedRedPin, HIGH);
- while(millis() < time_now + period);
- lcd.clear();
- }
- if ((digitalRead(downButton) == HIGH) && (digitalRead(upButton) == HIGH)){
- digitalWrite(LedRedPin, LOW);
- }
- }
- while(millis() < time_now + period/10);
- Serial.println(digitalRead(upButton));
- }
- }
- void action9() {
- lcd.print("NEDOSTUPNE");
- while(millis() < time_now + period);
- lcd.clear();
- }
- void action10() {
- lcd.clear();
- lcd.noBacklight();
- while(millis() < time_now + period);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement