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);
}
沒有留言:
張貼留言