Advertisement
Akarapidoz

Arduino_button_stop

Jun 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. #include <Keypad.h>
  2. int ENB = 3;
  3. int IN4 = 4;
  4. int IN3 = 5; // Input3 подключен к выводу 5
  5. int i;
  6. int a; int m; int w; int kb;
  7.  
  8. unsigned long upTime; // время для ускорения 1000
  9. unsigned long currentTime; // сейчас
  10. unsigned long startTime; // время начала цыкла
  11. unsigned long endTime; // окончание цыкла
  12. const byte ROWS = 1; //four rows
  13. const byte COLS = 4; //four columns
  14. //define the cymbols on the buttons of the keypads
  15. char hexaKeys[ROWS][COLS] = {
  16.   {'1', '2', '3', '4'}
  17. };
  18. byte rowPins[ROWS] = {10}; //connect to the row pinouts of the keypad
  19. byte colPins[COLS] = {8, 9, 6, 7}; //connect to the column pinouts of the keypad
  20.  
  21. //initialize an instance of class NewKeypad
  22. Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
  23.  
  24. void setup() {
  25.  
  26.   Serial.begin(9600);              // инициируем подключение к COM-порту на скорости 9600 бот
  27.   pinMode (ENB, OUTPUT);
  28.   pinMode (IN3, OUTPUT);
  29.   pinMode (IN4, OUTPUT);// указываем тип клавиатуры
  30. }
  31.  
  32. void loop() {
  33.   char customKey = customKeypad.getKey();
  34.   int sensorValue1 = analogRead(A0);
  35.   int sensorValue2 = analogRead(A1);
  36.   // конвертирует аналоговое считывание (которое идет от 0-1023) в напряжение (0-5 вольт):
  37.  
  38.   float voltage1 = sensorValue1 / 34 + 25; // скорость на резисторе + минимум
  39.   float voltage2 = sensorValue2 * 20;//время работы*20
  40.  
  41.   // если нажимается   кнопка, то ...
  42.  
  43.   if (customKey) {
  44.     Serial.println(customKey == 49);
  45.     if (customKey == 49) {
  46.  
  47.       analogWrite(ENB, 0);
  48.       digitalWrite (IN3, HIGH);
  49.       digitalWrite (IN4, LOW);
  50.     }
  51.     if (customKey == 50) {
  52.       if (digitalRead(IN3) == HIGH || digitalRead(IN4) == HIGH) {
  53.         startTime = millis();
  54.         upTime = startTime + 1000;
  55.         currentTime = startTime;
  56.         endTime = startTime + voltage2;
  57.         i = 25;
  58.         w = 1;
  59.  
  60.         while (i <= voltage1 && customKey != 51 && endTime >= currentTime)
  61.         {
  62.           analogWrite(ENB, i);
  63.           customKey = customKeypad.getKey();
  64.           Serial.print(i); Serial.print(" - "); Serial.println(voltage2); Serial.println(customKey);
  65.  
  66.           if (i + 5 >= voltage1 && endTime >= currentTime) {
  67.             currentTime = millis();
  68.             // Serial.print(voltage2); Serial.print(" "); Serial.println(endTime-currentTime);
  69.           }
  70.           else {
  71.             currentTime = millis();
  72.             if (upTime <= currentTime) {
  73.               i += 5;
  74.               upTime += 1000;
  75.             }
  76.  
  77.           }
  78.  
  79.           if (endTime < currentTime || customKey == 51) {
  80.             stop();
  81.             return;
  82.           }
  83.  
  84.         }
  85.         w = 0;
  86.       }
  87.     }
  88.     if (customKey == 51) {
  89.       stop();
  90.     }
  91.  
  92.     if (customKey == 52) {
  93.       analogWrite(ENB, 0);
  94.       digitalWrite (IN3, LOW);
  95.       digitalWrite (IN4, HIGH);
  96.     }
  97.  
  98.  
  99.     Serial.println(customKey);
  100.   }
  101. }
  102. void stop() {
  103.   if (w == 1) {
  104.     for (a = 0; a <= 10; a++)
  105.     {
  106.       delay(1000);
  107.       m = i / 10;
  108.       i = i - m;
  109.       analogWrite(ENB, i);
  110.     }
  111.     w = 0;
  112.     analogWrite(ENB, 0);
  113.     digitalWrite (IN4, LOW);
  114.     digitalWrite (IN3, LOW);
  115.   }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement