one shot on a button

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hello,

 

i have a small problem which i can't solve.

i'm ussing a button, witch pressed increase a number on a display (1-10)

the only thing i want is that when i pressed the button the number increments whit 1, and nothing more as long the button is pressed.

 i think it's a very common problem, but i can't solved it crying

 

thank you 

Last Edited: Sat. Dec 21, 2019 - 07:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So what would you like people to do now? Use telepathy to GUESS what the problem is?

 

Post your code and circuit diagram.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

the code is very basic, because its the first time i'm using a display.

so i try some things out.

i think i can do it whit a one-shot, but i can nowhere find a code witch i can use.

 



#ifndef F_CPU
#define F_CPU 1000000UL // 1 MHz clock speed
#endif
#define D4 eS_PORTB4
#define D5 eS_PORTB5
#define D6 eS_PORTB6
#define D7 eS_PORTB7
#define RS eS_PORTB0
#define EN eS_PORTB2

#include <avr/io.h>
#include <util/delay.h>
#include "lcd.h" // libary is down loaded from electrosome.com

int speed_selection = 0;
int temp_speed_selection = 0; // needed for speed_selection lus

int main(void)
{
	DDRB = 0b11111111;
	
	DDRD = 0b11111000;
	PORTD = 0b00000111;
	
	
	
	int i;
	Lcd4_Init(); 
	
	
	
	while(1)
	{
		if ((PIND & (1<<0)) == 0) // if button + = 0
		{
			speed_selection = speed_selection + 1;
			temp_speed_selection = 1;
		}
		
		if ((PIND & (1<<1)) == 0) // if button - = 0
		{
			speed_selection = speed_selection - 1;
			temp_speed_selection = 1;
		}
				
		
		Lcd4_Set_Cursor(1,1);
		Lcd4_Write_String("speed selection"); 		
			
		if (temp_speed_selection == 1) // only play this lus when something is changed
		{		
			if (speed_selection == 1)
			{
				Lcd4_Clear();
				Lcd4_Set_Cursor(2,7);
				Lcd4_Write_Char('1');
				Lcd4_Write_Char('0');
				Lcd4_Write_Char('%');			
			}
		
			if (speed_selection == 2)
			{
				Lcd4_Clear();
				Lcd4_Set_Cursor(2,7);
				Lcd4_Write_Char('2');
				Lcd4_Write_Char('0');
				Lcd4_Write_Char('%');
			}
		
			if (speed_selection == 3)
			{
				Lcd4_Clear();
				Lcd4_Set_Cursor(2,7);
				Lcd4_Write_Char('3');
				Lcd4_Write_Char('0');
				Lcd4_Write_Char('%');
			}
		
			if (speed_selection == 4)
			{
				Lcd4_Clear();
				Lcd4_Set_Cursor(2,7);
				Lcd4_Write_Char('4');
				Lcd4_Write_Char('0');
				Lcd4_Write_Char('%');
			}
		
			if (speed_selection == 5)
			{
				Lcd4_Clear();
				Lcd4_Set_Cursor(2,7);
				Lcd4_Write_Char('5');
				Lcd4_Write_Char('0');
				Lcd4_Write_Char('%');
			}
		
			if (speed_selection == 6)
			{
				Lcd4_Clear();
				Lcd4_Set_Cursor(2,7);
				Lcd4_Write_Char('6');
				Lcd4_Write_Char('0');
				Lcd4_Write_Char('%');
			}
		
			if (speed_selection == 7)
			{
				Lcd4_Clear();
				Lcd4_Set_Cursor(2,7);
				Lcd4_Write_Char('7');
				Lcd4_Write_Char('0');
				Lcd4_Write_Char('%');
			}
		
			if (speed_selection == 8)
			{
				Lcd4_Clear();
				Lcd4_Set_Cursor(2,7);
				Lcd4_Write_Char('8');
				Lcd4_Write_Char('0');
				Lcd4_Write_Char('%');
			}
		
			if (speed_selection == 9)
			{
				Lcd4_Clear();
				Lcd4_Set_Cursor(2,7);
				Lcd4_Write_Char('9');
				Lcd4_Write_Char('0');
				Lcd4_Write_Char('%');
			}
		
			if (speed_selection == 10)
			{
				Lcd4_Clear();
				Lcd4_Set_Cursor(2,7);
				Lcd4_Write_Char('1');
				Lcd4_Write_Char('0');
				Lcd4_Write_Char('0');			
				Lcd4_Write_Char('%');
			}
			
			temp_speed_selection = 0;
		}	
		

	}
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Look up debouncing techniques...several good ones are posted here on the the 'freaks.

 

As far as you code, why do you repeat the same lines over & over ?

Lcd4_Clear();
Lcd4_Set_Cursor(2,7);
Lcd4_Write_Char('%');

Be efficient & put them in one place (though that is more of a general improvement, not a bug fix).  Also why if, if, if, if, if,,,,use else if blocks, or maybe some sort of switch statements...again, that is not a bug fix, just efficiency.

 

 

https://www.geeksforgeeks.org/switch-statement-cc/

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sat. Dec 21, 2019 - 09:47 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Now let's try to determine what doesn't work mean. To start does the display work at all? Can you display anything on it?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes the display is working,

on the first row: "speed selection"

      second row: "20"                      (for example)

 

but the code is a start to program. it needs a lot of improvement, step by step. 

the next step was to make, that when the button was pressed the counter increments with 1 and no more.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need to maintain the pin of the switch so you can detect the transition from 'unpressed' to 'pressed' and vv. You can also use this to implement debouncing by ignoring state transitions that occur too quickly (e.g. within 20ms).

 

As it stands, your code implements 'auto repeat' rather than 'one shot' behaviour.

 

Add a variable that holds the last switch state. If the pin state changes, save the new state intros variable and do your action. You can detect switch release as well.