|
这篇主要是介绍NXT可编程砖上的按键的,函数内容不多,我干脆就直接写了个程序,大家先看,不懂的提问,我再根据提问下教程。
这是一个时钟程序,可以自己设置时间,按右方向键数字加一,按左方向键数字减一,按橙色键确定。时间还是蛮准的,就是如果拿着这么大个时钟的话未免有点OUT,纯属娱乐和学习了。
本篇所涉及的函数:
bool ButtonPressed (const byte btn,
bool resetCount
)
其中参数btn为按键值,可取常用值为: BTNRIGHT 、BTNLEFT 和BTNCNETER ,分别对应NXT主机上的左、右三角按钮和中间橙色按钮。
参数resetCount为是否重置按键计数器标志,一般取值为false(0)
函数返回值类型为布尔值,即按下后返回为true(1),没按下返回为false(0)
- char hour = 0;
- char min = 0;
- char sec = 0;
- char clear = 0;
- void display()
- {
- if(clear == 0)
- NumOut(74, LCD_LINE3, sec);
- else
- NumOut(74, LCD_LINE3, sec, 1);
- TextOut(58, LCD_LINE3, ":");
- NumOut(42, LCD_LINE3, min);
- TextOut(26, LCD_LINE3, ":");
- NumOut(10, LCD_LINE3, hour);
-
- TextOut(15, LCD_LINE7, "Designed by ");
- TextOut(25, LCD_LINE8, "Hollwen");
- TextOut(10, LCD_LINE1, "www.cmnxt.com");
- }
- void SetTime(char T)
- {
- char temp = 0;
- while(ButtonPressed(BTNCENTER,false) != 1){
- if(ButtonPressed(BTNRIGHT,false) == 1)
- {
- Wait(100);
- if(ButtonPressed(BTNRIGHT,false) == 1)
- temp++;
- }
- if(ButtonPressed(BTNLEFT,false) == 1)
- {
- Wait(100);
- if(ButtonPressed(BTNLEFT,false) == 1)
- temp--;
- }
- switch(T){
- case 0: hour = temp;
- TextOut(5, LCD_LINE5, "INPUT THE HOUR", 0);
- break;
- case 1: min = temp;
- TextOut(5, LCD_LINE5, "INPUT THE MIN", 0);
- break;
- case 2: sec = temp;
- TextOut(5, LCD_LINE5, "INPUT THE SEC", 0);
- break;
- }
- display();
- }
- }
- void Run()
- {
- while(1){
- display();
- if(sec<59)
- sec++;
- else {
- sec = 0;
- if(min<59)
- min++;
- else{
- min = 0;
- if(hour<23)
- hour++;
- else{
- sec = 0;
- min = 0;
- hour = 0;
- }
- }
- }
- Wait(1000);
- }
- }
- task main()
- {
- SetTime(0);
- Wait(1000);
- SetTime(1);
- Wait(1000);
- SetTime(2);
- ClearScreen();
- clear = 1;
- Run();
- }
复制代码
|
|