-1

I want to use 1X4 membrane keypad to enter 3 working parameters for LED string controlled by Arduino mini. Keypad is attached to D2-D5 pins and LED is attached to pin 12. My objective is to set the following parameters: frequency (4 values), duty cycle (4 values), run time 4 (values). I need Arduino to read keypad 3 times so I can enter 3 parameters. "Run time" In the sketch below has not been set. I want to use "while" to force Arduino to read the keys but arduino reads it once. Only first "while" is working in the code and I can set cycle period. Second "while" does not work because the key has been already pressed and Arduino does not read keypad second time. Are there any means to use more than 1 "while" in the code or to use one while to read keypad 3 times?

Thank you.

 #define Key1 3 // key 1 attached to pin 3
 #define Key2 2 // key 2 attached to pin 2
 #define Key3 5 // key 3 attached to pin 5
 #define Key4 4 // key 4 attached to pin 4
 float period; // cycle time
 float power; // duty cycle
 float ontime; // ontime time LED is on
 float rest; // rest time LED is off
 const int ledPin = 12; // the number of the LED pin
 
 void setup()
 {
 pinMode(Key1, INPUT_PULLUP);
 pinMode(Key2, INPUT_PULLUP);
 pinMode(Key3, INPUT_PULLUP);
 pinMode(Key4, INPUT_PULLUP);
 pinMode(ledPin, OUTPUT);
 }
 void loop() {
 digitalWrite(ledPin, LOW);
 while (digitalRead(Key1) == HIGH && digitalRead(Key2) == HIGH && digitalRead(Key3) == HIGH && digitalRead(Key4) == HIGH) {};
 
 int key1a = digitalRead(Key1);
 int key2a = digitalRead(Key2);
 int key3a = digitalRead(Key3);
 int key4a = digitalRead(Key4);
 
 if (!key1a) {
 period = 1000;
 }
 if (!key2a) {
 period = 2000;
 }
 if (!key3a) {
 period = 3000;
 }
 if (!key4a) {
 period = 4000;
 }
 
 while (digitalRead(Key1) == HIGH && digitalRead(Key2) == HIGH && digitalRead(Key3) == HIGH && digitalRead(Key4) == HIGH) {};
 
 int key1b = digitalRead(Key1);
 int key2b = digitalRead(Key2);
 int key3b = digitalRead(Key3);
 int key4b = digitalRead(Key4 );
 
 if (!key1b) {
 power = 10;
 }
 if (!key2b) {
 power = 20;
 }
 if (!key3b) {
 power = 50;
 }
 if (!key4b) {
 power = 80;
 }
 
 ontime = period * power / 100;
 rest = period - ontime;
 
 digitalWrite(ledPin, HIGH);
 delay(ontime);
 digitalWrite(ledPin, LOW);
 delay(rest);
 }
asked Dec 24, 2021 at 1:54
3
  • I want to use "while" ... why? ... what if it makes no sense to use "while"? Commented Dec 24, 2021 at 2:09
  • What makes sense? Commented Dec 24, 2021 at 2:39
  • 1
    it would make more sense to detect a button press at begining of loop() and set a variable to 0, 1, 2, 3 or 4 ... 0 = no button ... use a state variable to keep track of which parameter you are changing Commented Dec 24, 2021 at 4:06

1 Answer 1

0

Do something to wait until a key is released before advancing to the next section. It would be wise to structure your program to have only one function that processes key presses, and then call it for each key. (Pass key identification, pin number, into the function.)

answered Jan 9, 2022 at 14:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.