https://www.tutorialspoint.com/arduino/index.htm
指撥開關
sw1file
先側殿為
int sw=7;
int swv;
void setup() {
// put your setup code here, to run once:
pinMode(sw,INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
swv=digitalRead(sw);
Serial.println(swv);
delay(2000);
}
================================
只播開關控制LED燈
int sw1=2;
int sw2=3;
int led1=7;
int led2=8;
int swv1;
int swv2;
void setup() {
// put your setup code here, to run once:
pinMode(sw1,INPUT_PULLUP);
pinMode(sw2,INPUT_PULLUP);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
swv1=digitalRead(sw1);
swv2=digitalRead(sw2);
if(swv1==0)
{
digitalWrite(led1,HIGH);
}
else
{
digitalWrite(led1,LOW);
}
if(swv2==0)
{
digitalWrite(led2,HIGH);
}
else
{
digitalWrite(led2,LOW);
}
}
=======================
//超音波感測器
#include <Ultrasonic.h>
Ultrasonic ultrasonic(12,13);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print(ultrasonic.Ranging(CM));
Serial.println(" cm");
delay(3000);
}
=======================
//超音波感測器控制LED燈蜂鳴器
#include <Ultrasonic.h>
Ultrasonic ultrasonic(12,13);
int led=7;
int bee=2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(led,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int dic=ultrasonic.Ranging(CM);
Serial.print(dic);
if (dic<=20)
{
digitalWrite(led,HIGH);
for(int i=0;i<10;i++)
{
tone(bee,1000);
delay(50);
tone(bee,500);
delay(50);
}
}
else
{
digitalWrite(led,LOW);
noTone(bee);
}
delay(3000);
}
==============================
中斷服務系統
(1)attachInterrupt():用來指定外部服務函數
attachinterrupt(interrupt,function,mode);
(a)interrupt:外部中斷編號(int0,int1)D2 and D3
(b)function:中斷服務函數
(c)mode:中斷觸發信號
LOW:
CHANGE:HIGH-LOW,LOW-HIGH
RISING:LOW_HIGH
FALLING:HIGH-LOW
==============
int in=0;
int pb1=2;
int led=8;
volatile boolean state=false;
void setup() {
// put your setup code here, to run once:
pinMode(led,INPUT);
pinMode(pb1,INPUT);
attachInterrupt(in,fun1,RISING);
}
void fun1()
{
state=!state;
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(led,LOW);
if (state)
{
digitalWrite(led,HIGH);
delay(2000);
digitalWrite(led,LOW);
delay(50);
}
else
{
digitalWrite(led,LOW);
}
}
==================
#define pb1 2
#define pb2 3
#define led1 8
#define led2 9
volatile boolean s1=false;
volatile boolean s2=false;
void setup() {
// put your setup code here, to run once:
pinMode(led1,INPUT);
pinMode(led2,INPUT);
pinMode(pb1,INPUT);
pinMode(pb2,INPUT);
attachInterrupt(0,chin1,RISING);
attachInterrupt(1,chin2,RISING);
}
void chin1()
{
s1=true;
s2=false;
}
void chin2()
{
s1=false;
s2=true;
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
if (s1)
{
digitalWrite(led1,HIGH);
}
if (s2)
{
digitalWrite(led2,HIGH);
}
delay(3000);
}
==========================
2016年12月29日 星期四
2016年12月27日 星期二
define
#define a 13 //#define 變數名稱 接腳值
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(a);
}
------------------------------------------
int x=2;
int y=6;
#define k 2*x*y //#define 變數名稱 運算式
-------------------------------------
#define 函數名稱(參數) 運算式
#define d(x) 5*x
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(d(3));
}
#define d(x,y) 2*x*y
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(d(3,2));
}
-----------------------------------------------------
3~,5~,6~,9~,10~,11~,脈衝寬頻信號 類比可接
三色Led燈
int b=9;
int g=10;
int r=11;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
int bv=0;
int gv=0;
int rv=0;
for(bv=1;bv<=255;bv++)
{
analogWrite(b,bv);
delay(10);
}
for(bv=255;bv>=0;bv--)
{
analogWrite(b,bv);
delay(10);
}
for(gv=1;gv<=255;gv++)
{
analogWrite(g,gv);
delay(10);
}
for(gv=255;gv>=0;gv--)
{
analogWrite(g,gv);
delay(10);
}
for(rv=1;rv<=255;rv++)
{
analogWrite(r,rv);
delay(10);
}
for(rv=255;rv>=0;rv--)
{
analogWrite(r,rv);
delay(10);
}
for(int i=0;i<=100;i++)
{
rv=random(1,30);
gv=random(1,20);
bv=random(1,50);
analogWrite(r,rv);
analogWrite(g,gv);
analogWrite(b,gv);
}
analogWrite(r,0); //歸零 關燈
analogWrite(g,0);
analogWrite(b,0);
}
=============================
int led=13;
int pirout=2;
void setup() {
// put your setup code here, to run once:
pinMode(led,OUTPUT);
pinMode(pirout,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int val=digitalRead(pirout);
if(val==HIGH)
{
digitalWrite(led,HIGH);
delay(3000);
digitalWrite(led,LOW);
delay(2000);
}
else
{
digitalWrite(led,LOW);
}
}
=============================
ledBar
int ledbar[10]={4,5,6,7,8,9,10,11,12,13};
void cleared()
{
for(int k=0;k<10;k++)
{
digitalWrite(ledbar[k],LOW);
}
}
void setup() {
// put your setup code here, to run once:
for(int i=0;i<10;i++)
{
pinMode(ledbar[i],OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=0;j<10;j++)
{
cleared();
digitalWrite(ledbar[j],HIGH);
delay(50);
}
for(int j=10;j>0;j--)
{
cleared();
digitalWrite(ledbar[j],HIGH);
delay(50);
}
}
========================================
int ledbar[10]={4,5,6,7,8,9,10};
const int cr=A0;
void cleared()
{
for(int k=0;k<7;k++)
{
digitalWrite(ledbar[k],LOW);
}
}
void setup() {
// put your setup code here, to run once:
for(int i=0;i<7;i++)
{
pinMode(ledbar[i],OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
int crv=analogRead(cr);
int mcrv=map(crv,0,1023,0,6);
cleared();
for(int j=0;j<mcrv;j++)
{
digitalWrite(ledbar[j],HIGH);
delay(500);
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(a);
}
------------------------------------------
int x=2;
int y=6;
#define k 2*x*y //#define 變數名稱 運算式
-------------------------------------
#define 函數名稱(參數) 運算式
#define d(x) 5*x
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(d(3));
}
#define d(x,y) 2*x*y
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(d(3,2));
}
-----------------------------------------------------
3~,5~,6~,9~,10~,11~,脈衝寬頻信號 類比可接
三色Led燈
int b=9;
int g=10;
int r=11;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
int bv=0;
int gv=0;
int rv=0;
for(bv=1;bv<=255;bv++)
{
analogWrite(b,bv);
delay(10);
}
for(bv=255;bv>=0;bv--)
{
analogWrite(b,bv);
delay(10);
}
for(gv=1;gv<=255;gv++)
{
analogWrite(g,gv);
delay(10);
}
for(gv=255;gv>=0;gv--)
{
analogWrite(g,gv);
delay(10);
}
for(rv=1;rv<=255;rv++)
{
analogWrite(r,rv);
delay(10);
}
for(rv=255;rv>=0;rv--)
{
analogWrite(r,rv);
delay(10);
}
for(int i=0;i<=100;i++)
{
rv=random(1,30);
gv=random(1,20);
bv=random(1,50);
analogWrite(r,rv);
analogWrite(g,gv);
analogWrite(b,gv);
}
analogWrite(r,0); //歸零 關燈
analogWrite(g,0);
analogWrite(b,0);
}
=============================
int led=13;
int pirout=2;
void setup() {
// put your setup code here, to run once:
pinMode(led,OUTPUT);
pinMode(pirout,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int val=digitalRead(pirout);
if(val==HIGH)
{
digitalWrite(led,HIGH);
delay(3000);
digitalWrite(led,LOW);
delay(2000);
}
else
{
digitalWrite(led,LOW);
}
}
=============================
ledBar
int ledbar[10]={4,5,6,7,8,9,10,11,12,13};
void cleared()
{
for(int k=0;k<10;k++)
{
digitalWrite(ledbar[k],LOW);
}
}
void setup() {
// put your setup code here, to run once:
for(int i=0;i<10;i++)
{
pinMode(ledbar[i],OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=0;j<10;j++)
{
cleared();
digitalWrite(ledbar[j],HIGH);
delay(50);
}
for(int j=10;j>0;j--)
{
cleared();
digitalWrite(ledbar[j],HIGH);
delay(50);
}
}
========================================
int ledbar[10]={4,5,6,7,8,9,10};
const int cr=A0;
void cleared()
{
for(int k=0;k<7;k++)
{
digitalWrite(ledbar[k],LOW);
}
}
void setup() {
// put your setup code here, to run once:
for(int i=0;i<7;i++)
{
pinMode(ledbar[i],OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
int crv=analogRead(cr);
int mcrv=map(crv,0,1023,0,6);
cleared();
for(int j=0;j<mcrv;j++)
{
digitalWrite(ledbar[j],HIGH);
delay(500);
}
}
2016年12月22日 星期四
溫控,光敏電阻
A.先接好溫控,並測試
溫控
int tempr=A0;
int tv=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
tv=analogRead(tempr);
Serial.println(tv);
delay(2000);
}
=============================
兩顆七段顯示器可正常運作
int sp1[10][7]={{1,1,1,1,1,1,0},{0,1,1,0,0,0,0},{1,1,0,1,1,0,1},{1,1,1,1,0,0,1},{0,1,1,0,0,1,1},{1,0,1,1,0,1,1},{1,0,1,1,1,1,1},{1,1,1,0,0,0,0},{1,1,1,1,1,1,1},{1,1,1,1,0,1,1}};
int sp2[10][7]={{1,1,1,1,1,1,0},{0,1,1,0,0,0,0},{1,1,0,1,1,0,1},{1,1,1,1,0,0,1},{0,1,1,0,0,1,1},{1,0,1,1,0,1,1},{1,0,1,1,1,1,1},{1,1,1,0,0,0,0},{1,1,1,1,1,1,1},{1,1,1,1,0,1,1}};
int k=0;
int k2=0;
void setup() {
// put your setup code here, to run once:
for(int i=0;i<=13;i++)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=0;j<7;j++)
{
digitalWrite(j,sp1[k][j]);
}
for(int h=7;h<=13;h++)
{
digitalWrite(h,sp2[k2][h]);
}
delay(2000);
k++;
k2++;
if(k==10)
{
k=0;
}
if(k2==10)
{
k2=0;
}
}
=================================
C.再接上 溫控及讀取其值顯示在七段顯示器
int sp1[10][7]={{1,1,1,1,1,1,0},{0,1,1,0,0,0,0},{1,1,0,1,1,0,1},{1,1,1,1,0,0,1},{0,1,1,0,0,1,1},{1,0,1,1,0,1,1},{1,0,1,1,1,1,1},{1,1,1,0,0,0,0},{1,1,1,1,1,1,1},{1,1,1,1,0,1,1}};
int sp2[10][7]={{1,1,1,1,1,1,0},{0,1,1,0,0,0,0},{1,1,0,1,1,0,1},{1,1,1,1,0,0,1},{0,1,1,0,0,1,1},{1,0,1,1,0,1,1},{1,0,1,1,1,1,1},{1,1,1,0,0,0,0},{1,1,1,1,1,1,1},{1,1,1,1,0,1,1}};
int tempr=A0;
int tv=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for(int i=0;i<=13;i++)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
tv=analogRead(tempr);
Serial.println(tv);
String sdata=(String)tv;
int k=sdata.charAt(0)-'0';
int k2=sdata.charAt(1)-'0';
for(int j=0;j<7;j++)
{
digitalWrite(j,sp1[k][j]);
}
for(int h=7;h<=13;h++)
{
digitalWrite(h,sp2[k2][h]);
}
delay(2000);
k++;
k2++;
if(k==10)
{
k=0;
}
if(k2==10)
{
k2=0;
}
}
======================================
光敏電阻 控制LED燈
int lightr=A0;
int vin=0;
int refv=850; //視實際光度
int led=7;
void setup() {
// put your setup code here, to run once:
pinMode(led,OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
vin=analogRead(lightr);
Serial.println(vin);
if (vin>refv)
{
digitalWrite(led,LOW);
}
if (vin<refv)
{
digitalWrite(led,HIGH);
}
}
=================================================
#difine 定義
1.定義接腳
2.定義運算式
3.定義函數
#define 名稱 資料
溫控
int tempr=A0;
int tv=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
tv=analogRead(tempr);
Serial.println(tv);
delay(2000);
}
=============================
兩顆七段顯示器可正常運作
int sp1[10][7]={{1,1,1,1,1,1,0},{0,1,1,0,0,0,0},{1,1,0,1,1,0,1},{1,1,1,1,0,0,1},{0,1,1,0,0,1,1},{1,0,1,1,0,1,1},{1,0,1,1,1,1,1},{1,1,1,0,0,0,0},{1,1,1,1,1,1,1},{1,1,1,1,0,1,1}};
int sp2[10][7]={{1,1,1,1,1,1,0},{0,1,1,0,0,0,0},{1,1,0,1,1,0,1},{1,1,1,1,0,0,1},{0,1,1,0,0,1,1},{1,0,1,1,0,1,1},{1,0,1,1,1,1,1},{1,1,1,0,0,0,0},{1,1,1,1,1,1,1},{1,1,1,1,0,1,1}};
int k=0;
int k2=0;
void setup() {
// put your setup code here, to run once:
for(int i=0;i<=13;i++)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=0;j<7;j++)
{
digitalWrite(j,sp1[k][j]);
}
for(int h=7;h<=13;h++)
{
digitalWrite(h,sp2[k2][h]);
}
delay(2000);
k++;
k2++;
if(k==10)
{
k=0;
}
if(k2==10)
{
k2=0;
}
}
=================================
C.再接上 溫控及讀取其值顯示在七段顯示器
int sp1[10][7]={{1,1,1,1,1,1,0},{0,1,1,0,0,0,0},{1,1,0,1,1,0,1},{1,1,1,1,0,0,1},{0,1,1,0,0,1,1},{1,0,1,1,0,1,1},{1,0,1,1,1,1,1},{1,1,1,0,0,0,0},{1,1,1,1,1,1,1},{1,1,1,1,0,1,1}};
int sp2[10][7]={{1,1,1,1,1,1,0},{0,1,1,0,0,0,0},{1,1,0,1,1,0,1},{1,1,1,1,0,0,1},{0,1,1,0,0,1,1},{1,0,1,1,0,1,1},{1,0,1,1,1,1,1},{1,1,1,0,0,0,0},{1,1,1,1,1,1,1},{1,1,1,1,0,1,1}};
int tempr=A0;
int tv=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for(int i=0;i<=13;i++)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
tv=analogRead(tempr);
Serial.println(tv);
String sdata=(String)tv;
int k=sdata.charAt(0)-'0';
int k2=sdata.charAt(1)-'0';
for(int j=0;j<7;j++)
{
digitalWrite(j,sp1[k][j]);
}
for(int h=7;h<=13;h++)
{
digitalWrite(h,sp2[k2][h]);
}
delay(2000);
k++;
k2++;
if(k==10)
{
k=0;
}
if(k2==10)
{
k2=0;
}
}
======================================
光敏電阻 控制LED燈
int lightr=A0;
int vin=0;
int refv=850; //視實際光度
int led=7;
void setup() {
// put your setup code here, to run once:
pinMode(led,OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
vin=analogRead(lightr);
Serial.println(vin);
if (vin>refv)
{
digitalWrite(led,LOW);
}
if (vin<refv)
{
digitalWrite(led,HIGH);
}
}
=================================================
#difine 定義
1.定義接腳
2.定義運算式
3.定義函數
#define 名稱 資料
2016年12月20日 星期二
函數
https://www.arduino.cc/en/Reference/FunctionDeclaration
函數架構二
Void 函數名稱(資料型態 參數 1,資料型態 參數 2)
{ [ 程式區塊 ] }
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int a=12;
int b=14;
fun2(a,b);
}
void fun2(int x,int y)
{
int total=x+y;
Serial.println(total);
}
=====================
函數架構三
函數名稱()
{
[ 程式區塊 ]
return 回傳變數值
}
接受回傳值=函數名稱();
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int k=fun3(); //整數
Serial.println(k);
}
int fun3()
{
return 100;
}
----------------------------------------------
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
String str3=fun3_1(); //字串
Serial.println(str3);
}
String fun3_1()
{
String str1="Hello ";
String str2="world";
return str1+str2;
}
---------------------------------
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
char k=fun3_2();
Serial.println(k);
}
char fun3_2()
{
char k='q';
return k;
}
=======================
函數架構 四
函數名稱(資料型態 參數 1,資料型態 參數 2)
{ [ 程式區塊 ]
return 回傳變數值
}
接受回傳值=函數名稱(引數1,引數2);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int k=fun4(12,45);
Serial.println(k);
}
int fun4(int x,int y)
{
int k=x+y;
return k;
}
================================
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int a[]={11,12,13,14,15};
int len=sizeof(a)/sizeof(int);
Serial.println(len);
fun1(a,len);
}
int fun1(int b[],int n)
{
for(int i=0;i<n;i++)
{
Serial.println(b[i]);
}
}
===============
ex1
int k;
int a=12;
int b=43;
int result;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("please choose(1)+ (2)- (3)*");
}
int jung1(int x,int y)
{
return x+y;
}
int jung2(int x,int y)
{
return x-y;
}
int jung3(int x,int y)
{
return x*y;
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0)
{
k=Serial.read()-'0';
switch (k) {
case 1:
{
result=jung1(a,b);
break;
}
case 2:
{
result=jung2(a,b);
break;
}
case 3:
{
result=jung3(a,b);
break;
}
}
Serial.print("result=");
Serial.println(result);
}
}
=====================================
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
String s=sdata();
int slen=s.length();
for(int i=0;i<slen;i++)
{
char t=s.charAt(i);
if(t==",")
{
Serial.println();
}
else
{
Serial.print(s[i]);
}
}
}
String sdata()
{
String d="";
int k[5]={12,34,65,11,78};
int len=sizeof(k)/sizeof(int);
for (int i=0;i<len;i++)
{
d=d+(String)k[i]+',';
}
return d;
}
=========================
蜂鳴器
int sp=2;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
for(int i=0;i<10;i++)
{
tone(sp,500);
delay(50);
tone(sp,500);
delay(50);
}
noTone(sp);
delay(2000);
}
函數架構二
Void 函數名稱(資料型態 參數 1,資料型態 參數 2)
{ [ 程式區塊 ] }
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int a=12;
int b=14;
fun2(a,b);
}
void fun2(int x,int y)
{
int total=x+y;
Serial.println(total);
}
=====================
函數架構三
函數名稱()
{
[ 程式區塊 ]
return 回傳變數值
}
接受回傳值=函數名稱();
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int k=fun3(); //整數
Serial.println(k);
}
int fun3()
{
return 100;
}
----------------------------------------------
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
String str3=fun3_1(); //字串
Serial.println(str3);
}
String fun3_1()
{
String str1="Hello ";
String str2="world";
return str1+str2;
}
---------------------------------
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
char k=fun3_2();
Serial.println(k);
}
char fun3_2()
{
char k='q';
return k;
}
=======================
函數架構 四
函數名稱(資料型態 參數 1,資料型態 參數 2)
{ [ 程式區塊 ]
return 回傳變數值
}
接受回傳值=函數名稱(引數1,引數2);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int k=fun4(12,45);
Serial.println(k);
}
int fun4(int x,int y)
{
int k=x+y;
return k;
}
================================
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int a[]={11,12,13,14,15};
int len=sizeof(a)/sizeof(int);
Serial.println(len);
fun1(a,len);
}
int fun1(int b[],int n)
{
for(int i=0;i<n;i++)
{
Serial.println(b[i]);
}
}
===============
ex1
int k;
int a=12;
int b=43;
int result;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("please choose(1)+ (2)- (3)*");
}
int jung1(int x,int y)
{
return x+y;
}
int jung2(int x,int y)
{
return x-y;
}
int jung3(int x,int y)
{
return x*y;
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0)
{
k=Serial.read()-'0';
switch (k) {
case 1:
{
result=jung1(a,b);
break;
}
case 2:
{
result=jung2(a,b);
break;
}
case 3:
{
result=jung3(a,b);
break;
}
}
Serial.print("result=");
Serial.println(result);
}
}
=====================================
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
String s=sdata();
int slen=s.length();
for(int i=0;i<slen;i++)
{
char t=s.charAt(i);
if(t==",")
{
Serial.println();
}
else
{
Serial.print(s[i]);
}
}
}
String sdata()
{
String d="";
int k[5]={12,34,65,11,78};
int len=sizeof(k)/sizeof(int);
for (int i=0;i<len;i++)
{
d=d+(String)k[i]+',';
}
return d;
}
=========================
蜂鳴器
int sp=2;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
for(int i=0;i<10;i++)
{
tone(sp,500);
delay(50);
tone(sp,500);
delay(50);
}
noTone(sp);
delay(2000);
}
2016年12月15日 星期四
二維陣列
int s[5];
int g=1;
int j=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int len=sizeof(s)/sizeof(int);
if (Serial.available()>0)
{
if (j<len)
{
s[j]=Serial.readString().toInt();
Serial.println("input ["+(String)g+"] element:"+s[j]);
if(g==len)
{
Serial.println("finish input...end");
for (int y=0;y<len;y++)
{
Serial.println(s[y]);
}
}
else
{
g++;
j++;
}
}
}
}
====================================
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int a[2][3]={{10,20,30},{40,50,60}}; // a[y][x]
int len1=sizeof(a)/sizeof(int); //所有元素
int len2=sizeof(a[0])/sizeof(int); //y個元素
int len0=len1/len2; //x元素
Serial.println("len1:"+(String)len1);
Serial.println("len2:"+(String)len2);
Serial.println("len0:"+(String)len0);
}
============================
輸出
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int a[2][3]={{10,20,30},{40,50,60}};
int len1=sizeof(a)/sizeof(int);
int len2=sizeof(a[0])/sizeof(int);
int len0=len1/len2;
for(int i=0;i<len0;i++)
{
for (int j=0;j<len2;j++)
{
Serial.println("a["+(String)i+"]["+(String)j+"]="+a[i][j]);
}
}
}
====================================================
七段顯示器
int sp[10][7]={{1,1,1,1,1,1,0},
{0,1,1,0,0,0,0},
{1,1,0,1,1,0,1},
{1,1,1,1,0,0,1},
{0,1,1,0,0,1,1},
{1,0,1,1,0,1,1},
{1,0,1,1,1,1,1},
{1,1,1,0,0,0,0},
{1,1,1,1,1,1,1},
{1,1,1,1,0,1,1}};
int k;
void setup() {
// put your setup code here, to run once:
k=0;
for(int i=5;i<=11;i++)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=0;j<7;j++)
{
digitalWrite(j+5,sp[k][j]);
}
delay(2000);
k++;
if(k==10)
{
k=0;
}
}
=================================================
int sp[10][7]={{1,1,1,1,1,1,0},{0,1,1,0,0,0,0},{1,1,0,1,1,0,1},{1,1,1,1,0,0,1},{0,1,1,0,0,1,1},{1,0,1,1,0,1,1},{1,0,1,1,1,1,1},{1,1,1,0,0,0,0},{1,1,1,1,1,1,1},{1,1,1,1,0,1,1}};
int d[4]={1,3,1,4};
int k;
void setup() {
// put your setup code here, to run once:
k=0;
for(int i=5;i<=11;i++)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=0;j<7;j++)
{
digitalWrite(j+5,sp[d[k]][j]);
}
delay(2000);
k++;
if(k==4)
{
k=0;
}
}
===================================
int sp[10][7]={{1,1,1,1,1,1,0},{0,1,1,0,0,0,0},{1,1,0,1,1,0,1},{1,1,1,1,0,0,1},{0,1,1,0,0,1,1},{1,0,1,1,0,1,1},{1,0,1,1,1,1,1},{1,1,1,0,0,0,0},{1,1,1,1,1,1,1},{1,1,1,1,0,1,1}};
int d[3]={5,2,0};
int k;
void setup() {
// put your setup code here, to run once:
k=0;
for(int i=5;i<=11;i++)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=0;j<7;j++)
{
digitalWrite(j+5,sp[d[k]][j]);
}
delay(2000);
k++;
if(k==3)
{
k=0;
}
}
=============================================
輸入
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int a[2][3];
int len1=sizeof(a)/sizeof(int);
int len2=sizeof(a[0])/sizeof(int);
int len=len1/len2;
int k=10;
for(int i=0;i<len;i++)
{
for (int j=0;j<len2;j++)
{
a[i][j]=k;
k++;
}
}
for(int i=0;i<len;i++)
{
for (int j=0;j<len2;j++)
{
Serial.println(a[i][j]);
}
}
}
====================================================
int g=1;
int j=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int len=sizeof(s)/sizeof(int);
if (Serial.available()>0)
{
if (j<len)
{
s[j]=Serial.readString().toInt();
Serial.println("input ["+(String)g+"] element:"+s[j]);
if(g==len)
{
Serial.println("finish input...end");
for (int y=0;y<len;y++)
{
Serial.println(s[y]);
}
}
else
{
g++;
j++;
}
}
}
}
====================================
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int a[2][3]={{10,20,30},{40,50,60}}; // a[y][x]
int len1=sizeof(a)/sizeof(int); //所有元素
int len2=sizeof(a[0])/sizeof(int); //y個元素
int len0=len1/len2; //x元素
Serial.println("len1:"+(String)len1);
Serial.println("len2:"+(String)len2);
Serial.println("len0:"+(String)len0);
}
============================
輸出
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int a[2][3]={{10,20,30},{40,50,60}};
int len1=sizeof(a)/sizeof(int);
int len2=sizeof(a[0])/sizeof(int);
int len0=len1/len2;
for(int i=0;i<len0;i++)
{
for (int j=0;j<len2;j++)
{
Serial.println("a["+(String)i+"]["+(String)j+"]="+a[i][j]);
}
}
}
====================================================
七段顯示器
int sp[10][7]={{1,1,1,1,1,1,0},
{0,1,1,0,0,0,0},
{1,1,0,1,1,0,1},
{1,1,1,1,0,0,1},
{0,1,1,0,0,1,1},
{1,0,1,1,0,1,1},
{1,0,1,1,1,1,1},
{1,1,1,0,0,0,0},
{1,1,1,1,1,1,1},
{1,1,1,1,0,1,1}};
int k;
void setup() {
// put your setup code here, to run once:
k=0;
for(int i=5;i<=11;i++)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=0;j<7;j++)
{
digitalWrite(j+5,sp[k][j]);
}
delay(2000);
k++;
if(k==10)
{
k=0;
}
}
=================================================
int sp[10][7]={{1,1,1,1,1,1,0},{0,1,1,0,0,0,0},{1,1,0,1,1,0,1},{1,1,1,1,0,0,1},{0,1,1,0,0,1,1},{1,0,1,1,0,1,1},{1,0,1,1,1,1,1},{1,1,1,0,0,0,0},{1,1,1,1,1,1,1},{1,1,1,1,0,1,1}};
int d[4]={1,3,1,4};
int k;
void setup() {
// put your setup code here, to run once:
k=0;
for(int i=5;i<=11;i++)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=0;j<7;j++)
{
digitalWrite(j+5,sp[d[k]][j]);
}
delay(2000);
k++;
if(k==4)
{
k=0;
}
}
===================================
int sp[10][7]={{1,1,1,1,1,1,0},{0,1,1,0,0,0,0},{1,1,0,1,1,0,1},{1,1,1,1,0,0,1},{0,1,1,0,0,1,1},{1,0,1,1,0,1,1},{1,0,1,1,1,1,1},{1,1,1,0,0,0,0},{1,1,1,1,1,1,1},{1,1,1,1,0,1,1}};
int d[3]={5,2,0};
int k;
void setup() {
// put your setup code here, to run once:
k=0;
for(int i=5;i<=11;i++)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=0;j<7;j++)
{
digitalWrite(j+5,sp[d[k]][j]);
}
delay(2000);
k++;
if(k==3)
{
k=0;
}
}
=============================================
輸入
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int a[2][3];
int len1=sizeof(a)/sizeof(int);
int len2=sizeof(a[0])/sizeof(int);
int len=len1/len2;
int k=10;
for(int i=0;i<len;i++)
{
for (int j=0;j<len2;j++)
{
a[i][j]=k;
k++;
}
}
for(int i=0;i<len;i++)
{
for (int j=0;j<len2;j++)
{
Serial.println(a[i][j]);
}
}
}
====================================================
2016年12月13日 星期二
陣列 map函式
陣列
資料型態 陣列[]={值1,......}
對映函式 map(可變電阻,值1,值2,值3,值4);
int srv=(0,1023,1,4);
const int led[5]={3,4,5,6,7};
int va=A0;
int kv;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for(int i=0;i<5;i++)
{
pinMode(led[i],OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
int sv=analogRead(va);
kv=map(sv,0,1023,0,4);
//Serial.println(kv);
for(int i=0;i<5;i++)
{
digitalWrite(led[i],LOW);
}
digitalWrite(led[kv],HIGH);
delay(1000);
}
================================
使用if
int led1=3;
int led2=4;
int led3=5;
int led4=6;
int led5=7;
int srv;
void setup() {
// put your setup code here, to run once:
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(led5,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
srv=analogRead(A0);
srv=map(srv,0,1000,0,4);
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
digitalWrite(led5,LOW);
if (srv==0)
{
digitalWrite(led1,HIGH);
}
else if(srv==1)
{
digitalWrite(led2,HIGH);
}
else if(srv==2)
{
digitalWrite(led3,HIGH);
}
else if(srv==3)
{
digitalWrite(led4,HIGH);
}
else if(srv==4)
{
digitalWrite(led5,HIGH);
}
delay(1000);
}
==================================
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int k[6]={11,12,13,14,15,16};
int len=sizeof(k);//12
Serial.println(len);
}
====================
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int k[6]={11,12,13,14,15,16};
int len=sizeof(k)/sizeof(int);//6
Serial.println(len);
}
======================================
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int a[5]={10,20,30,40,50};
int len=sizeof(a)/sizeof(int);
for (int i=0;i<len;i++)
{
Serial.println(a[i]);//
}
for (int j=len-1;j>=0;j--)
{
Serial.println(a[j]);//
}
}
1-2 常用單位:如焦耳(J)、庫侖(C)、伏特(V)、安培(A)及瓦特(W)等
1-3 能量:包含能量定義、能量轉換、效率、單位轉換等
1-4 電荷:包含電荷定義、電荷特性、庫侖定律等
1-5 電壓:包含電壓定義、電位與電位差、電動勢、電壓降及端電壓等
1-6 電流:包含電流定義、電流速率等
1-7 功率:包含功率定義、功及功率等
1-8 基本元件及符號認識
資料型態 陣列[]={值1,......}
對映函式 map(可變電阻,值1,值2,值3,值4);
int srv=(0,1023,1,4);
const int led[5]={3,4,5,6,7};
int va=A0;
int kv;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for(int i=0;i<5;i++)
{
pinMode(led[i],OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
int sv=analogRead(va);
kv=map(sv,0,1023,0,4);
//Serial.println(kv);
for(int i=0;i<5;i++)
{
digitalWrite(led[i],LOW);
}
digitalWrite(led[kv],HIGH);
delay(1000);
}
================================
使用if
int led1=3;
int led2=4;
int led3=5;
int led4=6;
int led5=7;
int srv;
void setup() {
// put your setup code here, to run once:
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(led5,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
srv=analogRead(A0);
srv=map(srv,0,1000,0,4);
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
digitalWrite(led5,LOW);
if (srv==0)
{
digitalWrite(led1,HIGH);
}
else if(srv==1)
{
digitalWrite(led2,HIGH);
}
else if(srv==2)
{
digitalWrite(led3,HIGH);
}
else if(srv==3)
{
digitalWrite(led4,HIGH);
}
else if(srv==4)
{
digitalWrite(led5,HIGH);
}
delay(1000);
}
==================================
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int k[6]={11,12,13,14,15,16};
int len=sizeof(k);//12
Serial.println(len);
}
====================
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int k[6]={11,12,13,14,15,16};
int len=sizeof(k)/sizeof(int);//6
Serial.println(len);
}
======================================
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int a[5]={10,20,30,40,50};
int len=sizeof(a)/sizeof(int);
for (int i=0;i<len;i++)
{
Serial.println(a[i]);//
}
for (int j=len-1;j>=0;j--)
{
Serial.println(a[j]);//
}
}
=============================
求極大值
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int a[5]={67,6,99,12,89};
int len=sizeof(a)/sizeof(int);
int max=0;
for (int i=0;i<len;i++)
{
if (max<a[i])
{
max=a[i];
}
}
Serial.println(max);
}
=============================================
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int a[5]={67,6,99,12,89};
int len=sizeof(a)/sizeof(int);
int min=a[0];
for (int i=0;i<len;i++)
{
if (min>a[i])
{
min=a[i];
}
}
Serial.println(min);
}
============================
int b[5];
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for(int i=10;i<=14;i++)
{
b[i-10]=i;
}
int len=sizeof(b)/sizeof(int);
for (int i=0;i<len;i++)
{
Serial.println(b[i]);
}
}
=================================
1-1 電的特性:如物質的結構、原子的結構及電的產生等求極大值
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int a[5]={67,6,99,12,89};
int len=sizeof(a)/sizeof(int);
int max=0;
for (int i=0;i<len;i++)
{
if (max<a[i])
{
max=a[i];
}
}
Serial.println(max);
}
=============================================
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int a[5]={67,6,99,12,89};
int len=sizeof(a)/sizeof(int);
int min=a[0];
for (int i=0;i<len;i++)
{
if (min>a[i])
{
min=a[i];
}
}
Serial.println(min);
}
============================
int b[5];
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for(int i=10;i<=14;i++)
{
b[i-10]=i;
}
int len=sizeof(b)/sizeof(int);
for (int i=0;i<len;i++)
{
Serial.println(b[i]);
}
}
=================================
1-2 常用單位:如焦耳(J)、庫侖(C)、伏特(V)、安培(A)及瓦特(W)等
1-3 能量:包含能量定義、能量轉換、效率、單位轉換等
1-4 電荷:包含電荷定義、電荷特性、庫侖定律等
1-5 電壓:包含電壓定義、電位與電位差、電動勢、電壓降及端電壓等
1-6 電流:包含電流定義、電流速率等
1-7 功率:包含功率定義、功及功率等
1-8 基本元件及符號認識
2016年12月8日 星期四
指令switch呼吸燈練習
break and continue
輸入數字
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);
}
輸入數字
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);
}
2016年12月6日 星期二
敘述跟指標VR可變電阻控制LED燈
http://yhhuang1966.blogspot.tw/2015/09/arduino_14.html
x++;
if else 敘述
int x=20;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if ((x%2)==0)
{
Serial.println("1");
}
else
{
Serial.println("2");
}
===========================
if
else if
int x=20;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if (x>=90)
{
Serial.println("segament 1");
}
else if (x>=80)
{
Serial.println("segament 2");
}
else if (x>=70)
{
Serial.println("segament 3");
}
else if (x>=600)
{
Serial.println("segament 4");
}
else
{
Serial.println("segament 5");
}
}
=======================
for迴圈
語法
for(開始值;終止值;增量值)
{
[循環區塊]
}
int total=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for(int i=1;i<10;i++)
{
for(int y=1;y<10;y++)
{
total=i*y;
Serial.print(i);
Serial.print("*");
Serial.print(y);
Serial.print("=");
Serial.print(total);
Serial.print(" ");
}
Serial.println();
}
}
=========================
正向
void setup() {
// put your setup code here, to run once:
for(int i=7;i<=13;i++)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=7;j<=13;j++)
{
digitalWrite(j,HIGH);
delay(3000);
}
}
===================
逆向
void setup() {
// put your setup code here, to run once:
for(int i=13;i>=1;i--)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=13;j>=1;j--)
{
digitalWrite(j,HIGH);
delay(3000);
}
}
============================
void setup() {
// put your setup code here, to run once:
for(int i=13;i>=1;i--)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=13;j>=1;j--)
{
digitalWrite(j,HIGH);
delay(100);
digitalWrite(j,LOW);
}
for(int j=7;j<=13;j++)
{
digitalWrite(j,HIGH);
delay(100);
digitalWrite(j,LOW);
}
}
===========
anoloaRead(變數) A0-A5/PWM ~D3
anolofWrite(接腳,類比值)
const int va=A2;
const int led=9;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
int sv=analogRead(va);
analogWrite(led,sv/4);
delay(150);
}
Arduino 基本語法筆記
++x;x++;
if else 敘述
int x=20;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if ((x%2)==0)
{
Serial.println("1");
}
else
{
Serial.println("2");
}
===========================
if
else if
int x=20;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if (x>=90)
{
Serial.println("segament 1");
}
else if (x>=80)
{
Serial.println("segament 2");
}
else if (x>=70)
{
Serial.println("segament 3");
}
else if (x>=600)
{
Serial.println("segament 4");
}
else
{
Serial.println("segament 5");
}
}
=======================
for迴圈
語法
for(開始值;終止值;增量值)
{
[循環區塊]
}
int total=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for(int i=1;i<10;i++)
{
for(int y=1;y<10;y++)
{
total=i*y;
Serial.print(i);
Serial.print("*");
Serial.print(y);
Serial.print("=");
Serial.print(total);
Serial.print(" ");
}
Serial.println();
}
}
=========================
正向
void setup() {
// put your setup code here, to run once:
for(int i=7;i<=13;i++)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=7;j<=13;j++)
{
digitalWrite(j,HIGH);
delay(3000);
}
}
===================
逆向
void setup() {
// put your setup code here, to run once:
for(int i=13;i>=1;i--)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=13;j>=1;j--)
{
digitalWrite(j,HIGH);
delay(3000);
}
}
============================
void setup() {
// put your setup code here, to run once:
for(int i=13;i>=1;i--)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=13;j>=1;j--)
{
digitalWrite(j,HIGH);
delay(100);
digitalWrite(j,LOW);
}
for(int j=7;j<=13;j++)
{
digitalWrite(j,HIGH);
delay(100);
digitalWrite(j,LOW);
}
}
===========
anoloaRead(變數) A0-A5/PWM ~D3
anolofWrite(接腳,類比值)
const int va=A2;
const int led=9;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
int sv=analogRead(va);
analogWrite(led,sv/4);
delay(150);
}
2016年12月1日 星期四
語言基礎(02)
arduino SDK
arduino IDE
COM3
微控板 輸入 輸出 記憶 邏輯運算單元
monitor serial序列埠視窗 速率9600
麵包板1 digital
麵包板2 anolog
模組 LCD BT WIFI
元件庫 (第三方官方函數庫)->sketct c語言
Rx,Tx 用D0 D1 如使用D0 D1 要先拔出再插入
物聯網架構
實體層 生活上實體如飛機車子
感知層 微電腦感測器1.架構2.sketch c 3.信號 4.硬體連接方式 電壓電流控制
網路層 乙太網路 wifi bigzee Hc06 w5100 通訊協定 封包
應用層 存檔到雲端資料庫>app,web application other
>HTML,javascript,Jquery,php,mysql,firebase
創作層 bigdata R語言 PowerBee
sketch c 語言
資料型態
1.byte 位元
2.int 整數
3.short 短整數
4.long 常整數 65535以上
5.float 浮點數
6.double 倍精數
7.char 'c'
8.string "bull"
9.boolean true/false
10.Array 一維 二維
11.word number字
變數
資料型態 變數名稱;
例 int a=10; string str="bill"; shar[]="bill";
=====================================
serial:
1.serial.begin(9600); 定數綠
2.輸出
serial.print() 無換行
serial.println()
3.輸入
serial.read()
4.判斷是否有資料
serial.available()>0
======================================
運算子
1算術運算子 +,-,*,/,%,=
2關係運算子 >,<,>=,<=,!=
3邏輯運算子 &&,||,!
4增減量運算子 ++,--
5複合運算子 +=,-=,*=,/=,
==========================
int x=20;
int y=40;
int total=0;
void setup() {
// put your setup code here, to run once: 計算寫在setup() 可在序列號視窗看結果
Serial.begin(9600);
total=x+y;
Serial.println(total);
}
void loop() {
// put your main code here, to run repeatedly:
}
===================
int x=20;
int y=40;
char k;;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0)
{
k=Serial.read(); 由序列埠讀入
Serial.println(k); 送到com3
}
}
========================
int k;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0)
{
k=Serial.read()-'0'; //讀取字元ASCII碼減零得數字
Serial.println(k);
}
}
=================
練習&& 積法功能 || 加法功能 或
int k=56;
int y=60;
boolean z;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
z=((k<=80) && (y<=70));
Serial.println(z);
}
void loop() {
// put your main code here, to run repeatedly:
}
=================
例 由序列埠控制
int led1=7;
int led2=8;
int led3=9;
int n;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0)
{
n=Serial.read()-'0';
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
if(n==1)
{
digitalWrite(led1,HIGH);
}
if(n==2)
{
digitalWrite(led2,HIGH);
}
if(n==3)
{
digitalWrite(led3,HIGH);
}
delay(3000);
}
}
2016年11月29日 星期二
認識Arduino
- 先讀讀認識基礎電學 楊明豐老師
http://media.nihs.tp.edu.tw/user/yangmf/default.aspx?active=media&course=%E5%9F%BA%E6%9C%AC%E9%9B%BB%E5%AD%B8+I
- 軟體下載
https://www.arduino.cc/en/Main/Donate
下載window installer or zip file
安裝>接上arduino uno R3 板子>打開裝置管理員檢查com3是arduino否不明則安裝driver>打開IDE 到工具序列埠選com3,則看到IDE蝦方為COM3即可作業
=================================
- 設一資料夾arduinoProject 放程式
教學架構
Arduino
1.數位信號
2.類比信號
3.脈衝寬頻
4.Rx/Tx 序列埠
5.I2c
6.SPI
sketchc語言
1.C語言架構 ,非傳統
2.資料型態
3.變數
4.C敘述(if,for,while,do...while)
5.陣列
6.函數四架構
7.自元/字串處理方式
元件模組
1.基本機電元件
2.感測
3.馬達
4.藍芽對接
=================
台南民族路
電子材料行==> 1南一(最貴 材料多) 2 和美 3懇憶 4中天 (北門路)
arduino uon R3 315元
麵包板 85*55 84元
杜邦線 20CM 公公40個 100元
發光二極體 5mm 紅色10個 20元
電阻器220Ω紅紅棕金 10個 15元
電阻器10KΩ 10個 15元
共518元
Arduino 種類
本課用uno r3 藍色的
介紹
1.d0 ,d1 序列埠控制信號
2.d0~d13 數位信號接腳
3.a0~a5 類比訊號接腳
4.d3,d5,d6,d9,d10,d11 脈衝寬頻(PWM)
5.d10.d11.d12.d13 SPI主從信號
6.a4,a5 I2C 信號
power
1.3.3v
2.vcc+5v
3.GND
模擬器可到123D circuits-autodesk.123D 下載
麵包板
藍色負極
紅色正極
兩邊直向相通
中間橫向向通
杜邦線有 公公 公母 母母
測試
一.一個發光二極體
{
int pled1=7;
void setup() {
// put your setup code here, to run once:
pinMode(pled1,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(pled1,HIGH);
delay(2000);
digitalWrite(pled1,LOW);
delay(2000);
}
====================================
兩個發亮
{
int pled1=7;
int pled2=8;
void setup() {
// put your setup code here, to run once:
pinMode(pled1,OUTPUT);
pinMode(pled2,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(pled1,HIGH);
digitalWrite(pled2,LOW);
delay(2000);
digitalWrite(pled1,LOW);
digitalWrite(pled2,HIGH);
delay(2000);
}
ok!!!!!
訂閱:
文章 (Atom)