CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Problem: 2 Buttons with 3 Functions

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cyberant



Joined: 04 Jun 2007
Posts: 27

View user's profile Send private message

Problem: 2 Buttons with 3 Functions
PostPosted: Mon Jun 04, 2007 6:43 am     Reply with quote

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;
         }
      }

}
}
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon Jun 04, 2007 8:45 am     Reply with quote

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
Ttelmah
Guest







PostPosted: Mon Jun 04, 2007 9:00 am     Reply with quote

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
cyberant



Joined: 04 Jun 2007
Posts: 27

View user's profile Send private message

PostPosted: Wed Jun 06, 2007 2:16 am     Reply with quote

I Solfed it with a simple delay_ms(200) which is called after 1 button is pressed and then checks which buttons are pressed Very Happy
Thank you guys Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group