Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Is it possible???

Status
Not open for further replies.

PhilipWarence

New Member
If i press one of the push buttons, my output in portb works correctly but when i hold 2 or 3 push buttons, the output both perform the simulation. I was confused if it is possible to do it like when i hold 2 or 3 buttons, it will only perform the output of the button that i had pressed first? If yes, can anyone here please.. Thanks for help. :):)
 

Attachments

  • wew.PNG
    wew.PNG
    10.2 KB · Views: 222
Here is my code. It performs all the statements and equations when I pressed more than a button. I need it to disregard the other commands that will follow whenever one among the buttons was hold. Help guys..


Code:
bit oldstate, oldstate1, oldstate2 ;
int x = 1, y = 1, v = 1;

void main()
{


TRISB = 0 ;
TRISD = 0xFF;
PORTB = 0 ;
oldstate = 0;
oldstate1 = 0;
oldstate2 = 0;

         do {

             if (Button (&Portd, 1,1,1))
                {
                  Portb = x;
                  y = x ;
                  v = x ;
                
                  oldstate = 1;
                }
             if (oldstate && Button(&PORTD, 1, 1, 0))
                {
                 x++;
                 oldstate = 0;
                }

             if (Button (&Portd, 2,1,1))
                {
                  Portb = y*2;

                  oldstate1 = 1;
                }
             if (oldstate1 && Button(&PORTD, 2, 1, 0))
                {
                 y = y*2;
                 x = y + 1;
                 v = y ;
                 oldstate1 = 0;
                }

              if (Button (&Portd, 3,1,1))
                {
                  Portb = v/2;
                  oldstate2 = 1;
                }
             if (oldstate2 && Button(&PORTD, 3, 1, 0))
                {
                 v = v/2;
                 x = v + 1 ;
                 y = v;
                 oldstate2 = 0;
                }
             } while(1);
}
 
Of the top of my head something like this should work,
Code:
byte buttons,old,edges,firstkey;

while(1){
    old=buttons;                 //copy buttons
    buttons=(PORTD>>1)&7;        //get all button presses
    if(buttons==0) {             //if no button pressed
        firstkey=0;              //and key
    }
    edges=(buttons^old)&buttons; //keep only new presses
    if(edges!=0&&firstkey==0){   //if first pressed
        firstkey=edges;          //store it
    }
    //at this point firstkey = 0,1,2 or 4 depending on which key pressed first
}

edit, added the &7 to the code.

Mike.
 
Last edited:
Polling the buttons would need to be fast and reliable. You could add some hardware to disable the other buttons when one is pressed. Or, if you have external interrupt available, then using it would be most efficient solution.
If this is a practical problem, please give more info about the system.
 
Hi,

Your diagram shows R1 and R2 as 100 ohms , they should really be like R3 10k ohms ( drawing mistake?)
 
Maybe have it jump into a while() loop when you press the button, and only leaves the loop when the button is released?
Might also want to debounce the switches- a short delay after the if statement would work.
 
Last edited:
Hi,
misterT, We do not need to change/add any devices in the circuit. Only with the program that will be burn in the PIC to make works like what I had said before. Thanks anyway :). Here's my updated program. I think it was on my if conditions or functions with goto statement. When I hold 2 buttons, and release 1 it does nothing like what im expecting but when i released the 2nd button it performs its function that supposedly not.

The circuit must only works if only 1 button is pressed and released.

bit oldstate, oldstate1, oldstate2 ;

void main() {
int z = 1, y = 0, x = 1;
TRISD = 0xFF ;
TRISB = 0 ;
PORTB = 0 ;



oldstate = 0;
oldstate1 = 0;
oldstate2 = 0;



A:
do{
if ((oldstate && Button(&PORTD, 1, 1, 1)) || (oldstate1 && Button(&PORTD, 2, 1, 1)) || (oldstate2 && Button(&PORTD, 3, 1, 1)))
{ goto B; }

if (Button(&PORTD, 1, 1, 1)) {
oldstate = 1;
}
if (oldstate && Button(&PORTD, 1, 1, 0)) {
PORTB = x;
y = x;
z = x;
x++;
oldstate = 0;
}
if (Button(&PORTD, 2, 1, 1)) {
oldstate1 = 1;
}
if (oldstate1 && Button(&PORTD, 2, 1, 0)) {
Portb = y*2;
y = y*2;
x = y+1;
z = y;
oldstate1 = 0;
}
if (Button(&PORTD, 3, 1, 1)) {
oldstate2 = 1;
}
if (oldstate2 && Button(&PORTD, 3, 1, 0)) {
Portb = z/2;
z = z/2;
x = z+1;
y = z;
oldstate2 = 0;
}



} while(1);

B: {
PORTB = PORTB;{
if (oldstate && Button(&PORTD, 1, 1, 0)) {
PORTB = PORTB;
oldstate = 0;
}
if (oldstate1 && Button(&PORTD, 2, 1, 0)) {
PORTB = PORTB;
oldstate = 0;
}
if (oldstate2 && Button(&PORTD, 3, 1, 0)) {
PORTB = PORTB;
oldstate = 0; }
} goto A;
}


}
 
Pommie, Thanks but im just a beginner here and unable to absorb your code :(.

Wp100, that's only a pulldown resistor Sir.

@Semaphöre, Actually it do not go out of the loop right when i start the program even if i released the button..
 
Pseudo-code (meant to be run in the loop)

Code:
if (button 1 pressed) {
  do whatever you need for button 1;
  while (any of the buttons pressed) {
    do nothing;
  }
} else if (button 2 pressed) {
  do whatever you need for button 2;
  while (any of the buttons pressed) {
    do nothing;
  }
} else if (button 3 pressed) {
  do whatever you need for button 3;
   while (any of the buttons pressed) {
      do nothing;
   }
}

or

Code:
if (any of the buttons is pressed) {
  switch (button pressed) {
     case 1: do whatever you need for button 1;
     case 2: do whatever you need for button 2;
     case 3: do whatever you need for button 3;
  }
  while (any of the buttons pressed) {
     do nothing;
   }
}
 
NorthGuy, Here is my objectives:
note: PIC will do nothing whenever any of the buttons is pressed until it was depressed and it will do the ff. functions
1) Button 1 will increment my output by 1 at the time i have depressed the button.
2) Button 1 will double my output at the time i have depressed the button.
3)Button 1 will divide my output at the time i have depressed the button.

4. If i hold the 2 buttons, and depressed 1 of those buttons PIC should do nothing to the output, also, if I released the other button that remains pressed it must also do nothing to the output.

**The difference between those functions is if in case you hold two buttons at the same time and released those buttons it should do nothing COMPARE to if you hold only one button and depressed it, it should do functions herewith.
 
Code:
bit oldstate, oldstate1, oldstate2 ;

void main() {
...

A:
  do{
if ((oldstate && Button(&PORTD, 1, 1, 1)) || (oldstate1 && Button(&PORTD, 2, 1, 1)) || (oldstate2 && Button(&PORTD, 3, 1, 1)))
    { goto B; }
...
  } while(1);

B: {
PORTB = PORTB;{
...
    } goto A;
    }


          }
This whole thing is rather convoluted, messy and difficult to follow. Using goto to jump in an out of infinite loops, assigning PORTB to itself, it looks like you started coding without a clear idea of what you were trying to do. I would suggest three things:

  1. Write out your algorithm before you start coding.
  2. When your code doesn't behave as expected, don't change the code, check that the code matches your algorithm. If it still doesn't work, go back and find what is wrong with the algorithm.
  3. Get a rubber duck, if you can't explain your algorithm to the duck in less than a minute, go back and either find a simpler way to express what you are doing, or break the job down into smaller chunks and work on one chunk at a time.

Wp100, that's only a pulldown resistor Sir.

I think he knows that, the point is that with a 5V supply and a 100 ohm resistor, you are dissipating 50mA per switch. It is wasting a lot of power and producing heat for no good reason, a 10K resistor would dissipate 500uA.

EDIT:
Just to help with the clarity "press" and "depress" mean the same thing, I think you mean "press" and "release".

What you've written there is the start of your algorithm, write it out as a sequence of conditions and actions and read it to your duck :)
 
Last edited:
Another try:

Code:
var: press = 0;
 
if (any of the buttons pressed) {
  if (button 1 pressed) press |= 1;
  if (button 2 pressed) press |= 2;
  if (button 3 pressed) press |= 4;
} else {
  switch (press) {
    case 1: do whatever you need for button 1;
    case 2: do whatever you need for button 2;
    case 4: do whatever you need for button 3;
    otherwise: do nothing;
  }
  press = 0;
}
 
The reason why i jump into a goto statement is to perform the special function in case of it was started with 2 buttons pressed and after that go back again into start(the infinite loop). Yep sir, Depressed that i had said before is "RELEASED" pardon me.
Im just an average user :(.

Would somebody here just help me all just about the program or much better debug my code attached here coz im loosing hope could not think well :(:(:(. Sorry guys..
 
You need to figure out an algorithm before writing the code.

I am really depressed because you don't want to consider my pseudo-code which I released in post #12, so I'm going to press you more.

It works very simple. While some of the buttons are pressed it records which ones are pressed. It waits until all buttons are released and then, if only one of the buttons was pressed, it takes actions, but if two or three were pressed simultaneously at any point in the past, it does nothing. Isn't that what you want?
 
Yeah. Exactly Mr. NorthGuy, thanks! I already have my clear algorithm since Id started coding. Your codes were exactly what I'm aiming to. But as we all know, actual programming was so complicated and has a big big different than writing only algorithms.. though algo was a real great help.

THANKS TO ALL OF YOU guys, finally I finished the code. Ill just post it here later for those who will be needing it someday. You can also check it out that it wasnt as easy as we're thinking..
 
But as we all know, actual programming was so complicated and has a big big different than writing only algorithms.

For the most part, it shouldn't be. If your code and your design are significantly different from each other, one or both of them need work.

The pseudo code northguy posted is 95% of the way to being valid C, it shouldn't take massive changes to get it to compile.
 
The programming from the algorithm should be straightworward:

C:
char press = 0;
char b1, b2, b3;
 
while(1) {
  // do hardware dependent part here
  // set b1, b2 and b3 to 1 if the corresponding button is currently pressed
  // set b1, b2 and b3 to 0 if it's not
  if (b1|b2|b3) {
    if (b1) press |= 1;
    if (b2) press |= 2;
    if (b3) press |= 4;
  } else {
    switch (press) {
      case 1:
        // processing button 1
        break;
      case 2:
        // processing button 2
        break;
      case 4:
        // processing button 3
    }
    press = 0;
  }
  // do other stuff, not related to buttons
}
 
Don't take this the wrong way but maybe you should read up some more on programming if you don't understand what people have put or how to code from a pseudo algorithm.
If something doesn't work right, the people can steer you in the right direction, but unless you find out for yourself why it didn't work, and more importantly UNDERSTAND why then you will never learn to be a good embedded software engineer.
 
char oldstate, oldstate1, oldstate2;

void main() {
int z = 1, y = 0, x = 1;
TRISD = 0xFF ;
TRISB = 0 ;
PORTB = 0 ;
oldstate = 0;
oldstate1 = 0;
oldstate2 = 0;
do {
if (Button(&PORTD, 1, 1, 1)) {
oldstate = 1; }
if (oldstate == 1 && Button(&PORTD, 1, 1, 0)) {
oldstate = 2; }
if (Button(&PORTD, 2, 1, 1)) {
oldstate1 = 1; }
if (oldstate1 == 1 && Button(&PORTD, 2, 1, 0)) {
oldstate1 = 2; }
if (Button(&PORTD, 3, 1, 1)) {
oldstate2 = 1; }
if (oldstate2 == 1 && Button(&PORTD, 3, 1, 0)) {
oldstate2 = 2; }
if (oldstate == 2 && oldstate1 == 2) {
oldstate = 0;
oldstate1 = 0;
oldstate2 = 0; }
if (oldstate1 == 2 && oldstate2 == 2) {
oldstate = 0;
oldstate1 = 0;
oldstate2 = 0; }
if (oldstate == 2 && oldstate2 == 2) {
oldstate = 0;
oldstate1 = 0;
oldstate2 = 0; }
if (oldstate == 2 && oldstate1 == 0 && oldstate2 == 0) {
PORTB = x;
y = x;
z = x;
x++;
oldstate = 0; }
if (oldstate1 == 2 && oldstate == 0 && oldstate2 == 0) {
Portb = y*2;
y = y*2;
x = y+1;
z = y;
oldstate1 = 0; }
if (oldstate2 == 2 && oldstate == 0 && oldstate1 == 0) {
Portb = z/2;
z = z/2;
x = z+1;
y = z;
oldstate2 = 0; }

} while(1);
}


//It already works..
 
Well, Im grateful when i found this forum guys coz you all helps me a lot :):):). Im still in college and a beginner in software programming. Thank you guyz. I learned a lot from you!
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top