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 and the microswitch is open I want to run the motor until it reaches a specific measurement, after that the switch will be closed, and then to again run it only after the microswitch is again open
Problem:
I've prepared an example in the code to simplify:
I have an issue because for loop seems to be executing constantly although I've changed the state. The loop should execute 2 times, put the sensor in LOW, wait for the sensor to be again HIGH, and again to repeat the same code. Just search in the code for for (int i=0; i < 2; i++) {
Code:
#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 != '*') {
limitSwitch.loop();
state = limitSwitch.getState();
key = customKeypad.getKey();
if (state == HIGH) {
for (int i=0; i < 2; i++) {
Serial.println('works');
}
state = 0;
// for (int i = 0; i < calculationForTheMovement ; i++) {
// delay(1000);
// digitalWrite(stepPin, HIGH);
// delayMicroseconds(500);
// digitalWrite(stepPin, LOW);
// delayMicroseconds(500);
// }
}
}
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;
}
}