[LIB] ChooseNum

Discussion specific to projects ideas and support.
Post Reply
bungeshea
Posts: 207
Joined: 14 Aug 2011, 08:45
Location: Australia
Contact:

[LIB] ChooseNum

Post by bungeshea »

I have created yet another library, this one will allow the user to select a number on the NXT screen using the NXT buttons. Uses the CenterText library to center the displayed text.

Use:

Code: Select all

#include "ChooseNum.Lib.nxc"
int myvalue = ChooseNum( "Value: ", LCD_LINE3, 0, 9 );
Code:

Code: Select all

/*
	ChooseNum.Lib.nxc
*/

#include "CenterText.Lib.nxc"

int ChooseNum(string label, int y, int min, int max) {
	int value = (max+min)/2;
	while(!ButtonPressed(BTNCENTER,0)) {
		string msg = StrCat(label,NumToStr(value));
		TextOut(CenterText(msg), y, msg);
		if(ButtonPressed(BTNRIGHT,0)) {
			while(ButtonPressed(BTNRIGHT,0));
			value++;
		}
		if(ButtonPressed(BTNLEFT,0)) {
			while(ButtonPressed(BTNLEFT,0));
			value--;
		}
		if (value < min) value = max;
		if (value > max) value = min;
	}
	while(ButtonPressed(BTNCENTER,0));
	return value;
}
Download:
ChooseNum.lib.nxc
ChooseNum.Lib.nxc
(575 Bytes) Downloaded 380 times
cpapple123
Posts: 44
Joined: 04 May 2012, 20:20

Re: [LIB] ChooseNum

Post by cpapple123 »

Thank you for the code and for cluing me in on this functionality of NXC. I'm New to NXC and Mindstorms and learning fast. I modified you code to fit my needs and though I would share with you. I allow the user to hold the key down for scrolling through long list of numbers and also let me preset the start point. Next I might add a function to speed up the scroll if I hold the button for 10 counts.

Code: Select all

/*
	ChooseNum.Lib.nxc
	#include "ChooseNum.Lib.nxc"
     int myvalue = ChooseNum( "TEXT", "LINE#", min, max, preset);
*/

#include "CenterText.Lib.nxc"


int ChooseNum(string label, int y, int min, int max, int pre) {

     int value; = pre;
      
     while(!ButtonPressed(BTNCENTER,0)) {
		string msg = StrCat(label,NumToStr(value));
		TextOut(CenterText(msg), y, msg);
		if(ButtonPressed(BTNRIGHT,0)) {
			Wait(500);
               //while(ButtonPressed(BTNRIGHT,0));
			value++;
		}
		if(ButtonPressed(BTNLEFT,0)) {
			Wait(500);
               //while(ButtonPressed(BTNLEFT,0));
			value--;
		}
		if (value < min) value = max;
		if (value > max) value = min;
	}
	while(ButtonPressed(BTNCENTER,0));
	return value;
}
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests