Arduino 中斷與外部中斷
有時候執行Arduino程式時會發生Reset重起,這些資料可以參考
Cooper Maa attachInterrupt() 與外部中斷
Arduino 開發版中斷I/O Port 定義 UNO 外部中斷D2與D3
網站文章裡面提到:
要特別注意是,程式裏的 buttonState 變數是宣告成 volatile,這樣做的目的是告訴 Compiler 不要做最佳化,避免變數狀態不同步。給你一個建議,程式主體跟 ISR 都會用到的變數,盡可能把它宣告成 volatile。
attachInterrupt() 函式
attachInterrupt() 函式的用途是用來指定外部中斷的處理函式(Interrupt Service Routine, ISR),就像範例程式所示範的指定 buttonStateChanged() 當作 Interrupt 0 外部中斷的處理函式。
attachInterrupt() 函式有三個參數:
interrupt: 外部中斷的編號。大部份 Arduino 板子都有兩個外部中斷,編號 0 (Interrupt 0)是在 pin 2 上,而編號 1 (Interrupt 1)是在 pin 3 上。
function: 中斷處理函式(Interrupt Service Routine, ISR)。中斷處理函式必須是不接受參數而且不回傳任何東西。
mode: 定義什麼狀況下該觸發中斷,有四個可以設定的常數值:
LOW: 當 pin 為 LOW 時觸發中斷
CHANGE: 當 pin 狀態改變時觸發中斷,不管是從 HIGH 到 LOW 或從 LOW 到 HIGH
RISING: 當 pin 狀態從 LOW 到 HIGH 時觸發中斷,RISING 又稱正緣觸發
FALLING: 當 pin 狀態從 HIGH 到 LOW 時觸發中斷,FALLING 又稱負緣觸發
如果要移除外部中斷服務函式,就使用 detachInterrupt() 函式。
啟用與停止中斷
如果要停止 Arduino 所有中斷,可以呼叫 noInterrupt() 函式,要重新啟用中斷,只要呼叫一次 interrupts() 函式即可。
2015年11月24日 星期二
2015年11月20日 星期五
Arduino 與 全彩LED燈泡測試
Arduino 與 全彩LED燈泡測試
這次要測試剛買來的全彩LED燈泡測試
//範例用PWM來控制所以用9,10,11三隻PWM腳位,其他非PWM腳位也都可以但只有HIGH or LOW(1 or o) 看使用者應用,因為Arduino UNO腳位不多因此所有腳位都要充分利用。
//或是用Analog腳位A0~A5也是可以的
int redpin = 11; //select the pin for the red LED
int bluepin =10; // select the pin for the blue LED
int greenpin =9;// select the pin for the green LED
或是數位腳
int redpin = 7; //select the pin for the red LED
int greenpin =2;// select the pin for the green LED
int bluepin =4; // select the pin for the blue LED
int val;
void setup() {
pinMode(redpin, OUTPUT);
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
analogWrite(A3, 255); //255 R 紅 其餘0
analogWrite(A5, 0); //255 G 綠 其餘0
analogWrite(A4, 0); //255 B 藍 其餘0
//黃=R255,G255,B0 橘=228,120,51
delay(1000);
analogWrite(A3, 0); //255 R 紅 其餘0
analogWrite(A5, 255); //255 G 綠 其餘0
analogWrite(A4, 0); //255 B 藍 其餘0
delay(1000);
analogWrite(A3, 255); //255 R 紅 其餘0
analogWrite(A5, 255); //255 G 綠 其餘0
analogWrite(A4, 0); //255 B 藍 其餘0
delay(1000);
digitalWrite(redpin, HIGH); //255 R 紅 其餘0
digitalWrite(greenpin, LOW); //255 G 綠 其餘0
digitalWrite(bluepin, LOW); //255 B 藍 其餘0
/*
for(val=255; val>0; val--)
{
analogWrite(11, val);
analogWrite(10, 255-val);
analogWrite(9, 128-val);
delay(10);
}
for(val=0; val<255 p="" val=""> {
analogWrite(11, val);
analogWrite(10, 255-val);
analogWrite(9, 128-val);
delay(10);
}
Serial.println(val, DEC);
*/
}255>
這次要測試剛買來的全彩LED燈泡測試
![]() |
| 網路上的示意圖但紅線(V腳位)不能接5V,要接Ground才能亮 |
//範例用PWM來控制所以用9,10,11三隻PWM腳位,其他非PWM腳位也都可以但只有HIGH or LOW(1 or o) 看使用者應用,因為Arduino UNO腳位不多因此所有腳位都要充分利用。
//或是用Analog腳位A0~A5也是可以的
int redpin = 11; //select the pin for the red LED
int bluepin =10; // select the pin for the blue LED
int greenpin =9;// select the pin for the green LED
或是數位腳
int redpin = 7; //select the pin for the red LED
int greenpin =2;// select the pin for the green LED
int bluepin =4; // select the pin for the blue LED
int val;
void setup() {
pinMode(redpin, OUTPUT);
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
analogWrite(A3, 255); //255 R 紅 其餘0
analogWrite(A5, 0); //255 G 綠 其餘0
analogWrite(A4, 0); //255 B 藍 其餘0
//黃=R255,G255,B0 橘=228,120,51
delay(1000);
analogWrite(A3, 0); //255 R 紅 其餘0
analogWrite(A5, 255); //255 G 綠 其餘0
analogWrite(A4, 0); //255 B 藍 其餘0
delay(1000);
analogWrite(A3, 255); //255 R 紅 其餘0
analogWrite(A5, 255); //255 G 綠 其餘0
analogWrite(A4, 0); //255 B 藍 其餘0
delay(1000);
digitalWrite(redpin, HIGH); //255 R 紅 其餘0
digitalWrite(greenpin, LOW); //255 G 綠 其餘0
digitalWrite(bluepin, LOW); //255 B 藍 其餘0
/*
for(val=255; val>0; val--)
{
analogWrite(11, val);
analogWrite(10, 255-val);
analogWrite(9, 128-val);
delay(10);
}
for(val=0; val<255 p="" val=""> {
analogWrite(11, val);
analogWrite(10, 255-val);
analogWrite(9, 128-val);
delay(10);
}
Serial.println(val, DEC);
*/
}255>
2015年10月9日 星期五
Arduino WIFI 注意事項
Arduino WIFI 注意事項
ESP8266 WiFi 是Arduino常用的WIFI模組,該模組有兩種運行模式(AP模式和STA模式)
AP模式(相當於區域網路連結,手機本地控制):
把ESP8266 WiFi 熱點,也就是預先設定ESP8266 AP熱點名稱與密碼,這樣手機搜尋WIFI找到設定的名稱與密碼就可以連線使用。
執行手機APK程式就可以用WIFI方式與Arduino ESP8266溝通。
STA模式(相當於Internet網路連結,用於外部網路讓手機、網頁控制):
網路資源
另一塊WIFI模組 :nRF24L01 (電壓不可超過3.6V)
nFR24L01 2.4G無線模組只能單向雙工,一個模組只能單純發送或接收,不能改變,所以我們使用兩塊Arduino開發板來分別收發
網路資源 網路資源02 網路資源03 網路資源04
使用器材
arduino UNR R3*2
NRF2401 *2
使用庫
RF24
RF24Network
//////////////////////////////////////主機-發送信號/////////////////////////////////////
#include
#include
#include
// nRF24L01(+) radio attached using Getting Started board
RF24 radio(9,10);
// Network uses that radio
RF24Network network(radio);
// Address of our node
const uint16_t this_node = 1;
// Address of the other node
const uint16_t other_node = 0;
// How often to send 'hello world to the other unit
const unsigned long interval = 150; //ms
// When did we last send?
unsigned long last_sent;
// How many have we sent already
//unsigned long packets_sent;
// Structure of our payload
struct payload_t
{
uint32_t ms;
uint32_t sensorDataA;
uint32_t sensorDataB;
};
boolean power_SW;
int led_VOL;
void setup(void)
{
Serial.begin(115200);
Serial.println("RF24Network/examples/helloworld_tx/");
SPI.begin();
radio.begin();
radio.setDataRate( RF24_250KBPS ) ;
network.begin(/*channel*/ 50, /*node address*/ this_node);
randomSeed(analogRead(0));
pinMode(7,INPUT_PULLUP);
}
void loop(void)
{
power_SW=!digitalRead(7);
led_VOL=analogRead(A0)/4;
// Pump the network regularly
network.update();
// If it's time to send a message, send it!
unsigned long now = millis();
if ( now - last_sent >= interval )
{
last_sent = now;
Serial.print("power_SW:");
Serial.println(power_SW);
Serial.print("led_VOL:");
Serial.println(led_VOL);
Serial.print("Sending...");
payload_t payload = {
millis(),power_SW,led_VOL };
RF24NetworkHeader header(/*to node*/ other_node);
bool ok = network.write(header,&payload,sizeof(payload));
if (ok)
Serial.println("ok.");
else
Serial.println("failed.");
}
}
//////////////////////////////////////從機-接收信號/////////////////////////////////////
#include
#include
#include
// nRF24L01(+) radio attached using Getting Started board
RF24 radio(9,10);
// Network uses that radio
RF24Network network(radio);
// Address of our node
const uint16_t this_node = 0;
// Address of the other node
const uint16_t other_node = 1;
// Structure of our payload
struct payload_t
{
uint32_t ms;
uint32_t sensorDataA;
uint32_t sensorDataB;
};
#define power_PIN 5
#define led_PIN 6
boolean power_SW;
int led_VOL;
void setup(void)
{
Serial.begin(115200);
SPI.begin();
radio.begin();
radio.setDataRate( RF24_250KBPS ) ;
network.begin(/*channel*/ 50, /*node address*/ this_node);
pinMode(power_PIN,OUTPUT);
pinMode(led_PIN,OUTPUT);
}
void loop(void)
{
// Pump the network regularly
network.update();
// Is there anything ready for us?
while ( network.available() )
{
// If so, grab it and print it out
RF24NetworkHeader header;
payload_t payload;
network.read(header,&payload,sizeof(payload));
power_SW=payload.sensorDataA;
led_VOL=payload.sensorDataB;
Serial.print("power_SW:");
Serial.println(power_SW);
Serial.print("led_VOL:");
Serial.println(led_VOL);
}
digitalWrite(power_PIN,power_SW);
analogWrite(led_PIN,led_VOL);
}
ESP8266 WiFi 是Arduino常用的WIFI模組,該模組有兩種運行模式(AP模式和STA模式)
AP模式(相當於區域網路連結,手機本地控制):
把ESP8266 WiFi 熱點,也就是預先設定ESP8266 AP熱點名稱與密碼,這樣手機搜尋WIFI找到設定的名稱與密碼就可以連線使用。
執行手機APK程式就可以用WIFI方式與Arduino ESP8266溝通。
STA模式(相當於Internet網路連結,用於外部網路讓手機、網頁控制):
網路資源
另一塊WIFI模組 :nRF24L01 (電壓不可超過3.6V)
nFR24L01 2.4G無線模組只能單向雙工,一個模組只能單純發送或接收,不能改變,所以我們使用兩塊Arduino開發板來分別收發
網路資源 網路資源02 網路資源03 網路資源04
使用器材
arduino UNR R3*2
NRF2401 *2
使用庫
RF24
RF24Network
//////////////////////////////////////主機-發送信號/////////////////////////////////////
#include
#include
#include
// nRF24L01(+) radio attached using Getting Started board
RF24 radio(9,10);
// Network uses that radio
RF24Network network(radio);
// Address of our node
const uint16_t this_node = 1;
// Address of the other node
const uint16_t other_node = 0;
// How often to send 'hello world to the other unit
const unsigned long interval = 150; //ms
// When did we last send?
unsigned long last_sent;
// How many have we sent already
//unsigned long packets_sent;
// Structure of our payload
struct payload_t
{
uint32_t ms;
uint32_t sensorDataA;
uint32_t sensorDataB;
};
boolean power_SW;
int led_VOL;
void setup(void)
{
Serial.begin(115200);
Serial.println("RF24Network/examples/helloworld_tx/");
SPI.begin();
radio.begin();
radio.setDataRate( RF24_250KBPS ) ;
network.begin(/*channel*/ 50, /*node address*/ this_node);
randomSeed(analogRead(0));
pinMode(7,INPUT_PULLUP);
}
void loop(void)
{
power_SW=!digitalRead(7);
led_VOL=analogRead(A0)/4;
// Pump the network regularly
network.update();
// If it's time to send a message, send it!
unsigned long now = millis();
if ( now - last_sent >= interval )
{
last_sent = now;
Serial.print("power_SW:");
Serial.println(power_SW);
Serial.print("led_VOL:");
Serial.println(led_VOL);
Serial.print("Sending...");
payload_t payload = {
millis(),power_SW,led_VOL };
RF24NetworkHeader header(/*to node*/ other_node);
bool ok = network.write(header,&payload,sizeof(payload));
if (ok)
Serial.println("ok.");
else
Serial.println("failed.");
}
}
//////////////////////////////////////從機-接收信號/////////////////////////////////////
#include
#include
#include
// nRF24L01(+) radio attached using Getting Started board
RF24 radio(9,10);
// Network uses that radio
RF24Network network(radio);
// Address of our node
const uint16_t this_node = 0;
// Address of the other node
const uint16_t other_node = 1;
// Structure of our payload
struct payload_t
{
uint32_t ms;
uint32_t sensorDataA;
uint32_t sensorDataB;
};
#define power_PIN 5
#define led_PIN 6
boolean power_SW;
int led_VOL;
void setup(void)
{
Serial.begin(115200);
SPI.begin();
radio.begin();
radio.setDataRate( RF24_250KBPS ) ;
network.begin(/*channel*/ 50, /*node address*/ this_node);
pinMode(power_PIN,OUTPUT);
pinMode(led_PIN,OUTPUT);
}
void loop(void)
{
// Pump the network regularly
network.update();
// Is there anything ready for us?
while ( network.available() )
{
// If so, grab it and print it out
RF24NetworkHeader header;
payload_t payload;
network.read(header,&payload,sizeof(payload));
power_SW=payload.sensorDataA;
led_VOL=payload.sensorDataB;
Serial.print("power_SW:");
Serial.println(power_SW);
Serial.print("led_VOL:");
Serial.println(led_VOL);
}
digitalWrite(power_PIN,power_SW);
analogWrite(led_PIN,led_VOL);
}
2015年10月5日 星期一
Arduino 與藍芽模組設定 HC-05 HC06
Arduino 與藍芽模組設定 HC-05 HC06
網昱多媒體 網路參考資料 透過藍芽傳輸程式 透過藍芽傳輸程式02
透過藍芽傳輸程式03
AT:測試,回應「OK」
AT+VERSION:回應靭體的版本。
AT+NAMExyz:將裝置名稱改為「xyz」。
AT+PIN1234:將連線密碼換為「1234」。
AT+BAUD4:將 baud rate 換為 9600。
AT+BAUD5:將 baud rate 換為 19200
AT+BAUD6:將 baud rate 換為 38400
AT+BAUD7:將 baud rate 換為 57600
網昱多媒體 網路參考資料 透過藍芽傳輸程式 透過藍芽傳輸程式02
透過藍芽傳輸程式03
HC-06 和 Arduino 的腳位對應如下:
HC-06 VCC → Arduino 5V
HC-06 GND → Arduino GND
HC-06 TXD → Arduino pin 10
HC-06 RXD → Arduino pin 11
#include
SoftwareSerial BTSerial(10, 11); // RX | TX
void setup()
{
Serial.begin(9600);
Serial.println("Enter AT commands:");
BTSerial.begin(9600); // HC-06 current bound rate (default 9600)
}
void loop()
{
// Keep reading from HC-06 and send to Arduino Serial Monitor
if (BTSerial.available())
Serial.write(BTSerial.read());
// Keep reading from Arduino Serial Monitor and send to HC-06
if (Serial.available())
BTSerial.write(Serial.read());
}
執行AT命令修改相關資料
2015年9月18日 星期五
Arduino UNO R3 與光敏電阻感測器
Arduino UNO R3 與光敏電阻感測器
紅外線循跡感測器 (IR Tracker Sensor)
/*
KSM017
入射光強,電阻減小,入射光弱,電阻增大
1、VCC 電源3.3-5V
2、GND 接地
3、DO TTL開關信号输出 數位輸出(0 or 1)
4、AO 模擬信號输出 類比輸出(電壓)
*/
int iAo = A1;
int iDo = 7;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(iDo,INPUT);
pinMode(iAo,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int pr = analogRead(iAo);
//int pr2 = analogRead(iDo);
Serial.print("Dgt=");
Serial.print(digitalRead(iDo));
Serial.print(" | ");
Serial.print("Alg=");
Serial.println(pr);
//int sensorValue = analogRead(1);
//float Rsensor=(float)(1023-sensorValue)*10/sensorValue;
//Serial.println(Rsensor);
delay(1000);
}
紅外線循跡感測器 (IR Tracker Sensor)
/*
KSM017
入射光強,電阻減小,入射光弱,電阻增大
1、VCC 電源3.3-5V
2、GND 接地
3、DO TTL開關信号输出 數位輸出(0 or 1)
4、AO 模擬信號输出 類比輸出(電壓)
*/
int iAo = A1;
int iDo = 7;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(iDo,INPUT);
pinMode(iAo,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int pr = analogRead(iAo);
//int pr2 = analogRead(iDo);
Serial.print("Dgt=");
Serial.print(digitalRead(iDo));
Serial.print(" | ");
Serial.print("Alg=");
Serial.println(pr);
//int sensorValue = analogRead(1);
//float Rsensor=(float)(1023-sensorValue)*10/sensorValue;
//Serial.println(Rsensor);
delay(1000);
}
2015年9月13日 星期日
Arduino UNO R3 與 L298N馬達驅動板 測試
Arduino UNO R3 與 L298N馬達驅動板的配合是蠻多人使用的方式,一開始我也是照網路上大家的接法但是馬達就是不動作,後來又爬文爬好久才知道自己的錯誤點(就是沒把L298N與Arduino 板用GRD串接起來)因此怎麼測試馬達就是不動作。
另外要注意的一點就是你用L298N接2個DC馬達,其中ENA或ENB接在D9、D10腳位控制PWM輸出時,如果又用上Servo時會發現D9、D10失效
我之前組裝尋跡自走車時用L298N控制2顆DC馬達可以走得穩穩的,後來加上一顆SG90 Servo9G腳位接在D12時其中一顆DC馬達ENA就會發生不動作,原以為是電壓不足就外接電源但ENA依然不動作,上網查資料才知道當使用Servo.h函式庫時D9 與 D10 腳位的 PWM 功能就會被停用,所以ENA PWM失效解決辦法就是ENA PWM不要接D9或 D10
Servo伺服器雖然使用了 PWM 訊號來控制馬達轉動,不過,這不表示要使用 Arduino 的 PWM 腳位,只不過根據 Servo library 的說明,除了 Arduino Mega 之外,如果使用了 Arduino 的 Servo 程式庫,D9 與 D10 腳位的 PWM 功能就會被停用。(網路資源) (網路資源)
原文如下
The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins. On the Mega, up to 12 servos can be used without interfering with PWM functionality; use of 12 to 23 motors will disable PWM on pins 11 and 12.
在Arduino板子中的第3、5、6、9、10、11腳可做PWM輸出,其頻率大都為490Hz,而在Uno板子,第5、6腳的頻率約980Hz
L298N使用方式
網路上另一篇文章
這篇就是解決問題的文章
/*
首先來看電源的接法,電源的接法可分成三種,第一種是只提供一組外部電源+12V。
將+12V電源的正負分別接到+12V與GND這兩個插座,同時將+12V上方的Jumper短路,
此時+12V會連接到7805,然後降壓成5V,提供給板子上的IC使用。不需要將5V接到Arduino的板子取得5V電源。
第二種接法同第一種接法,但將5V接到Arduino板子的5V,提供5V給Arduino板,所以Arduino板不需要另外再接5V電源來供電。
第三種接法則是將+12V上方的Jumper拔起來,不透過7805來產生5V,
但必須將+5V的插座接到Arduino的5V,也就是從Arduino提供5V給L298N使用。
以上三種接法,不論哪一種接法,一定要將GND插座與Arduino的GND相連,如此`才有共同的零電壓準位。
*/
/*
int value;
int dir;
void setup() {
pinMode(3, OUTPUT); //接到ENA
pinMode(4, OUTPUT); //接到N1
pinMode(5, OUTPUT); //接到N2
digitalWrite(4, HIGH); //設定馬達正轉
digitalWrite(5, LOW);
value= 255; //PWM輸出值
dir = -1; //value值的遞增或遞減
}
void loop() {
value= value+ dir;
if (value< 80) //當value值小於80時,馬達不會轉,所以dir改成遞增(如果使用3V馬達 上下限值不能大於50~150, 5V 80~255)
dir = 1;
else if (value>= 255) //當value值為最大255時,dir改成遞減
dir = -1;
analogWrite(3, value);
delay(200);
}
*/
程式碼如下:
//也可以用 Analog A0 ~ A5
//Digital PWM 3、5、6、9、10、11
const int motorIn1=5;
const int motorIn2=6;
const int motorIn3=9;
const int motorIn4=10;
void setup() {
pinMode(motorIn1, OUTPUT); //接到N5
pinMode(motorIn2, OUTPUT); //接到N6
pinMode(motorIn3, OUTPUT); //接到N9
pinMode(motorIn4, OUTPUT); //接到N10
}
void loop() {
//正轉
for(int i=150;i<255 i="" p=""> analogWrite(motorIn1,i);
analogWrite(motorIn2,0);
analogWrite(motorIn3,i);
analogWrite(motorIn4,0);
delay(500);
}
analogWrite(motorIn1,0);
analogWrite(motorIn2,0);
analogWrite(motorIn3,0);
analogWrite(motorIn4,0);
delay(2000);
//反轉
for(int i=150;i<255 i="" p=""> analogWrite(motorIn1,0);
analogWrite(motorIn2,i);
analogWrite(motorIn3,0);
analogWrite(motorIn4,i);
delay(500);
}
analogWrite(motorIn1,0);
analogWrite(motorIn2,0);
analogWrite(motorIn3,0);
analogWrite(motorIn4,0);
delay(2000);
}
255>255>
另外要注意的一點就是你用L298N接2個DC馬達,其中ENA或ENB接在D9、D10腳位控制PWM輸出時,如果又用上Servo時會發現D9、D10失效
我之前組裝尋跡自走車時用L298N控制2顆DC馬達可以走得穩穩的,後來加上一顆SG90 Servo9G腳位接在D12時其中一顆DC馬達ENA就會發生不動作,原以為是電壓不足就外接電源但ENA依然不動作,上網查資料才知道當使用Servo.h函式庫時D9 與 D10 腳位的 PWM 功能就會被停用,所以ENA PWM失效解決辦法就是ENA PWM不要接D9或 D10
Servo伺服器雖然使用了 PWM 訊號來控制馬達轉動,不過,這不表示要使用 Arduino 的 PWM 腳位,只不過根據 Servo library 的說明,除了 Arduino Mega 之外,如果使用了 Arduino 的 Servo 程式庫,D9 與 D10 腳位的 PWM 功能就會被停用。(網路資源) (網路資源)
原文如下
The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins. On the Mega, up to 12 servos can be used without interfering with PWM functionality; use of 12 to 23 motors will disable PWM on pins 11 and 12.
在Arduino板子中的第3、5、6、9、10、11腳可做PWM輸出,其頻率大都為490Hz,而在Uno板子,第5、6腳的頻率約980Hz
L298N使用方式
網路上另一篇文章
這篇就是解決問題的文章
/*
首先來看電源的接法,電源的接法可分成三種,第一種是只提供一組外部電源+12V。
將+12V電源的正負分別接到+12V與GND這兩個插座,同時將+12V上方的Jumper短路,
此時+12V會連接到7805,然後降壓成5V,提供給板子上的IC使用。不需要將5V接到Arduino的板子取得5V電源。
第二種接法同第一種接法,但將5V接到Arduino板子的5V,提供5V給Arduino板,所以Arduino板不需要另外再接5V電源來供電。
第三種接法則是將+12V上方的Jumper拔起來,不透過7805來產生5V,
但必須將+5V的插座接到Arduino的5V,也就是從Arduino提供5V給L298N使用。
以上三種接法,不論哪一種接法,一定要將GND插座與Arduino的GND相連,如此`才有共同的零電壓準位。
*/
/*
int value;
int dir;
void setup() {
pinMode(3, OUTPUT); //接到ENA
pinMode(4, OUTPUT); //接到N1
pinMode(5, OUTPUT); //接到N2
digitalWrite(4, HIGH); //設定馬達正轉
digitalWrite(5, LOW);
value= 255; //PWM輸出值
dir = -1; //value值的遞增或遞減
}
void loop() {
value= value+ dir;
if (value< 80) //當value值小於80時,馬達不會轉,所以dir改成遞增(如果使用3V馬達 上下限值不能大於50~150, 5V 80~255)
dir = 1;
else if (value>= 255) //當value值為最大255時,dir改成遞減
dir = -1;
analogWrite(3, value);
delay(200);
}
*/
程式碼如下:
//也可以用 Analog A0 ~ A5
//Digital PWM 3、5、6、9、10、11
const int motorIn1=5;
const int motorIn2=6;
const int motorIn3=9;
const int motorIn4=10;
void setup() {
pinMode(motorIn1, OUTPUT); //接到N5
pinMode(motorIn2, OUTPUT); //接到N6
pinMode(motorIn3, OUTPUT); //接到N9
pinMode(motorIn4, OUTPUT); //接到N10
}
void loop() {
//正轉
for(int i=150;i<255 i="" p=""> analogWrite(motorIn1,i);
analogWrite(motorIn2,0);
analogWrite(motorIn3,i);
analogWrite(motorIn4,0);
delay(500);
}
analogWrite(motorIn1,0);
analogWrite(motorIn2,0);
analogWrite(motorIn3,0);
analogWrite(motorIn4,0);
delay(2000);
//反轉
for(int i=150;i<255 i="" p=""> analogWrite(motorIn1,0);
analogWrite(motorIn2,i);
analogWrite(motorIn3,0);
analogWrite(motorIn4,i);
delay(500);
}
analogWrite(motorIn1,0);
analogWrite(motorIn2,0);
analogWrite(motorIn3,0);
analogWrite(motorIn4,0);
delay(2000);
}
255>255>
2015年6月24日 星期三
Arduino Color Sensor 顏色感應器 TCS230
Arduino Color Sensor 顏色感應器 TCS230
Pin 腳接法
#include
#define S0 6 // Please notice the Pin's define
#define S1 5
#define S2 4
#define S3 3
#define OUT 2
int g_count = 0; // count the frequecy
int g_array[3]; // store the RGB value
int g_flag = 0; // filter of RGB queue
float g_SF[3]; // save the RGB Scale factor
// Init TSC230 and setting Frequency.
void TSC_Init()
{
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(OUT, INPUT);
digitalWrite(S0, LOW); // OUTPUT FREQUENCY SCALING 2%
digitalWrite(S1, HIGH);
}
// Select the filter color
void TSC_FilterColor(int Level01, int Level02)
{
if(Level01 != 0)
Level01 = HIGH;
if(Level02 != 0)
Level02 = HIGH;
digitalWrite(S2, Level01);
digitalWrite(S3, Level02);
}
void TSC_Count()
{
g_count ++ ;
}
void TSC_Callback()
{
switch(g_flag)
{
case 0:
Serial.println("->WB Start");
TSC_WB(LOW, LOW); //Filter without Red
break;
case 1:
Serial.print("->Frequency R=");
Serial.println(g_count);
g_array[0] = g_count;
TSC_WB(HIGH, HIGH); //Filter without Green
break;
case 2:
Serial.print("->Frequency G=");
Serial.println(g_count);
g_array[1] = g_count;
TSC_WB(LOW, HIGH); //Filter without Blue
break;
case 3:
Serial.print("->Frequency B=");
Serial.println(g_count);
Serial.println("->WB End");
g_array[2] = g_count;
TSC_WB(HIGH, LOW); //Clear(no filter)
break;
default:
g_count = 0;
break;
}
}
void TSC_WB(int Level0, int Level1) //White Balance
{
g_count = 0;
g_flag ++;
TSC_FilterColor(Level0, Level1);
Timer1.setPeriod(1000000); // set 1s period
}
void setup()
{
TSC_Init();
Serial.begin(9600);
Timer1.initialize(); // defaulte is 1s
Timer1.attachInterrupt(TSC_Callback);
attachInterrupt(0, TSC_Count, RISING);
delay(4000);
for(int i=0; i<3 i="" p=""> Serial.println(g_array[i]);
g_SF[0] = 255.0/ g_array[0]; //R Scale factor
g_SF[1] = 255.0/ g_array[1] ; //G Scale factor
g_SF[2] = 255.0/ g_array[2] ; //B Scale factor
Serial.println(g_SF[0]);
Serial.println(g_SF[1]);
Serial.println(g_SF[2]);
}
3>
void loop()
{
g_flag = 0;
for(int i=0; i<3 i="" p=""> Serial.println(int(g_array[i] * g_SF[i]));
delay(4000);
}
3>
Pin 腳接法
#include
#define S0 6 // Please notice the Pin's define
#define S1 5
#define S2 4
#define S3 3
#define OUT 2
int g_count = 0; // count the frequecy
int g_array[3]; // store the RGB value
int g_flag = 0; // filter of RGB queue
float g_SF[3]; // save the RGB Scale factor
// Init TSC230 and setting Frequency.
void TSC_Init()
{
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(OUT, INPUT);
digitalWrite(S0, LOW); // OUTPUT FREQUENCY SCALING 2%
digitalWrite(S1, HIGH);
}
// Select the filter color
void TSC_FilterColor(int Level01, int Level02)
{
if(Level01 != 0)
Level01 = HIGH;
if(Level02 != 0)
Level02 = HIGH;
digitalWrite(S2, Level01);
digitalWrite(S3, Level02);
}
void TSC_Count()
{
g_count ++ ;
}
void TSC_Callback()
{
switch(g_flag)
{
case 0:
Serial.println("->WB Start");
TSC_WB(LOW, LOW); //Filter without Red
break;
case 1:
Serial.print("->Frequency R=");
Serial.println(g_count);
g_array[0] = g_count;
TSC_WB(HIGH, HIGH); //Filter without Green
break;
case 2:
Serial.print("->Frequency G=");
Serial.println(g_count);
g_array[1] = g_count;
TSC_WB(LOW, HIGH); //Filter without Blue
break;
case 3:
Serial.print("->Frequency B=");
Serial.println(g_count);
Serial.println("->WB End");
g_array[2] = g_count;
TSC_WB(HIGH, LOW); //Clear(no filter)
break;
default:
g_count = 0;
break;
}
}
void TSC_WB(int Level0, int Level1) //White Balance
{
g_count = 0;
g_flag ++;
TSC_FilterColor(Level0, Level1);
Timer1.setPeriod(1000000); // set 1s period
}
void setup()
{
TSC_Init();
Serial.begin(9600);
Timer1.initialize(); // defaulte is 1s
Timer1.attachInterrupt(TSC_Callback);
attachInterrupt(0, TSC_Count, RISING);
delay(4000);
for(int i=0; i<3 i="" p=""> Serial.println(g_array[i]);
g_SF[0] = 255.0/ g_array[0]; //R Scale factor
g_SF[1] = 255.0/ g_array[1] ; //G Scale factor
g_SF[2] = 255.0/ g_array[2] ; //B Scale factor
Serial.println(g_SF[0]);
Serial.println(g_SF[1]);
Serial.println(g_SF[2]);
}
3>
void loop()
{
g_flag = 0;
for(int i=0; i<3 i="" p=""> Serial.println(int(g_array[i] * g_SF[i]));
delay(4000);
}
訂閱:
文章 (Atom)


