輸入數字
int k;void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Please choice (1)Led1 HIGH (2)Led2 HIGH(3)Led3 HIGH");
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()>0)
{
k=Serial.read()-'0';
switch(k)
{
case 1:
{
Serial.println("Led1 HIGH");
break;
}
case 2:
{
Serial.println("Led2 HIGH");
break;
}
case 3:
{
Serial.println("Led3 HIGH");
break;
}
default:
{
Serial.println("choose 1-3 number");
}
}
}
}
==============
輸入字元
char k;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Please choice (a)Led1 HIGH (b)Led2 HIGH(c)Led3 HIGH");
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()>0)
{
k=Serial.read();
switch(k)
{
case 'a':
{
Serial.println("Led1 HIGH");
break;
}
case 'b':
{
Serial.println("Led2 HIGH");
break;
}
case 'c':
{
Serial.println("Led3 HIGH");
break;
}
default:
{
Serial.println("choose a-c character");
}
}
}
}
===================================
輸入字元
String data;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Please type p1001 p1002 p1003");
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()>0)
{
data=Serial.readString();
if(data=="p1001")
{
Serial.println("bill");
}
else if(data=="p1002")
{
Serial.println("Lise");
}
else if(data=="p1003")
{
Serial.println("Mary");
}
}
}
===================
呼吸燈
http://yehnan.blogspot.tw/2012/02/arduino_16.html
=========================
int bh=0;
int fa=5;
int led=3;
void setup() {
// put your setup code here, to run once:
pinMode(led,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
analogWrite(led,bh);
bh=bh+fa;
if(bh<=0 || bh>=255) //0-255
{
fa=-fa; //反相
}
delay(30);
}
========================
int bt=4;
int pin=7;
int vt=0;
void setup() {
// put your setup code here, to run once:
pinMode(pin,OUTPUT);
pinMode(bt,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
vt=digitalRead(bt);
if (vt==HIGH)
{
digitalWrite(pin,HIGH);
}
======================
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int sum=0;
int i=1;
do
{
sum +=i;
i=i+2;
}while (i<=100);
Serial.println(sum);
}
============
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int sum=0;
int i=0;
while (i<=100)
{
sum +=i;
i=i+2;
}
Serial.println(sum);
}
沒有留言:
張貼留言