2414 views|11 replies

13

Posts

0

Resources
The OP
 

Please help me write a program to monitor the status of 2 buttons. Is it really that difficult? I just can't figure it out! [Copy link]

 
A car window lifter: A single-pole double-throw switch, KEY1, KEY2; Two output points OUT1, OUT2 No press: KEY1=1; KEY2=1; Press lightly: KEY1=0; KEY2=1; Press hard: KEY1=1; KEY2=0; The switching process from light press to hard press, or from hard press to light press: KEY1=1; KEY2=1; takes at least several hundred milliseconds. Program OUT1, OUT2 output requirements: When KEY1=0, OUT1=1; When KEY2=0, OUT1=1; OUT2=1; In the process from light press to heavy press, keep: OUT1=1; cannot be interrupted In the process from heavy press to light press, keep: OUT1=1; cannot be interrupted Not pressed: OUT1=0; OUT2=0; I can't figure out how to write the logic~ Please give me some advice, masters!

This post is from stm32/stm8

Latest reply

The original poster should divide this problem into two parts, one is to obtain the key status, and the other is to process according to the key status, so that the logic may be clearer. If the original poster's heavy press and light press mean long press and short press, then you can assign key1=0 first, and when it detects that the key is pressed, key1=1, delay for a period of time and then detect again. If the key is still pressed, key1=2, and finally process according to the value of key1 in the main loop. This is what I did, and I can make one key play the role of two keys.  Details Published on 2019-2-17 09:21
 

5303

Posts

454

Resources
2
 
Is it not enough? If you press lightly, will you press harder? Is it not enough to press lightly?

This post is from stm32/stm8
 
 

714

Posts

2

Resources
3
 
It should be enough to determine the key operation by judging the duration of the key state, right?                                   
This post is from stm32/stm8
 
Personal signatureHello astroturfers
 

13

Posts

0

Resources
4
 
Blue Rain Night posted on 2019-2-14 14:37 Is it not enough? If you press lightly, will you definitely press hard? Is a light press not enough?
There are three levels in total: no press, light press, and heavy press. Please help!
This post is from stm32/stm8

Comments

Three levels: no pressing, light pressing, and heavy pressing. What if you just press lightly and then don't press again?  Details Published on 2019-2-14 20:27
 
 
 

13

Posts

0

Resources
5
 
y909334873 posted on 2019-2-14 14:41 It should be enough to determine the key operation by judging the duration of the key state?
Newbies don’t know how to do this!
This post is from stm32/stm8
 
 
 

5303

Posts

454

Resources
6
 
yfpc2006 posted on 2019-2-14 20:05 There are three gears in total: no press, light press, and hard press. Please help!
There are three gears in total: no press, light press, and hard press. What if you only press lightly and then don't press again?
This post is from stm32/stm8
 
 
 

1

Posts

0

Resources
7
 
I am also a novice, but you can see if this is right. You can check it according to this logic: if(KEY1 = 1 && key2 = 1) //No action in the initial state{ delay 1 s; if(KEY1 =1 && KEY2 =1) //Delay for one second if the key is not pressed{ OUT1= 0; OUT2 = 0; } else break; } else if(KEY1 = 0 || KEY2 = 0) //A key is pressed{ if(KEY1 =0 && KEY2 = 1) //If it is a light press{ { OUT1= 1;OUT2 = 0;} delay 1s; //Delay for one secondif(KEY1 =1 && KEY2= 0) //Judge whether the state is switchedOUT2 =1; else break; } else if(KEY1 =1 && KEY2 = 0) //If it is pressed hard{ OUT1 =1; OUT2 =1; delay 1s; if(KEY1 =0 && KEY2 = 1) //Judge state switching { OUT1 = 1; OUT2 = 0; } else break; } else //If KEY1 = 0 KEY2 = 0, then invalid break; } I don't know if it's right, you can take a look. This is probably the logic of the meaning.

This post is from stm32/stm8
 
 
 

1368

Posts

6

Resources
8
 
Sort out the state and write a simple state machine
This post is from stm32/stm8
 
Personal signature专注智能产品的研究与开发,专注于电子电路的生产与制造……QQ:2912615383,电子爱好者群: void
 
 

29

Posts

0

Resources
9
 
Do you mean short press and long press when you say light press and heavy press? Or can you please post the picture of the SPDT switch you mentioned for reference?
This post is from stm32/stm8
 
 
 

216

Posts

0

Resources
10
 
The description is not clear enough, please post the switch information or switch model
This post is from stm32/stm8
 
 
 

1

Posts

0

Resources
11
 
Operation key value status Output status Not pressed: KEY1=1; KEY2=1; 0 OUT1=0; OUT2=0; Press lightly: KEY1=0; KEY2=1; 1 OUT1=1; OUT2=0; Switch between light and heavy: KEY1=1; KEY2=1; 2 OUT1=1; OUT2=0; Press hard: KEY1=1; KEY2=0; 3 OUT1=1; OUT2=1; According to the original poster's description, is the status divided in this way? If so, you can first judge the status of 0-3, and then operate the output according to the status; the judgment from light press to release requires a delay judgment of "several hundred milliseconds";
This post is from stm32/stm8
 
 
 

931

Posts

3

Resources
12
 
The original poster should divide this problem into two parts, one is to obtain the key status, and the other is to process according to the key status, so that the logic may be clearer. If the original poster's heavy press and light press mean long press and short press, then you can assign key1=0 first, and when it detects that the key is pressed, key1=1, delay for a period of time and then detect again. If the key is still pressed, key1=2, and finally process according to the value of key1 in the main loop. This is what I did, and I can make one key play the role of two keys.
This post is from stm32/stm8
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list