TED ------ Massimo Banzi: Arduino 是如何打開想像力的
Arduino 課程
Arduino入門套件中文字幕教學第一課
Arduino 入門套件中文字幕教學第二課
Arduino 入門套件中文字幕教學第三課
2015年11月11日 星期三
2015年11月9日 星期一
Arduino 9:超聲波測距離
超聲波感測器 HCSR04
- const byte trigPin = 10; // 超音波模組的觸發腳
- const int echoPin = 9; // 超音波模組的接收腳
- unsigned long d; // 儲存高脈衝的持續時間
- unsigned long ping() {
- digitalWrite(trigPin, HIGH); // 觸發腳設定成高電位
- delayMicroseconds(5); // 持續 5 微秒
- 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); // 等待一秒鐘(每隔一秒測量一次)
- }
顯示在LCD上
- //------------------------- LCD
- //Library version:1.1
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #if defined(ARDUINO) && ARDUINO >= 100
- #define printByte(args) write(args);
- #else
- #define printByte(args) print(args,BYTE);
- #endif
- LiquidCrystal_I2C lcd(0x20,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line
- //-------------------------
- //+ + + + + + + + + + + 超音波模組
- const byte trigPin = 13; // 超音波模組的觸發腳
- const int echoPin = 12; // 超音波模組的接收腳
- unsigned long d; // 儲存高脈衝的持續時間
- unsigned long ping() {
- digitalWrite(trigPin, HIGH); // 觸發腳設定成高電位
- delayMicroseconds(5); // 持續 5 微秒
- digitalWrite(trigPin, LOW); // 觸發腳設定成低電位
- return pulseIn(echoPin, HIGH); // 傳回高脈衝的持續時間
- }
- //+ + + + + + + + + + +
- void setup() {
- //------------------------- LCD
- lcd.init(); // initialize the lcd
- lcd.backlight();
- lcd.setCursor(0, 0);
- lcd.print ("Distance= ");
- //------------------------- LCD
- //+ + + + + + + + + + + 超音波模組
- pinMode(trigPin, OUTPUT); // 觸發腳設定成「輸出」
- pinMode(echoPin, INPUT); // 接收腳設定成「輸入」
- //Serial.begin(9600); // 初始化序列埠
- //+ + + + + + + + + + +
- }
- void loop(){
- //+ + + + + + + + + + + 超音波模組
- d = ping() / 58; // 把高脈衝時間值換算成公分單位
- //Serial.print(d); // 顯示距離
- //Serial.print("cm");
- //Serial.println();
- //------------------------- LCD
- lcd.setCursor(9, 1);
- lcd.print((unsigned long) d );
- // lcd.setCursor(14, 1);
- lcd.print ("cm");
- //------------------------- LCD
- delay(1000); // 等待一秒鐘(每隔一秒測量一次)
- }
Arduino 8:溫度量測,來個LCD顯示
DS18B20
需要引入函式庫
- #include <OneWire.h>
- int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
- //Temperature chip i/o
- OneWire ds(DS18S20_Pin); // on digital pin 2
- void setup(void) {
- Serial.begin(9600); //從螢幕上監測
- }
- void loop(void) {
- float temperature = getTemp();
- Serial.println(temperature);
- delay(100); //just here to slow down the output so it is easier to read
- }
- float getTemp(){
- //returns the temperature from one DS18S20 in DEG Celsius
- byte data[12];
- byte addr[8];
- if ( !ds.search(addr)) {
- //no more sensors on chain, reset search
- ds.reset_search();
- return -1000;
- }
- if ( OneWire::crc8( addr, 7) != addr[7]) {
- Serial.println("CRC is not valid!");
- return -1000;
- }
- if ( addr[0] != 0x10 && addr[0] != 0x28) {
- Serial.print("Device is not recognized");
- return -1000;
- }
- ds.reset();
- ds.select(addr);
- ds.write(0x44,1); // start conversion, with parasite power on at the end
- byte present = ds.reset();
- ds.select(addr);
- ds.write(0xBE); // Read Scratchpad
- for (int i = 0; i < 9; i++) { // we need 9 bytes
- data[i] = ds.read();
- }
- ds.reset_search();
- byte MSB = data[1];
- byte LSB = data[0];
- float tempRead = ((MSB << 8) | LSB); //using two's compliment
- float TemperatureSum = tempRead / 16;
- return TemperatureSum;
- }
改成將結果輸出到LCD
- //------------------------- LCD
- //Library version:1.1
- #include <OneWire.h> // DS18S20 waterproof溫度模組
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #if defined(ARDUINO) && ARDUINO >= 100
- #define printByte(args) write(args);
- #else
- #define printByte(args) print(args,BYTE);
- #endif
- LiquidCrystal_I2C lcd(0x20,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line
- //-------------------------
- int DS18S20_Pin = 2; //DS18S20 溫度Signal pin on digital 2
- //Temperature chip i/o
- OneWire ds(DS18S20_Pin); // on digital pin 2
- void setup() {
- //------------------------- LCD
- lcd.init(); // initialize the lcd
- lcd.backlight();
- lcd.setCursor(0, 0);
- lcd.print ("Temp.=");
- //------------------------- LCD
- }
- void loop(){
- float temperature = getTemp();
- //Serial.println(temperature);
- //------------------------- LCD
- lcd.setCursor(6, 0);
- lcd.print(temperature, 3 );
- lcd.print((char)223); //度
- lcd.print("C");
- //------------------------- LCD
- delay(100); //just here to slow down the output so it is easier to read
- // delay(1000); // 等待一秒鐘(每隔一秒測量一次)
- }
- float getTemp(){
- //returns the temperature from one DS18S20 in DEG Celsius
- byte data[12];
- byte addr[8];
- if ( !ds.search(addr)) {
- //no more sensors on chain, reset search
- ds.reset_search();
- return -1000;
- }
- if ( OneWire::crc8( addr, 7) != addr[7]) {
- Serial.println("CRC is not valid!");
- return -1000;
- }
- if ( addr[0] != 0x10 && addr[0] != 0x28) {
- Serial.print("Device is not recognized");
- return -1000;
- }
- ds.reset();
- ds.select(addr);
- ds.write(0x44,1); // start conversion, with parasite power on at the end
- byte present = ds.reset();
- ds.select(addr);
- ds.write(0xBE); // Read Scratchpad
- for (int i = 0; i < 9; i++) { // we need 9 bytes
- data[i] = ds.read();
- }
- ds.reset_search();
- byte MSB = data[1];
- byte LSB = data[0];
- float tempRead = ((MSB << 8) | LSB); //using two's compliment
- float TemperatureSum = tempRead / 16;
- return TemperatureSum;
- }
Arduino 7: LCD--將數值輸出到LCD
LCD
- Arduino控制器的控制埠數量實在是有限,連接幾個感測器,通訊設備什麼的,你就會發現埠不夠用了,還想擴展一個液晶顯示器,怎麼辦?
- 為了解決上述問題,我們開發的I2C介面的LCD顯示器,I2C只需兩根線就可以實現資料顯示,還可以串聯多個I2C設備。標準IIC介面,除了Arduino可以使用之外,其他單片機同樣可以進行驅動控制。
- I2C LCD1602液晶模組,可以顯示2行,每行16個字元。對於Arduino初學者來說,不必為繁瑣複雜液晶驅動電路連線而頭疼了,這款LCD擴展板將電路簡化,使用相關文檔中的庫檔,您只需使用幾行簡單的Arduino控制代碼便能完成LCD控制顯示的功能。
- I2C LCD1602液晶模組背面的電位器還能提供你調節液晶顯示器對比度的功能。
- 新版的IIC LCD模組支援gadgeteer介面,並且具有位址設置功能,可以通過跳線設置位址(0x20-0x27)。
需引入函式庫
- //------------------------- LCD
- //Library version:1.1
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #if defined(ARDUINO) && ARDUINO >= 100
- #define printByte(args) write(args);
- #else
- #define printByte(args) print(args,BYTE);
- #endif
- LiquidCrystal_I2C lcd(0x20,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
- //-------------------------
- void setup(){
- //------------------------- LCD
- lcd.init(); // initialize the lcd
- lcd.backlight();
- lcd.home();
- lcd.print ("Hello world...");
- lcd.setCursor(0, 1); //設定游標位置
- lcd.print ("CTJH");
- //------------------------- LCD
- }
- //------------------------- LCD 每隔 interval 閃爍,測試用
- int backlightState = LOW;
- long previousMillis = 0;
- long interval = 1000;
- //-------------------------
- void loop(){
- //------------------------- LCD 每隔 interval 閃爍,測試用
- unsigned long currentMillis = millis();
- if(currentMillis - previousMillis > interval) {
- previousMillis = currentMillis;
- if (backlightState == LOW)
- backlightState = HIGH;
- else
- backlightState = LOW;
- if(backlightState == HIGH) lcd.backlight();
- else lcd.noBacklight();
- }
- //-------------------------
- }
2015年11月8日 星期日
Arduino 6:光學特雷明-- 聲音與光學的樂器
Light Theremin
光學特雷明
- // variable to hold sensor value
- int sensorValue;
- // variable to calibrate low value
- int sensorLow = 1023;
- // variable to calibrate high value
- int sensorHigh = 0;
- // LED pin
- const int ledPin = 13;
- void setup() {
- // Make the LED pin an output and turn it on
- pinMode(ledPin, OUTPUT);
- digitalWrite(ledPin, HIGH);
- // calibrate for the first five seconds after program runs
- while (millis() < 5000) {
- //keep record of the maximum sensor value
- sensorValue = analogRead(A0);
- if (sensorValue > sensorHigh) {
- sensorHigh = sensorValue;
- }
- // record the minimum sensor value
- if (sensorValue < sensorLow) {
- sensorLow = sensorValue;
- }
- }
- // turn the LED off, signaling the end of the calibration period
- digitalWrite(ledPin, LOW);
- }
- void loop() {
- //read the input from A0 and store it in a variable
- sensorValue = analogRead(A0);
- // map the sensor values to a wide range of pitches
- int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000);
- // play the tone for 20 ms on pin 8
- tone(8, pitch, 20);
- // wait for a moment
- delay(10);
- }
Arduino 5:馬達控制--使用IRF520控制轉速
延續上一篇的利用IRF520控制1W的LED,並且加上[可變電阻]直接控制轉速
- // named constants for the switch and motor pins
- const int motorPin = 9; // the number of the motor pin
- const int potentialPin= 0; // potential meter
- int value = 0;
- void setup() {
- // initialize the motor pin as an output:
- pinMode(motorPin, OUTPUT);
- // initialize the switch pin as an input:
- // pinMode(potentialPin, INPUT); No need declare Analog
- }
- void loop(){
- value = analogRead(potentialPin);
- int intensity = map(value, 0, 1023, 0, 255);
- analogWrite( motorPin , intensity);
- }
- // named constants for the switch and motor pins
- const int switchPin = 2; // the number of the switch pin
- const int motorPin = 9; // the number of the motor pin
- const int potentialPin= 0; // potential meter
- int switchState = 0; // variable for reading the switch's status
- void setup() {
- // initialize the motor pin as an output:
- pinMode(motorPin, OUTPUT);
- // initialize the switch pin as an input:
- pinMode(switchPin, INPUT);
- // pinMode(potentialPin, INPUT); No need declare Analog
- }
- void loop(){
- // read the state of the switch value:
- switchState = digitalRead(switchPin);
- int value = analogRead(potentialPin);
- int intensity= map(value, 0, 1023, 0,255);
- // check if the switch is pressed.
- if (switchState == HIGH) {
- // turn motor on:
- // digitalWrite(motorPin, HIGH);
- analogWrite(motorPin, intensity);
- }
- else {
- // turn motor off:
- digitalWrite(motorPin, LOW);
- }
- }
Arduino 4:LED 1W
要控制1W的LED必須要有足夠的電流:
Arduino 可利用[IRF520]當作開關,控制1W LED:
加上按鈕開關,控制LED是否亮起:
加入[可變電阻]當作輸入,控制1W LED的閃爍頻率:
- const int motorPin = 9; // the number of the motor pin
- const int potentialPin= 0; // potential meter
- int value = 0;
- void setup() {
- // initialize the motor pin as an output:
- pinMode(motorPin, OUTPUT);
- // initialize the switch pin as an input:
- // pinMode(potentialPin, INPUT); No need declare Analog
- Serial.begin(9600); //顯示在螢幕上
- }
- void loop(){
- value = analogRead(potentialPin);
- // Serial.print("value=");
- // Serial.println(value);
- if(value == 0){
- digitalWrite(motorPin, LOW);
- delay(1000);
- }
- else{
- int frequency = map(value, 0, 1023, 0,30); //控制頻率
- // Serial.print("freq=");
- // Serial.println(frequency);
- //int interval = (1/ frequency) *1000* 0.5; //週期的一半 (毫秒)
- int interval = 500/ frequency;
- // Serial.print("interval=");
- // Serial.println(interval);
- digitalWrite(motorPin, HIGH);
- delay(interval);
- digitalWrite(motorPin, LOW);
- delay(interval);
- }
- }
將閃爍的頻率,顯示在LCD上:
- //------------------------- LCD
- //Library version:1.1
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #if defined(ARDUINO) && ARDUINO >= 100
- #define printByte(args) write(args);
- #else
- #define printByte(args) print(args,BYTE);
- #endif
- LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
- // ^'20'改成"27" ,for "PCF8574"
- // SDA --> "A4"
- // SCL --> "A5"
- //-------------------------
- // named constants for the switch and motor pins
- const int motorPin = 9; // the number of the motor pin
- const int potentialPin= 0; // potential meter
- int value = 0;
- void setup() {
- // initialize the motor pin as an output:
- pinMode(motorPin, OUTPUT);
- // initialize the switch pin as an input:
- // pinMode(potentialPin, INPUT); No need declare Analog
- Serial.begin(9600);
- //------------------------- LCD
- lcd.init(); // initialize the lcd
- lcd.backlight();
- // lcd.setCursor(0, 0);
- // lcd.print ("freq. =");
- // lcd.setCursor(0, 1);
- // lcd.print ("period=");
- //------------------------- LCD
- }
- void loop(){
- value = analogRead(potentialPin);
- // Serial.print("value=");
- // Serial.println(value);
- if(value == 0){
- digitalWrite(motorPin, LOW);
- delay(1000);
- //------------------------- LCD
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print ("freq. =");
- lcd.setCursor(0, 1);
- lcd.print ("period=");
- lcd.setCursor(8, 0);
- lcd.print(0);
- lcd.setCursor(8, 1);
- lcd.print(0);
- //------------------------- LCD
- }
- else{
- int frequency = map(value, 0, 1023, 0,30); //控制頻率
- // Serial.print("freq=");
- // Serial.println(frequency);
- //int interval = (1/ frequency) *1000* 0.5; //週期的一半 (毫秒)
- int interval = 500/ frequency;
- // Serial.print("interval=");
- // Serial.println(interval);
- //------------------------- LCD
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print ("freq. =");
- lcd.setCursor(0, 1);
- lcd.print ("period=");
- lcd.setCursor(8, 0);
- lcd.print(frequency);
- lcd.setCursor(8, 1);
- lcd.print(2*interval);
- lcd.setCursor(13, 1);
- lcd.print("ms");
- //------------------------- LCD
- digitalWrite(motorPin, HIGH);
- delay(interval);
- digitalWrite(motorPin, LOW);
- delay(interval);
- }
- }
POV_視覺暫留
Arduino 3: LED - - RGB
共陰極 RGB LED
使用random()來隨機產生數值:
- #define LEDR 9
- #define LEDG 10
- #define LEDB 11
- void setup() {
- pinMode(LEDR, OUTPUT);
- pinMode(LEDG, OUTPUT);
- pinMode(LEDB, OUTPUT);
- }
- int r = 0;
- int g = 0;
- int b = 0;
- void loop() {
- r = random(0, 255);
- g = random(0, 255);
- b = random(0, 255);
- analogWrite(LEDR, r);
- analogWrite(LEDG, g);
- analogWrite(LEDB, b);
- delay(1000);
- }
改用[可變電阻]改變不同的R、G、B數值:
- #define LEDR 9
- #define LEDG 10
- #define LEDB 11
- void setup() {
- pinMode(LEDR, OUTPUT);
- pinMode(LEDG, OUTPUT);
- pinMode(LEDB, OUTPUT);
- Serial.begin(9600);
- }
- int r = 0;
- int g = 0;
- int b = 0;
- void loop() {
- int rREAD=analogRead(0);
- int gREAD=analogRead(1);
- int bREAD=analogRead(2);
- //r = random(0, 255);
- //g = random(0, 255);
- //b = random(0, 255);
- r = map(rREAD, 0,1023, 0,255);
- g = map(gREAD, 0,1023, 0,255);
- b = map(bREAD, 0,1023, 0,255);
- Serial.println(r);
- Serial.println(g);
- Serial.println(b);
- analogWrite(LEDR, r);
- analogWrite(LEDG, g);
- analogWrite(LEDB, b);
- delay(100);
- }
Attiny85
- // This program is in the public domain.
- // This sketch waits for an IR pulse from a remote control
- // and then randomly changes the color of an RGB LED
- int ledPinRED = 2; // LED on digital pin 2
- int ledPinGREEN = 1; // LED on digital pin 1
- int ledPinBLUE = 4; // LED on digital pin 0
- int inPin = 0; // the input pin for the IR phototransistor
- int randRED = 0;
- int randGREEN = 0;
- int randBLUE = 0;
- void setup() {
- pinMode(inPin, INPUT); // declare IR phototransistor as input
- }
- void loop(){
- while(digitalRead(inPin) != LOW) {}; // read input value
- randRED = random(255); // picking a random number
- randGREEN = random(255); // between 1 and 255
- randBLUE = random(255);
- analogWrite(ledPinRED, randRED);
- analogWrite(ledPinGREEN, randGREEN);
- analogWrite(ledPinBLUE, randBLUE);
- delay(100); // de-bounces the input so it does not zoom
- // through a zillion colors with every button click
- }
Arduino 1: LED
Arduino 板
首先,來[寫]一個簡單的arduino程式:
可以利用 delay() 調整不同頻率。
再來看一個[改寫版]:
現在,實際接上一個LED:
再使用[開關]控制 LED是否要亮起來:
- void setup(){
- // declare the LED pins as outputs
- pinMode(3,OUTPUT);
- // declare the switch pin as an input
- pinMode(2,INPUT);
- }
- void loop(){
- // read the value of the switch
- // digitalRead() checks to see if there is voltage
- // on the pin or not
- switchstate = digitalRead(2);
- // if the button is not pressed
- // blink the red LEDs
- if (switchstate == LOW) {
- digitalWrite(3, LOW); // turn the green LED on pin 3 off
- }
- // this else is part of the above if() statement.
- // if the switch is not LOW (the button is pressed)
- // the code below will run
- else {
- digitalWrite(3, HIGH); // turn the red LED on pin 5 on
- // wait for a quarter second before changing the light
- delay(250);
- }
- }
訂閱:
文章 (Atom)