zdenekpetrzd

Untitled

Mar 22nd, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. IPAddress ip(1, 1, 1, 125);
  3. IPAddress gateway(1, 1, 1, 1);
  4. IPAddress subnet(255, 255, 255, 0);
  5.  
  6. String ssid = "NAG";
  7. String heslo = "";
  8.  
  9. int led1 = D5;
  10. int tlacitko = D8;
  11. int pinMotor[] = {16, 5, 4, 0};
  12.  
  13. int delkaKroku = 500;
  14.  
  15. int buttonState = 0; // current state of the button
  16. int lastButtonState = 0; // previous state of the button
  17. int startPressed = 0; // the time button was pressed
  18. int endPressed = 0; // the time button was released
  19. int timeHold = 0; // the time button is hold
  20. int timeReleased = 0; // the time button is released
  21. bool pripojOdpoj = false;
  22. int cas1;
  23. int cas2;
  24. int cas3;
  25. int poziceMot[8][4] = {
  26. {0,0,0,1},
  27. {0,0,1,1},
  28. {0,0,1,0},
  29. {0,1,1,0},
  30. {0,1,0,0},
  31. {1,1,0,0},
  32. {1,0,0,0},
  33. {1,0,0,1},
  34. };
  35.  
  36. void otaceni(int smer) {
  37. while(smer > 0) {
  38. for(int i = 0; i < 8; i++) {
  39. for(int d = 0; d < 4; d++) {
  40. digitalWrite(pinMotor[d], poziceMot[i][d]);
  41. }
  42. delay(1);
  43. }
  44. smer--;
  45. }
  46. while(smer < 0){
  47. for(int i = 7; i >= 0; i--) {
  48. for(int d = 0; d < 4; d++) {
  49. digitalWrite(pinMotor[d], poziceMot[i][d]);
  50. }
  51. delay(1);
  52. }
  53. smer++;
  54. }
  55. }
  56.  
  57. void pripojSeNaWIFI() {
  58. WiFi.config(ip, gateway, subnet);
  59. // Připojení k wifi síti
  60. WiFi.begin(ssid,heslo);
  61. Serial.print("Připojuji se k wifi síti");
  62. while (WiFi.status() != WL_CONNECTED)
  63. {
  64. delay(500);
  65. Serial.print(".");
  66. }
  67. Serial.println();
  68. Serial.print("Připojeno k WIFI, IP: : ");
  69. Serial.println(WiFi.localIP());
  70. }
  71.  
  72.  
  73. void kontrolaTlacitka() {
  74. buttonState = digitalRead(tlacitko);
  75. // button state changed
  76. if (buttonState != lastButtonState) {
  77. // the button was just pressed
  78. if (buttonState == HIGH) {
  79. startPressed = millis();
  80. timeReleased = startPressed - endPressed;
  81. if (timeReleased >= 1000) {
  82. Serial.print("Tlačítko stisknuto na více než 1 sekundu, ");
  83. pripojOdpoj = !pripojOdpoj;
  84. if(pripojOdpoj == true) {
  85. pripojSeNaWIFI();
  86. }
  87. else {
  88. Serial.println("Odpojuji od wifi sítě");
  89. WiFi.disconnect();
  90. }
  91. }
  92. // the button was just released
  93. } else {
  94. endPressed = millis();
  95. timeHold = endPressed - startPressed;
  96. }
  97. }
  98. // save the current state as the last state,
  99. //for next time through the loop
  100. lastButtonState = buttonState;
  101. }
  102.  
  103. void setup() {
  104. // Inicializace Sériové linky
  105. Serial.begin(115200);
  106. Serial.println();
  107. WiFi.disconnect();
  108. for(int i = 0; i < 4; i++) {
  109. pinMode(pinMotor[i], OUTPUT);
  110. }
  111. pinMode(led1,OUTPUT);
  112. pinMode(tlacitko,INPUT_PULLUP);
  113.  
  114. }
  115.  
  116. void loop() {
  117. cas1 = millis();
  118. if(cas1-cas2 > delkaKroku) {
  119. otaceni(1);
  120. cas2 = millis();
  121. }
  122. if(WiFi.status() == WL_CONNECTED && cas1-cas3 > 500) {
  123. bool stavLed = !stavLed;
  124. digitalWrite(led1,stavLed);
  125. cas3 = millis();
  126. }
  127. kontrolaTlacitka();
  128. }
Advertisement
Add Comment
Please, Sign In to add comment