View previous topic :: View next topic |
Author |
Message |
cyberant
Joined: 04 Jun 2007 Posts: 27
|
Problem: 2 Buttons with 3 Functions |
Posted: Mon Jun 04, 2007 6:43 am |
|
|
I need help - i want to check 2 buttons which are on the analog port set to digital.
I want to have 3 functions:
1. if button 1 is pressed : fahremotor()
2. if button 2 is pressed : fahrevorschub()
3. if both buttons are pressed : schleuse()
I've programmed the following code, I tried to solf the problem that its nearly imposible to press both a the same time -- the function (fahremotor() or fahrevorschub() ) gets startet if u release the button but it doesn't work - the funktion gets also startet if u press the button and another time if u release the button. Furthermore I have the Problem that if funcktion 3 was called it also starts function1/2 after I release the buttons.I cant find the Misstake - please help me =)
The Code:
Code: | #include "main.h"
#include "funktionen.c"
#define Schalterreturn input(PIN_E0)
#define Schalterstart input(PIN_E1)
#define Schaltervorschub input(PIN_A3)
void main()
{
short startgedrueckt=0;
short returngedrueckt=0;
setup_adc(ADC_CLOCK_INTERNAL); //AD Wandler ein
setup_adc_ports(AN0); //Analogen Eingänge(Pins)einschalten
setup_psp(PSP_DISABLED); //Parallel POort aus
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256); //konfiguriert Timer0
setup_timer_1(T1_DISABLED); //Abschalten Timer 1
setup_timer_2(T2_DISABLED,0,1); //Abschalten Timer 2
while (true)
{
if (startgedrueckt==1)
{
fahremotor(2500,1);
startgedrueckt=0;
}
if (returngedrueckt==1)
{
fahrevorschub();
returngedrueckt=0;
}
while (Schalterstart==0)
{
startgedrueckt=1;
if (Schalterreturn==0)
{
startgedrueckt=0;
schleuse();
break;
}
}
while (Schalterreturn==0)
{
returngedrueckt=1;
if (Schalterstart==0)
{
returngedrueckt=0;
schleuse();
break;
}
}
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Mon Jun 04, 2007 8:45 am |
|
|
Using an adapted debouncing technique, the following simplified model should work:
Code: |
int8 task;
.................
if(!Schalterreturn)
{task=0;}
if(!Schalterstart)
{task=1;}
if((!Schalterreturn)&&(!Schalterstart))
{task=2;}
switch(task)
{
case 0: fahremotor();
break;
case 1: fahrevorschub();
break;
case 2: schleuse();
break;
}
................
|
Humberto |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Mon Jun 04, 2007 9:00 am |
|
|
However the potential 'timing' problem would remain.
I'd suggest that when a key is first 'seen', a counter for it is started (as is normally used for 'debounce', but slightly slower), and the key is not accepted till this counter expires, and it is still pressed. If another key is pressed in this time, _restart the counter_. Then when the counter expires check the keys as shown. This way, so long as the second key is pressed inside the duration of the counter (I'd suggest something like 1/5th second), both keys will be seen by the test.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
cyberant
Joined: 04 Jun 2007 Posts: 27
|
|
Posted: Wed Jun 06, 2007 2:16 am |
|
|
I Solfed it with a simple delay_ms(200) which is called after 1 button is pressed and then checks which buttons are pressed
Thank you guys ![Smile](images/smiles/icon_smile.gif) |
|
![](templates/subSilver/images/spacer.gif) |
|