這裡記錄著我在課堂中的學習
⬇⬇⬇文章開始⬇⬇⬇
請至 Tinkercad 註冊帳號。
若已經有Autodesk的帳號,就不用在註冊一次,使用原本註冊的帳號密碼登入即可。
登入後,點選左上就LOGO
,找到Circuits
(電路
)的按鈕,再建立新電路,即可在網站模擬Arduino的實體樣子。
2019年10月24日
傳資料到 PC
1 2 3 4 5 6 7 8 9 10 11
| const byte ledPin = 13;
void setup(){ Serial.begin(9600); Serial.println("Hello," ); Serial.print("\tLED pin is: "); Serial.print(ledPin); Serial.print("\nBYE!"); } void loop(){ }
|
- 執行後,結果為
1 2 3
| Hello, LED pin is: 13 BYE!
|
更改內容test
1 2 3 4 5 6 7 8 9 10 11
| const long ledPin = 525566;
void setup(){ Serial.begin(9600); Serial.println("Hello," ); Serial.print("\tYongCheng: "); Serial.print(ledPin); Serial.print("\nBYE!"); } void loop(){ }
|
- 執行後,結果為
1 2 3
| Hello, YongCheng: 525566 BYE!
|
顯示正弦波
1 2 3 4 5 6 7 8 9 10 11 12 13
| void setup(){ Serial.begin(9600); }
void loop(){ float angle = 0;float sineData; for (angle = 0.0; angle <= 90; angle += 0.1) { sineData = sin(angle); Serial.println(sineData); delay(1); } }
|
- 執行後,顯示為
Arduino%E8%A8%AD%E8%A8%88/%E8%A8%BB%E8%A7%A32019-10-21-1.jpg)
串列監視器畫出心形
這裡可以參考一下上一篇顯示正弦波
我自己Google到的心形公式
(sqrt(cos(x))*cos(200*x)+sqrt(abs(x))-0.7)*(4-x^2)^0.01)
由於Arduino不支援上面打法,所必須改為:(pow的意思可以參考這邊連結)
(sqrt(cos(x))*cos(200*x)+sqrt(abs(x))-0.7)*pow((4-x^2),0.01)
1 2 3 4 5 6 7 8 9 10 11
| void setup(){ Serial.begin(9600); double angle = 0;float sineData; for (angle = -4.5; angle <= 4.5; angle += 0.008){ double x=angle; Serial.println(sqrt(cos(x))*cos(200*x)+sqrt(abs(x))-0.7)*pow((4-x*x),0.01); } }
void loop(){ }
|
- 執行後,顯示為
Arduino%E8%A8%AD%E8%A8%88/%E8%A8%BB%E8%A7%A32019-10-21-3.jpg)
老師找到的心形公式
(sqrt(cos(x))*cos(300*x)+sqrt(abs(x))-0.2)*pow((4-x*x),0.01)
1 2 3 4 5 6 7 8 9 10 11
| void setup(){ Serial.begin(9600); double angle = 0;float sineData; for (angle = -4.5; angle <= 4.5; angle += 0.008){ double x=angle; Serial.println(sqrt(cos(x))*cos(300*x)+sqrt(abs(x))-0.2)*pow((4-x*x),0.01); } }
void loop(){ }
|
- 執行後,顯示為
Arduino%E8%A8%AD%E8%A8%88/%E8%A8%BB%E8%A7%A32019-10-21-2.jpg)
試做一個電壓調節器
新建一個電路專案,新建一個Arduino Uno R3
和電位器
。
程式碼新增:
1 2 3 4 5 6 7 8 9 10 11 12
| const byte potPin = A0; int val;
void setup(){ Serial.begin(9600); } void loop(){ val = analogRead(potPin); Serial.println(val); delay(500); }
|
- 執行後,顯示為
Arduino%E8%A8%AD%E8%A8%88/%E8%A8%BB%E8%A7%A32019-10-21-4.jpg)
更改內容test
設置一個LED接收到512值以上,燈亮;
反之,接收到512值以下,燈滅。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| const byte potPin = A0; int val;
void setup(){ Serial.begin(9600); pinMode(13,OUTPUT); } void loop(){ val = analogRead(potPin); Serial.println(val); if(val>512){ digitalWrite(13,HIGH); } else { digitalWrite(13,LOW); } }
|
- 執行後,顯示為
Arduino%E8%A8%AD%E8%A8%88/%E8%A8%BB%E8%A7%A32019-10-21-5.jpg)
2019年10月28日
使用光敏電阻控制LED
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| const byte LED = 13; const byte CdS = A0;
void setup() { Serial.begin(9600); pinMode(13, OUTPUT); }
void loop(){ int val; val = analogRead(CdS); Serial.println(val); if(val>=700){ digitalWrite(13,HIGH); } else{ digitalWrite(13,LOW); } }
|
- 執行後,顯示為
Arduino%E8%A8%AD%E8%A8%88/%E8%A8%BB%E8%A7%A32019-10-28-1.jpg)
74HC595控制「七段顯示器」
讓七段顯示器,顯示0~9。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| const byte datePin = 2; const byte latchPin = 3; const byte clockPin = 4;
byte index = 0;
const byte LEDs[10] = { B01111110, B00110000, B01101101, B01111001, B00110011, B01011011, B01011111, B01110000, B01111111, B01110011};
void setup() { pinMode(datePin, OUTPUT); pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); }
void loop() { digitalWrite(latchPin, LOW); shiftOut(datePin,clockPin, LSBFIRST, LEDs[index]); digitalWrite(latchPin, HIGH); delay(1000); index ++; if (index == 10){ index = 0; } }
|
- 執行後,顯示為
Arduino%E8%A8%AD%E8%A8%88/%E8%A8%BB%E8%A7%A32019-10-28-2.jpg)
更改TEST
將七段顯示器顯示為「9、7、5、3、1」
(電路與上者相同)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| const byte datePin = 2; const byte latchPin = 3; const byte clockPin = 4;
byte index = 0;
const byte LEDs[5] = { B01110011, B01110000, B01011011, B01111001, B00110000};
void setup() { pinMode(datePin, OUTPUT); pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); }
void loop() { digitalWrite(latchPin, LOW); shiftOut(datePin,clockPin, LSBFIRST, LEDs[index]); digitalWrite(latchPin, HIGH); delay(1000); index ++; if (index == 5){ index = 0; } }
|
- 顯示結果為,依序「9、7、5、3、1」循環。
Arduino%E8%A8%AD%E8%A8%88/%E8%A8%BB%E8%A7%A32019-10-28-3.jpg)
2019年11月11日
超音波
課本範例(9-29)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| const byte trigPin = 10; const int echoPin = 9; unsigned long d;
unsigned long ping(){ digitalWrite(trigPin, HIGH); microseconds(10); digitalWrite(trigPin, LOW); return pulseIn(echoPin, HIGH); } void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); Serial.begin(9600); }
void loop() { d = ping() /58;
Serial.print(d); Serial.print("cm"); Serial.println();
delay(1000); }
|
test更改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| const int pingPin = 7; const int ledPin = 13;
void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); }
void loop() { long duration, cm; pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH); cm = microsecondestToCentimeters(duration); Serial.print("Distance: "); Serial.print(cm); Serial.print("cm"); Serial.println(); if (cm < 100){ digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } delay(100); } long microsecondestToCentimeters(long microseconds){ return microseconds / 29 / 2; }
|
- 依此配置,執行後,結果為
Arduino%E8%A8%AD%E8%A8%88/%E8%A8%BB%E8%A7%A32019-11-11-1.jpg)
電壓輸出
1 2 3 4
| void setup(){ analogWrite(10,102);} void loop(){ }
|
- 電路依此配置,執行後,結果為
Arduino%E8%A8%AD%E8%A8%88/%E8%A8%BB%E8%A7%A32019-11-11-2.jpg)
變頻控制LED燈光和馬達
2019年11月18日
傳值、傳址、傳參考
- call by ralue 傳值
- call by adress 傳址
- call by reference 傳參考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| void swap(int* x,int* y){ int temp = *x; *x = *y; *y = temp; } void setup(){ int a=10, b=20; Serial.begin(9600); Serial.println(String("Before swap: ")+ "\t" + a + "\t" +b); swap(&a,&b); Serial.println(String("After swap: ")+ "\t" + a + "\t" +b); } void loop(){ }
|
- 顯示如下:
1 2
| Before swap: 10 20 After swap: 20 10
|
8-1 函數
1 2 3 4 5 6 7 8 9 10 11 12
| coid cirArea(){ int r=5; float area = 3.14 *r *r; Serial.println(area); } void setup(){ Serial.begin(9600); } void loop(){ cirArea(); delay(2000); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
float ans;
float cirArea(int r) { float area = 3.14 * r * r; return area; }
void setup() { Serial.begin(9600); ans = cirArea(10); Serial.println(ans); ans = cirArea(20); Serial.println(ans); } void loop() { }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| double result = 0; double i; double SumCC() { double SumC=0; for (int n=1; n<101;n++){ SumC=(n*5/3)+SumC; } return SumC; } void setup() { Serial.begin(9600); for( i=0 ; i<300 ; i++){ result=SumCC()+result; } Serial.println(result); }
void loop(){ }
|
2019年12月02日
Arduino%E8%A8%AD%E8%A8%88/%E8%A8%BB%E8%A7%A32019-12-02-1.jpg)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| double fun(double t) { double A=2; double f=60; double result; double pi=3.1415926; result = A*sin(2*pi*f*t);
return result; }
void setup() { Serial.begin(9600); for(double t=0;t<=(1);t=t+0.001) { double result = fun(t); Serial.println(result); } }
void loop() { }
|
- 執行後,結果為
Arduino%E8%A8%AD%E8%A8%88/%E8%A8%BB%E8%A7%A32019-12-02-2.jpg)
 
t = 0 度 ~ 360 度
x = A sin ( 2 π f t ) + B cos ( 2 π f t )
A = 0.2
B = 0.3
f = 60
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| double fun(double t) { double A=0.2; double B=0.3; double f=60; double result; double pi=3.1415926; result = A*sin(2*pi*f*t) + B*cos(2*pi*f*t);
return result; }
void setup() { Serial.begin(9600); for(double t=0;t<=(1);t=t+0.001) { double result = fun(t); Serial.println(result); } }
void loop() { }
|
- 執行後,結果為
Arduino%E8%A8%AD%E8%A8%88/%E8%A8%BB%E8%A7%A32019-12-02-3.jpg)
老師上課補充
中斷 ➜ 最高權限
中斷
- 線路配置圖
Arduino%E8%A8%AD%E8%A8%88/%E8%A8%BB%E8%A7%A32019-12-02-4.jpg)
程式內容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| int pbInt = 0; int ledOut = A0; volatile int state = LOW; void setup() { pinMode(ledOut, OUTPUT); attachInterrupt(pbInt, stateChange, CHANGE); } void loop() { for (int i = 0; i < 100; i++) { delay(100); } } void stateChange() { state = !state; digitalWrite(ledOut, state); }
|
2019年12月09日
斜率
- x^2+5
2019年12月16日
外部中斷(FALLING)
Arduino%E8%A8%AD%E8%A8%88/%E8%A8%BB%E8%A7%A32019-12-16-1.jpg)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
int SensorLED = 9; int SensorINPUT = 2; char state = LOW;
void setup() {
pinMode(SensorLED, OUTPUT); pinMode(SensorINPUT, INPUT_PULLUP);
attachInterrupt(0, blink, FALLING); }
void loop() {
if (state == HIGH) { state = LOW; digitalWrite(SensorLED, HIGH); delay(500); } else { digitalWrite(SensorLED, LOW); } } void blink() { state = !state; }
|
進階(CHANGE)
Arduino%E8%A8%AD%E8%A8%88/%E8%A8%BB%E8%A7%A32019-12-16-1.jpg)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
int SensorLED = 9; int SensorINPUT = 2; char state = LOW;
void setup() {
pinMode(SensorLED, OUTPUT); pinMode(SensorINPUT, INPUT_PULLUP);
attachInterrupt(0, blink, CHANGE); }
void loop() {
if (state == HIGH) { state = LOW; digitalWrite(SensorLED, HIGH); delay(500); } else { digitalWrite(SensorLED, LOW); } } void blink() { state = !state; }
|