Page 1 of 1

MPG ENABLE AND DISABLE USING A PUSH BUTTON

Posted: Fri Sep 11, 2020 10:54 am
by AmitKumar171
Hi tom ,

I am using kflop+konnect+kanalog.

I have connected one MPG handwheel as per the connection on dynomotion wiki.

I want to control that MPG using a push button, similar to feed hold push button .

I checked the External buttons program and understood the debounce of a switch, but what if want to enable one i/o using bouncing that switch again. ??

One push button to enable that mpg and if pressing same push button, will disable the MPG, please help on how to proceed with that?

Waiting for your kind reply.

Re: MPG ENABLE AND DISABLE USING A PUSH BUTTON

Posted: Fri Sep 11, 2020 7:06 pm
by TomKerekes
Hi Amit,

You might do something like:

Code: Select all

#include "KMotionDef.h"

#define ENABLE_MPG_BIT 46

int elast=0,elastsolid=-1,ecount=0;
int Debounce(int n, int *cnt, int *last, int *lastsolid);


void main()
{
	int EnableMPG=0;

	for(;;)
	{
		WaitNextTimeSlice();
		// Handle Enable/Disable MPG
		if (Debounce(ReadBit(ENABLE_MPG_BIT),&ecount,&elast,&elastsolid)==1)
		{ 
			EnableMPG = 1-EnableMPG;  // toggle the enable
			printf("Enable = %d\n",EnableMPG);
		}
	}
}


// Debounce a bit
//
// return 1 one time when first debounced high 
// return 0 one time when first debounced low 
// return -1 otherwise 
#define DBTIME 300

int Debounce(int n, int *cnt, int *last, int *lastsolid)
{
	int v = -1;
	
	if (n == *last)  // same as last time?
	{
		if (*cnt == DBTIME-1)
		{
			if (n != *lastsolid)
			{
				v = *lastsolid = n;  // return debounced value
			}
		}
		if (*cnt < DBTIME)	(*cnt)++;
	}
	else
	{
		*cnt = 0;  // reset count
	}
	*last = n;
	return v;
}

Re: MPG ENABLE AND DISABLE USING A PUSH BUTTON

Posted: Tue Sep 29, 2020 6:23 am
by AmitKumar171
Hi tom.

thanks for your reply.