2015年7月31日 星期五

Arduino的教學02-控制 LED 燈光亮度與PWM

一、教學目標:控制LE燈亮度(呼吸燈)


三、程式碼

  1. int brightness = 0; // how bright the LED is
  2. int fadeAmount = 5; // how many points to fade the LED by
  3.  
  4. void setup() {
  5. // declare pin 9 to be an output:
  6. pinMode(9, OUTPUT);
  7. }
  8.  
  9. void loop() {
  10. // set the brightness of pin 9:
  11. analogWrite(9, brightness);
  12.  
  13. // change the brightness for next time through the loop:
  14. brightness = brightness + fadeAmount;
  15.  
  16. // reverse the direction of the fading at the ends of the fade:
  17. if (brightness == 0 || brightness == 255) {
  18. fadeAmount = -fadeAmount ;
  19. }
  20. // wait for 30 milliseconds to see the dimming effect
  21. delay(30);
  22. }
四、作業練習 1.如何讓呼吸燈的變化頻率快一點?

2015年7月29日 星期三

Arduino的教學01-arduino的介紹

一、Arduino簡介
二、實驗器材清單
三、麫包板
         http://www.letry.com.tw/letryhandbookc/handbook.htm
三、第一支程式-Blinking LED
四、作業

  1. 請用麫包板接出二個串聯的LED燈(LED不可以直接接電源會燒毀,須串聯 220歐姆的電阻)
  2. 並聯的LED燈?
  3. 如何讓2個 LED燈交替亮?


程式碼

  1. /*
  2. Blink
  3. Turns on an LED on for one second, then off for one second, repeatedly.
  4. This example code is in the public domain.
  5. */
  6. // Pin 13 has an LED connected on most Arduino boards.
  7. // give it a name:
  8. int led = 13;
  9. int led2 = 12;
  10. // the setup routine runs once when you press reset:
  11. void setup() {
  12. // initialize the digital pin as an output.
  13. pinMode(led, OUTPUT);
  14. pinMode(led2, OUTPUT);
  15. }
  16.  
  17. // the loop routine runs over and over again forever:
  18. void loop() {
  19. digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
  20. digitalWrite(led2, LOW); // turn the LED on (HIGH is the voltage level)
  21. delay(1000); // wait for a second
  22. digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
  23. digitalWrite(led2, HIGH); // turn the LED off by making the voltage LOW
  24. delay(1000); // wait for a second
  25. }
  26.  

三、網路資源

4. Arduino教學

http://1know.net/knowledge/97636f101c46?v=list

5.電阻色碼表


  • Arduino創始人在TED大會的演說 (Massimo Banzi: How Arduino is open-sourcing

練習01:暴力解-霹靂燈

  1. //(1)設定檔
  2. int ledPin1 = 8; // the number of the LED pin
  3. int ledPin2 = 9; // the number of the LED pin
  4. int ledPin3 = 10; // the number of the LED pin
  5. //(2)setup 函式
  6. void setup() {
  7. pinMode(ledPin1, OUTPUT);
  8. pinMode(ledPin2, OUTPUT);
  9. pinMode(ledPin3, OUTPUT);
  10. }
  11. //(3)loop 函式
  12. void loop() {
  13. digitalWrite(ledPin1, HIGH);
  14. delay(100);
  15. digitalWrite(ledPin1, LOW);
  16. delay(100);
  17. digitalWrite(ledPin2, HIGH);
  18. delay(100);
  19. digitalWrite(ledPin2, LOW);
  20. delay(100);
  21.  
  22.  
  23. digitalWrite(ledPin3, HIGH);
  24. delay(100);
  25. digitalWrite(ledPin3, LOW);
  26. delay(100);
  27. digitalWrite(ledPin2, HIGH);
  28. delay(100);
  29. digitalWrite(ledPin2, LOW);
  30. delay(100);
  31. }


Arduino教學
https://www.youtube.com/playlist?list=PLXbFMuyNWWqBQxgALwjrDSEC97f4Krq3P