選擇
#include<Wire.h>
#include<LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);
#define right 1
#define up 2
#define down 3
#define left 4
#define select 5
#define noselect 0
int akey=0;
void selectfun()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("select button");
lcd.setCursor(0,1);
lcd.print("1:R,2:U,3:D,4:L,5:S,0:N#");
}
int btfun()
{
akey=analogRead(A0);
if (akey>1000)
{
return noselect;
}
else if (akey<50)
{
return right;
}
else if (akey<195)
{
return up;
}
else if (akey<380)
{
return down;
}
else if (akey<555)
{
return left;
}
else if (akey<790)
{
return select;
}
return noselect;
}
void setup() {
// put your setup code here, to run once:
lcd.begin(16,2);
selectfun();
}
void loop() {
// put your main code here, to run repeatedly:
lcd.clear();
int a=btfun();
switch (a)
{
case noselect:
{
selectfun();
break;
}
case right:
{
lcd.print("Right");
break;
}
case up:
{
lcd.print("Up");
break;
}
case down:
{
lcd.print("Down");
break;
}
case left:
{
lcd.print("Left");
break;
}
case select:
{
lcd.print("Select");
break;
}
}
delay(500);
}
==============================================
控制LED燈
#include<Wire.h>
#include<LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);
#define right 1
#define up 2
#define down 3
#define left 4
#define select 5
#define noselect 0
int led1=1;
int led2=2;
int led3=3;
int led4=A1;
int akey=0;
void selectfun()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("select button");
lcd.setCursor(0,1);
lcd.print("1:R,2:U,3:D,4:L:N#");
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
}
int btfun()
{
akey=analogRead(A0);
if (akey>1000)
{
return noselect;
}
else if (akey<50)
{
return right;
}
else if (akey<195)
{
return up;
}
else if (akey<380)
{
return down;
}
else if (akey<555)
{
return left;
}
else if (akey<790)
{
return select;
}
return noselect;
}
void ledclear()
{
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
}
void setup() {
// put your setup code here, to run once:
lcd.begin(16,2);
selectfun();
}
void loop() {
// put your main code here, to run repeatedly:
lcd.clear();
int a=btfun();
switch (a)
{
case noselect:
{
selectfun();
break;
}
case right:
{
ledclear();
digitalWrite(led1,HIGH);
lcd.print("Right");
break;
}
case up:
{
ledclear();
digitalWrite(led2,HIGH);
lcd.print("Up");
break;
}
case down:
{
ledclear();
digitalWrite(led3,HIGH);
lcd.print("Down");
break;
}
case left:
{
ledclear();
digitalWrite(led4,HIGH);
lcd.print("Left");
break;
}
case select:
{
ledclear();
lcd.print("STOP");
break;
}
}
delay(500);
}
================================
沒有留言:
張貼留言