Hi,
I'm making a project with a friend of mine, I will try shortly to explain the project.
Project context:
I have a matrix keyboard, motor, micro switch, and led screen.
The motor needs to pull the paper, based on the input that the user gives, to confirm the input user needs to press # on the keypad. Once the keypad input is pressed I want to run the motor over and over if the microswitch is open (using a while loop), and stop it if it is not open, the loop will run until "*" is pressed on the keyboard.
Problem:
Being in the while loop I'm trying to get the state of the microswitch (Serial.println(state);), but somehow it always returns HIGH, even though when the microswitch is closed. Seems while loop is not updating back the state of the microswitch, and I don't know how to fix it. Here is the whole code. Just search for while (key != '*')
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <ezButton.h> // mikroprekidac biblioteka
#define dirPin 12 // step motor
#define stepPin 13 // step motor
#define stepsPerRevolution 1600 // revolucije step motora
LiquidCrystal_I2C lcd(0x27,16,2); // ekran
String readKeypadInput;
char key;
int state;
ezButton limitSwitch(11); // mikroprekidac
const byte ROWS = 4; // Define broj redova od tastature
const byte COLS = 4; // Define broj kolona od tastature
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {6, 5, 4, 3}; //konektovani pinovi od tastature (redovi)
byte colPins[COLS] = {10, 9, 8, 7}; //konektovani pinovi od tastature (kolone)
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
limitSwitch.setDebounceTime(50);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
lcd.init();
lcd.clear();
lcd.backlight();
}
void loop() {
limitSwitch.loop();
lcd.setCursor(0,0); // definisi prvi potez na ekranu
lcd.print("Zadaj duzinu koraka u mm:");
// Dodaj u buducnosti za ekran
// Da se zada broj koraka; (npr 3)
// Da se zada duzina svakog koraka (npr 100mm)
// Da se zada ponavljanje svakog koraka (3x npr)
readKeypad();
if (key == '#') {
digitalWrite(dirPin, HIGH);
float currentMovement = 170; // trenutni pomeraj motora po jednoj revoluciji
float wantedMovement = readKeypadInput.toInt(); // zeljeno pomeranje motora
float calculationForTheMovement = (stepsPerRevolution * wantedMovement) / currentMovement ;
while (key != '*') {
state = limitSwitch.getState();
key = customKeypad.getKey();
Serial.println(state);
// if (state == HIGH) {
// for (int i = 0; i < 2 ; i++) {
// Serial.println("RADI");
// }
// // for (int i = 0; i < calculationForTheMovement ; i++) {
// // delay(1000);
// // digitalWrite(stepPin, HIGH);
// // delayMicroseconds(500);
// // digitalWrite(stepPin, LOW);
// // delayMicroseconds(500);
// // }
// state == LOW;
// } else {
// Serial.println("spusten prekidac");
// }
}
reset();
} else {
lcd.setCursor(0, 1);
lcd.print(readKeypadInput);
}
if (key == '*') {
reset();
}
}
void reset() {
readKeypadInput = "";
lcd.clear();
}
void readKeypad() {
key = customKeypad.getKey();
if (key != '#' ) {
String storeKeypadInput = String(key);
readKeypadInput += storeKeypadInput;
}
}