8 years, 8 months ago.

adding buttons to stm creates problem. please help??

hello there, i have been working with stm 32 nucleo lately and found out that it is behaving strange when adding external buttons. If I add two buttons to toggle the number up and down and make the user button as start command the stm doesnt take any input from the external buttons. But if I change the functions of buttons and make the user buttons as one of the toggling buttons it take input from all three buttons and works fine, except having glitches sometimes with external buttons. Kindly explain me this phenomenon and guide me if I can fix this problem.

#include "mbed.h"
#include "TextLCD.h"


TextLCD lcd(PC_0, PC_1, D3, D5, D7, D9);
DigitalOut switchengine(PC_2);
DigitalIn pin_up(PA_10); 
DigitalIn pin_down(PA_1);
InterruptIn pin_start(PC_13);
 
Timeout quarter;      //timeout called every 15min when running
int Quarters;    //selected number of quarters
float runtime;            //currently set runtime interval in quarters
float runtimeleft;       //remaining runtime interval in quarters
 
void runtimeUpdate();
void displayUpdate();
void startRun() ;
 
//called when 15min has passed
void runtimeUpdate(){
   runtimeleft--;
   displayUpdate();
}
 
//update the display and start/stop the engine
void displayUpdate(){
 
 if (runtimeleft ==0){
   // we are done
   switchengine=0;
 
   //update display
   lcd.cls();
   lcd.locate(2,0);
   lcd.printf("ENGINE OFF");
     lcd.locate(0,1);
    lcd.printf("Rantime: %1.2f Hr", Quarters*15/60.0);
  
 }
 else  {
  //engine should be running
   switchengine=1;
 
  //update display first line
   
   if (runtime < 4){
     lcd.cls();
   lcd.locate(1,0); 
     lcd.printf("Time = %1.2f mn", (runtime*15));
   }
   else{
     lcd.cls();
   lcd.locate(1,0);
     lcd.printf("Time = %1.2f Hr", (runtime*15.0)/60.0);
   }
 
  //update display second line
   if(runtimeleft<4){

       lcd.locate(0,1);
       lcd.printf("Timeleft:%1.1f mn", runtimeleft*15);
       }
      else{ 

       lcd.locate(0,1);
   lcd.printf("Timeleft:%1.2f H", (runtimeleft*15.0)/60.0);
   }
 
  // now start timeout to call again in 15min  
  quarter.attach(&runtimeUpdate, 900);
  }  
}
 
 
// init and start the run
void startRun() {
 runtime = Quarters;
 runtimeleft = runtime;
 
 lcd.cls();
 lcd.locate(1,0);
 lcd.printf("ENGINE START");
 wait(1);
 
 displayUpdate();
}
 
 
int main() {
 
  int i= 0;
  pin_up.mode(PullUp);
  pin_down.mode(PullUp);
  pin_start.mode(PullDown);
  float time;
  
  lcd.locate(3,0);
  lcd.printf("SET TIME");
  

          if(pin_up==0)
           {
               i= i++;
           wait(0.1);
           if (i>200)
            i=200;
            time = i;
            if(i<4){
             lcd.cls();
             lcd.locate(1,0);
             lcd.printf("Time= %2.2f mn", time*15);
           
                          }
             else{
                 lcd.cls();
                 lcd.locate(1,0);
             lcd.printf("Time= %1.2f Hr", time*15/60);
             
           }
            }
                 else if(pin_down==0){
                     i=i--;
                     wait(0.1);
                     if( i<0)
                     i=0;
                     time = i;
                     if(i<4){
                         lcd.cls();
                         lcd.locate(1,0);
             lcd.printf("Time= %1.2f mn", time*15);
             }
              else{      
              lcd.cls();
              lcd.locate(1,0);
              lcd.printf("Time= %1.2f Hr",time*15/60);
              
                     }
                     }
                
  
  
  Quarters = i; //Initial selected number of quarters 
  
    if(runtimeleft > 0){
        startRun();
   // call to (re)start engine run)
}
else{
   pin_start.rise(&startRun);
   
   }// just wait for keypress
while(1);
}

Question relating to:

Affordable and flexible platform to ease prototyping using a STM32L152RET6 microcontroller.

Sounds like a button bounce problem. Please show some code you are using so that someone can help you. Please use code tags when posting code.

posted by David Fletcher 24 Aug 2015
Be the first to answer this question.