2015年11月8日 星期日

Arduino 4:LED 1W

要控制1W的LED必須要有足夠的電流:





Arduino 可利用[IRF520]當作開關,控制1W LED:

加上按鈕開關,控制LED是否亮起:




加入[可變電阻]當作輸入,控制1W LED的閃爍頻率:


  1. const int motorPin =  9;      // the number of the motor pin
  2. const int potentialPin= 0;  // potential meter
  3. int   value = 0;
  4. void setup() {
  5.                                    // initialize the motor pin as an output:
  6.   pinMode(motorPin, OUTPUT);      
  7.                                         // initialize the switch pin as an input:
  8.                                         // pinMode(potentialPin, INPUT);  No need declare Analog
  9.   Serial.begin(9600);       //顯示在螢幕上
  10. }
  11. void loop(){
  12.    
  13.   value = analogRead(potentialPin);
  14.               // Serial.print("value=");
  15.               // Serial.println(value);            
  16.   if(value == 0){      
  17.           digitalWrite(motorPin, LOW);   
  18.     delay(1000);
  19.    }
  20.   else{
  21.      int frequency = map(value, 0, 1023, 0,30);  //控制頻率
  22.                // Serial.print("freq=");
  23.                // Serial.println(frequency);
  24.        //int interval  = (1/ frequency) *1000* 0.5;  //週期的一半  (毫秒)
  25.      int interval  = 500/ frequency;
  26.              // Serial.print("interval=");
  27.              // Serial.println(interval);    
  28.      digitalWrite(motorPin, HIGH);
  29.        delay(interval);
  30.      digitalWrite(motorPin, LOW);
  31.        delay(interval);
  32.     }
  33.  }

將閃爍的頻率,顯示在LCD上:


  1. //-------------------------   LCD 
  2. //Library version:1.1
  3. #include <Wire.h>
  4. #include <LiquidCrystal_I2C.h>
  5. #if defined(ARDUINO) && ARDUINO >= 100
  6. #define printByte(args)  write(args);
  7. #else
  8. #define printByte(args)  print(args,BYTE);
  9. #endif
  10. LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
  11.                   //    ^'20'改成"27"  ,for "PCF8574"
  12.                   //    SDA  -->   "A4"
  13.                   //    SCL  -->   "A5"
  14. //-------------------------

  15. // named constants for the switch and motor pins
  16. const int motorPin =  9; // the number of the motor pin
  17. const int potentialPin= 0;  // potential meter
  18. int   value = 0;

  19. void setup() {
  20.      // initialize the motor pin as an output:
  21.   pinMode(motorPin, OUTPUT);      
  22.      // initialize the switch pin as an input:
  23.   // pinMode(potentialPin, INPUT);  No need declare Analog
  24.   Serial.begin(9600);
  25.      //-------------------------   LCD  
  26.       lcd.init();                      // initialize the lcd 
  27.       lcd.backlight();

  28.      //   lcd.setCursor(0, 0);
  29.      //   lcd.print ("freq. =");
  30.      // lcd.setCursor(0, 1);
  31.      // lcd.print   ("period=");
  32.      //-------------------------   LCD 
  33. }

  34. void loop(){
  35.    
  36.   value = analogRead(potentialPin);
  37.           //  Serial.print("value=");
  38.           //  Serial.println(value);            
  39.   if(value == 0){      
  40.           digitalWrite(motorPin, LOW);   
  41.     delay(1000);
  42.          //-------------------------   LCD
  43.         lcd.clear(); 
  44.             lcd.setCursor(0, 0);
  45.             lcd.print ("freq. =");
  46.           lcd.setCursor(0, 1);
  47.           lcd.print   ("period=");
  48.                 lcd.setCursor(8, 0);
  49.                 lcd.print(0);

  50.               lcd.setCursor(8, 1);
  51.               lcd.print(0);
  52.          //-------------------------   LCD
  53.    }
  54.   else{
  55.      int frequency = map(value, 0, 1023, 0,30);  //控制頻率
  56.              // Serial.print("freq=");
  57.              // Serial.println(frequency);
  58.      //int interval  = (1/ frequency) *1000* 0.5;  //週期的一半  (毫秒)
  59.      int interval  = 500/ frequency;
  60.              // Serial.print("interval=");
  61.              // Serial.println(interval);       
  62.         //-------------------------   LCD
  63.         lcd.clear(); 
  64.             lcd.setCursor(0, 0);
  65.             lcd.print ("freq. =");
  66.           lcd.setCursor(0, 1);
  67.           lcd.print   ("period=");
  68.         
  69.                 lcd.setCursor(8, 0);
  70.                 lcd.print(frequency);

  71.               lcd.setCursor(8, 1);
  72.               lcd.print(2*interval);
  73.                lcd.setCursor(13, 1);
  74.               lcd.print("ms");
  75.               //-------------------------   LCD
  76.      digitalWrite(motorPin, HIGH);
  77.        delay(interval);
  78.      digitalWrite(motorPin, LOW);
  79.        delay(interval);
  80.   }
  81.     
  82. }

POV_視覺暫留


沒有留言:

張貼留言