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_視覺暫留
沒有留言:
張貼留言