MPG ENABLE AND DISABLE USING A PUSH BUTTON

Moderators: TomKerekes, dynomotion

Post Reply
AmitKumar171
Posts: 134
Joined: Tue Feb 20, 2018 7:35 am
Location: India

MPG ENABLE AND DISABLE USING A PUSH BUTTON

Post by AmitKumar171 » Fri Sep 11, 2020 10:54 am

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.
Thank You

AMIT KUMAR

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

Re: MPG ENABLE AND DISABLE USING A PUSH BUTTON

Post by TomKerekes » Fri Sep 11, 2020 7:06 pm

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

Tom Kerekes
Dynomotion, Inc.

AmitKumar171
Posts: 134
Joined: Tue Feb 20, 2018 7:35 am
Location: India

Re: MPG ENABLE AND DISABLE USING A PUSH BUTTON

Post by AmitKumar171 » Tue Sep 29, 2020 6:23 am

Hi tom.

thanks for your reply.
Thank You

AMIT KUMAR

Post Reply