Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Keypad.h>
- int ENB = 3;
- int IN4 = 4;
- int IN3 = 5; // Input3 подключен к выводу 5
- int i;
- int a; int m; int w; int kb;
- unsigned long upTime; // время для ускорения 1000
- unsigned long currentTime; // сейчас
- unsigned long startTime; // время начала цыкла
- unsigned long endTime; // окончание цыкла
- const byte ROWS = 1; //four rows
- const byte COLS = 4; //four columns
- //define the cymbols on the buttons of the keypads
- char hexaKeys[ROWS][COLS] = {
- {'1', '2', '3', '4'}
- };
- byte rowPins[ROWS] = {10}; //connect to the row pinouts of the keypad
- byte colPins[COLS] = {8, 9, 6, 7}; //connect to the column pinouts of the keypad
- //initialize an instance of class NewKeypad
- Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
- void setup() {
- Serial.begin(9600); // инициируем подключение к COM-порту на скорости 9600 бот
- pinMode (ENB, OUTPUT);
- pinMode (IN3, OUTPUT);
- pinMode (IN4, OUTPUT);// указываем тип клавиатуры
- }
- void loop() {
- char customKey = customKeypad.getKey();
- int sensorValue1 = analogRead(A0);
- int sensorValue2 = analogRead(A1);
- // конвертирует аналоговое считывание (которое идет от 0-1023) в напряжение (0-5 вольт):
- float voltage1 = sensorValue1 / 34 + 25; // скорость на резисторе + минимум
- float voltage2 = sensorValue2 * 20;//время работы*20
- // если нажимается кнопка, то ...
- if (customKey) {
- Serial.println(customKey == 49);
- if (customKey == 49) {
- analogWrite(ENB, 0);
- digitalWrite (IN3, HIGH);
- digitalWrite (IN4, LOW);
- }
- if (customKey == 50) {
- if (digitalRead(IN3) == HIGH || digitalRead(IN4) == HIGH) {
- startTime = millis();
- upTime = startTime + 1000;
- currentTime = startTime;
- endTime = startTime + voltage2;
- i = 25;
- w = 1;
- while (i <= voltage1 && customKey != 51 && endTime >= currentTime)
- {
- analogWrite(ENB, i);
- customKey = customKeypad.getKey();
- Serial.print(i); Serial.print(" - "); Serial.println(voltage2); Serial.println(customKey);
- if (i + 5 >= voltage1 && endTime >= currentTime) {
- currentTime = millis();
- // Serial.print(voltage2); Serial.print(" "); Serial.println(endTime-currentTime);
- }
- else {
- currentTime = millis();
- if (upTime <= currentTime) {
- i += 5;
- upTime += 1000;
- }
- }
- if (endTime < currentTime || customKey == 51) {
- stop();
- return;
- }
- }
- w = 0;
- }
- }
- if (customKey == 51) {
- stop();
- }
- if (customKey == 52) {
- analogWrite(ENB, 0);
- digitalWrite (IN3, LOW);
- digitalWrite (IN4, HIGH);
- }
- Serial.println(customKey);
- }
- }
- void stop() {
- if (w == 1) {
- for (a = 0; a <= 10; a++)
- {
- delay(1000);
- m = i / 10;
- i = i - m;
- analogWrite(ENB, i);
- }
- w = 0;
- analogWrite(ENB, 0);
- digitalWrite (IN4, LOW);
- digitalWrite (IN3, LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement