Hi all, I have an issue with my "while" statement on line 146.
The idea is that I have a keypad, when I push a button, I can enter a code, and when the code is right, I have some gear motor turning. While these are turning, I have 12 LEDs red turning to show that the motor is still not finished. When motors are finished, the red lights would turn off (not yet in the code), and some green one would light on.
Problem is that the red turning light (working fine if alone), part of ledloop() stop as soon as the motor is starting Closepiston().
I don't get how to write it so that they keep on turning until the motor are finished.
I have added one var1, and though that, i could change the variable at the end of the Closepiston() going back to the loop and with the changed value, the ledloop() would then stop. But still, same, the ledloop() actually stop as soon as Closepiston() starts ...
Here my code:
int var1=0;
// include the library keypad & LiquidCrystal:
#include <LiquidCrystal.h>
#include <Keypad.h>
//Define constantn for the keypad
const int ROW_NUM = 4;
const int COLUMN_NUM = 3;
//Matrix for the Keypad
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
//DEFINE pins for the keypad
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
//DEFINE code for the keypad
const String password = "1234"; // password
String input_password;
//Define matrix for colors
const int LED_1 = 21; //LED row 1
const int LED_2 = 19; //LED row 2
const int LED_3 = 20; //LED row 3
//Declare PIN for the green LEDS
const int LED_4 = 29;
// Declare pins for the LCD
const int rs = 30, en = 31, d4 = 35, d5 = 34, d6 = 33, d7 = 32;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#define LED 41
// PushbuttonOpen
const int buttonPinOpen = 40;
// PushbuttonClose
const int buttonPinClose = 41;
// variables will change:
int buttonStateOpen = 0;
int buttonStateClose = 0;
// Motor A connections
int enA = 12;
int in1 = 42;
int in2 = 43;
// Motor B connections
int enB = 13;
int in3 = 44;
int in4 = 45;
// Motor C connections
int enC = 10;
int in5 = 46;
int in6 = 47;
// Motor D connections
int enD = 11;
int in7 = 48;
int in8 = 49;
// Motor E connections
int enE = 1;
int in9 = 50;
int in10 = 51;
// Motor F connections
int enF = 2;
int in11 = 52;
int in12 = 53;
void setup(){
Serial.begin(9600);
input_password.reserve(32); // maximum input characters is 33, change if needed
pinMode(LED, OUTPUT);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Welcome");
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(enC, OUTPUT);
pinMode(enD, OUTPUT);
pinMode(enE, OUTPUT);
pinMode(enF, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(in5, OUTPUT);
pinMode(in6, OUTPUT);
pinMode(in7, OUTPUT);
pinMode(in8, OUTPUT);
pinMode(in9, OUTPUT);
pinMode(in10, OUTPUT);
pinMode(in11, OUTPUT);
pinMode(in12, OUTPUT);
// Turn off motors - Initial state
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
digitalWrite(in5, LOW);
digitalWrite(in6, LOW);
digitalWrite(in7, LOW);
digitalWrite(in8, LOW);
digitalWrite(in9, LOW);
digitalWrite(in10, LOW);
digitalWrite(in11, LOW);
digitalWrite(in12, LOW);
// Set motors to maximum speed
// For PWM maximum possible values are 0 to 255
analogWrite(enA, 255);
analogWrite(enB, 255);
analogWrite(enC, 255);
analogWrite(enD, 255);
analogWrite(enE, 255);
analogWrite(enF, 255);
}
void loop(){
char key = keypad.getKey();
// read the state of the pushbutton value:
buttonStateOpen = digitalRead(buttonPinOpen);
buttonStateClose = digitalRead(buttonPinClose);
if (buttonStateOpen == HIGH) {
if (key)
{
// lcd.clear();
// lcd.print("Type code");
//Serial.println(key);
// if(key == '*') {
// input_password = ""; // clear input password
// } else
if(key == '#')
{
if(password == input_password)
{
lcd.clear();
lcd.print("CORRECT");
//while loop to have red lights
while (var1 < 1) {
ledloop();
ClosePiston();
};
}
else
{
Serial.println("password is incorrect, try again");
lcd.clear();
lcd.print("INCORRECT");
}
input_password = ""; // clear input password
}
else
{
input_password += key; // append new character to input password string
lcd.clear();
lcd.print(input_password);
}
}
}
}
// This function lets you control spinning direction of motors
void ClosePiston() {
// Turn on motor A
lcd.clear();
lcd.print("Lock A");
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
delay(2000);
// Turn on motor B
lcd.clear();
lcd.print("Lock B");
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(2000);
// Turn on motor C
lcd.clear();
lcd.print("Lock C");
digitalWrite(in5, HIGH);
digitalWrite(in6, LOW);
delay(2000);
// Turn on motor D
lcd.clear();
lcd.print("Lock D");
digitalWrite(in7, HIGH);
digitalWrite(in8, LOW);
delay(2000);
// Turn on motor E
lcd.clear();
lcd.print("Lock E");
digitalWrite(in9, HIGH);
digitalWrite(in10, LOW);
delay(2000);
// Turn on motor F
lcd.clear();
lcd.print("Lock F");
digitalWrite(in11, HIGH);
digitalWrite(in12, LOW);
delay(2000);
// Now change motor directions
//digitalWrite(in1, LOW);
//digitalWrite(in2, HIGH);
//digitalWrite(in3, LOW);
//digitalWrite(in4, HIGH);
//delay(2000);
// Turn off motors
digitalWrite(in1, LOW);
delay(2000);
digitalWrite(in3, LOW);
delay(2000);
digitalWrite(in5, LOW);
delay(2000);
digitalWrite(in7, LOW);
delay(2000);
digitalWrite(in9, LOW);
delay(2000);
digitalWrite(in11, LOW);
lcd.clear();
lcd.print("All closed");
var1++;
pinMode(LED_4, OUTPUT);
digitalWrite(LED_4, HIGH);
}
void ledloop()
{
//turn on LED L1
pinMode(LED_1, OUTPUT); //row 1
digitalWrite(LED_1, LOW);
pinMode(LED_2, OUTPUT); //row 2
digitalWrite(LED_2, HIGH);
pinMode(LED_3, INPUT); //row 3
digitalWrite(LED_3, LOW);
delay(200);
//turn on LED L2
pinMode(LED_1, OUTPUT); //row 1
digitalWrite(LED_1, HIGH);
pinMode(LED_2, OUTPUT); //row 2
digitalWrite(LED_2, LOW);
pinMode(LED_3, INPUT); //row 3
digitalWrite(LED_3, LOW);
delay(200);
//turn on LED L3
pinMode(LED_1, INPUT); //row 1
digitalWrite(LED_1, LOW);
pinMode(LED_2, OUTPUT); //row 2
digitalWrite(LED_2, LOW);
pinMode(LED_3, OUTPUT); //row 3
digitalWrite(LED_3, HIGH);
delay(200);
//turn on LED L4
pinMode(LED_1, INPUT); //row 1
digitalWrite(LED_1, LOW);
pinMode(LED_2, OUTPUT); //row 2
digitalWrite(LED_2, HIGH);
pinMode(LED_3, OUTPUT); //row 3
digitalWrite(LED_3, LOW);
delay(200);
//turn on LED L5
pinMode(LED_1, OUTPUT); //row 1
digitalWrite(LED_1, LOW);
pinMode(LED_2, INPUT); //row 2
digitalWrite(LED_2, LOW);
pinMode(LED_3, OUTPUT); //row3
digitalWrite(LED_3, HIGH);
delay(200);
//turn on LED L6
pinMode(LED_1, OUTPUT);
digitalWrite(LED_1, HIGH);
pinMode(LED_2, INPUT);
digitalWrite(LED_2, LOW);
pinMode(LED_3, OUTPUT);
digitalWrite(LED_3, LOW);
delay(200);
}