2016年1月17日 星期日

Arduino UNO學習331.內建範例-PWM與數位



Arduino學習331.內建範例-PWM與數位

前面的學習都知道了UNO板的數位接腳2,4,7,8,12,13,和3,5,6,9,10,11,
二者之間的不同,標示差異多了”~”符號,表示該腳位才可傳送PWM數據

硬體準備
分別由數位腳Pin2~13各接上燈及電阻共12個

打開內建範例>Analog>AnalogWriteMega
// These constants won't change.  They're used to give names
// to the pins used:
const int lowestPin = 2;
const int highestPin = 13;


void setup() {
  // set pins 2 through 13 as outputs:
  for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {
    pinMode(thisPin, OUTPUT);
  }
}

void loop() {
  // iterate over the pins:
  for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {
    // fade the LED on thisPin from off to brightest:
    for (int brightness = 0; brightness < 255; brightness++) {
      analogWrite(thisPin, brightness);
      delay(2);
    }
    // fade the LED on thisPin from brithstest to off:
    for (int brightness = 255; brightness >= 0; brightness--) {
      analogWrite(thisPin, brightness);
      delay(2);
    }
    // pause between LEDs:
    delay(100);
  }
}

將程式上傳後,很明顯的可以看到,PWM腳位和純數位腳位的亮燈差異,
https://www.arduino.cc/en/Tutorial/AnalogWriteMega


Arduino UNO學習330.可變電阻5段開關

Arduino學習330.可變電阻5段開關

透過數據的輸入來控制LED燈或被動件的開動,
將數值1024分由4燈顯示,於0時關閉,
因此可分成0,256,256*2,256*3,256*4,總5段
安裝時注意燈腳的正負極

硬體準備
可變電阻*1
LED燈*4
220或330電阻*4

找出ArduBlock拼圖,並依序1~4對應亮起Pin4,Pin5,Pin6,Pin7,

int _ABVAR_1_A0 = 0 ;

void setup()
{
Serial.begin(9600);
        pinMode( 4 , OUTPUT);
        pinMode( 5 , OUTPUT);
        pinMode( 6 , OUTPUT);
        pinMode( 7 , OUTPUT);
}

void loop()
{
        _ABVAR_1_A0 = map ( analogRead(0) , 0 , 1023 , 1 , 5 )  ;
Serial.print( analogRead(0) );
Serial.println();
        if (( ( _ABVAR_1_A0 ) > ( 1 ) ))
        {
                digitalWrite( 4 , HIGH );
        }
        else
        {
                digitalWrite( 4 , LOW );
        }
        if (( ( _ABVAR_1_A0 ) > ( 2 ) ))
        {
                digitalWrite( 5 , HIGH );
        }
        else
        {
                digitalWrite( 5 , LOW );
        }
        if (( ( _ABVAR_1_A0 ) > ( 3 ) ))
        {
                digitalWrite( 6 , HIGH );
        }
        else
        {
                digitalWrite( 6 , LOW );
        }
        if (( ( _ABVAR_1_A0 ) > ( 4 ) ))
        {
                digitalWrite( 7 , HIGH );
        }
        else
        {
                digitalWrite( 7 , LOW );
        }
}

上傳後打開監控視窗並調整電位器,看到0~1023分別控4個燈,
是不是可以運用在家用燈的調整上


Arduino UNO學習321.電位器類比控制閃燈



Arduino學習321.類比控制閃燈

硬體,可變電阻*1

↓打開內建範例,,檔案>範例>Analog>AnalogInput

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);
  // stop the program for <sensorValue> milliseconds:
  delay(sensorValue);
  // turn the ledPin off:
  digitalWrite(ledPin, LOW);
  // stop the program for for <sensorValue> milliseconds:
  delay(sensorValue);
}


上傳後調整電阻試試,內建Pin13的LED燈會變快閃慢閃,
再來試著打開ArduBlock拼圖如下↓
void setup()
{
        pinMode( 13 , OUTPUT);
Serial.begin(9600);
}

void loop()
{
        digitalWrite( 13 , HIGH );
        delay( analogRead(0) );
        digitalWrite( 13 , LOW );
Serial.print( analogRead(0) );
Serial.println();
        delay( 100 );
}

上傳後打開監控視窗,調整可變電阻,可得到數據,並控制LED閃燈秒差,
數位訊號只有開和關,0和1的訊號,並無法像PWM訊號調整亮度,
學習了腳位功能有何不同嗎?


Arduino UNO學習320.可變電阻調光



Arduino學習320.可變電阻調光

Arduino UNO除了數位訊號也設置了六個類比訊號A0~A5腳,
類比訊號透過電壓的高低變化來轉換成數據的大小,
Arduino UNO的最大讀取值為1024,輸出值為255,最大電壓為5V,
因此可換算每一刻度為5V/1024=0.0049V,
進而運用於溫度,距離,光度等等的數據運用,
此頁先學習顯示可變電阻發出的值,並控制燈光亮度,
而燈的腳位要在有~的PWM腳位才有效果

硬體準備
可變電阻(電位器)*1
LED燈*1
220或330電阻*1
↓打開內建範例,,檔案>範例>Analog>AnalogInOutSerial
上傳後調整電阻試試,LED燈會跟著高亮或變暗

再來試著打開ArduBlock拼圖如下↓
void setup()
{
Serial.begin(9600);
        Serial.begin(9600);
        pinMode( 9 , OUTPUT);
}

void loop()
{
Serial.print( analogRead(0) );
Serial.println();
        delay( 200 );
        Serial.print( ( analogRead(0) * 0.0049 ) );
        Serial.print(" ");
Serial.print( "V" );
Serial.println();
        delay( 200 );
        analogWrite(9 , ( analogRead(0) / 4 ));
}

上傳後打開監控視窗,調整可變電阻,可得到數據,並傳送調整LED燈的亮度,
特別注意,UNO板的數位接腳2,4,7,8,12,13,和3,5,6,9,10,11,
二者之間的不同,標示差異多了”~”符號,表示該腳位才可傳送PWM數據,


2016年1月16日 星期六

Arduino UNO學習261.外接液晶顯示器1602



Arduino學習261.外接液晶顯示器1602

延續上篇學習260,此篇運用外掛拼圖ArduBlock,
 
一樣有內建的ArduBlock模組,依圖塊顏色找出拼圖↓
和上一篇一樣的硬體連結↓
 

#include <LiquidCrystal.h>

char _ABVAR_1_temp = ' ' ;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
        Serial.begin(9600);
lcd.begin(16, 2);
Serial.begin(9600);
}

void loop()
{
        if (( ( Serial.available() ) > ( 0 ) ))
        {
                _ABVAR_1_temp = Serial.read();
        lcd.print( _ABVAR_1_temp );
        Serial.print( _ABVAR_1_temp );
Serial.println();
        }
}

上傳後打開監控視窗,並輸入文字,有沒有顯示在監控視窗和外接螢幕上,
由於文字是一個字元慢慢傳輸的,若是一次打了多個字元,在傳輸上是分開的,
而內建的拼圖模組,只能在外接螢幕上顯示一排文字,
如果沒有顯示在外接螢幕上或是亂碼,多是因為接觸不良的關係,多試幾次吧,


Arduino UNO學習260.內建範例-外接顯示器



Arduino學習260.內建範例-外接顯示器

LCD顯示器有很多種,接腳有14和16針2種,差異於背光
由於Arduino腳位有限,一般使用4位元方式傳送
1602模組腳位說明
1=Vss=接地0V
2=Vdd=電源+5V
3=Vo=對比0~+5V,接可變電阻調整對比
4=RS=資料暫存器
5=RW=讀取或寫入
6=E=Enabl致能信號
7=D0=Data I/O
8=D1=Data I/O
9=D2=Data I/O
10=D3=Data I/O
11=D4=Data I/O
12=D5=Data I/O
13=D6=Data I/O
14=D7=Data I/O
15=A+=背光電源(可不接)
16=K-=背光接地(可不接)

硬體
LCD液晶顯示器模組*1
可變電阻(電位器)*1
此外接螢幕需安裝資料庫

↓打開內建範例,,檔案>範例>LiquidCrystal>Hello World
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);
}


上傳後慢慢調整電位器可變電阻,直到液晶螢幕出現字樣↓
由於接線較多,經常會有接觸不良顯示不出的情形,故市售另有1602轉接模組,將電阻及接線等簡化為4線,使用更簡單↓


Arduino UNO學習241.內建範例-按鍵控燈訊號回傳



Arduino學習241.內建範例-按鍵控燈訊號回傳

準備硬體
開關
10K或1k電阻

↓打開內建範例,,檔案>範例>Digital >StateChangeDetection

會跳出另一個視窗↓

上傳後打開監控視窗,並壓按開關,會發現每壓4次,內建LED燈會亮起並回傳,

試著修改程式下方if (buttonPushCounter % 4 == 0)
將參數4改成3後上傳,會發現壓按開關的倍數3時,內建燈才會亮起,