วันพฤหัสบดีที่ 28 สิงหาคม พ.ศ. 2557

RGB LED / PWM-based Dimming

โจทย์ Warm-up ทบทวน Arduino และการเขียนโปรแกรมภาษา C/C++

1) จงเขียนโค้ดสำหรับ Arduino และวงจรที่ทำงานได้ตามข้อกำหนดต่อไปนี้ [RGB LED / PWM-based Dimming]

  • วงจรทำงานโดยใช้ระดับแรงดันสำหรับ I/O ที่ 5V เท่านั้น
  • มีปุ่มกด 3 ปุ่ม (ให้ชื่อว่า R, G, B) ทำงานแบบ Pull-up (active-low) ให้ต่อวงจรปุ่มกดเอง เพื่อใช้งานกับบอร์ด Arduino
  • มีเอาต์พุต 3 ขา ต่อกับวงจร RGB LED (จะใช้แบบ Common-Anode หรือ Common-Cathode ก็ได้) พร้อมตัวต้านทานจำกัดกระแส 3 ตัว
  • เขียนโค้ดด้วยภาษา C++ สำหรับ Arduino เพื่อสร้าง Class ที่มีชื่อว่า "RGB_LED"
  • กำหนดให้ constructor สำหรับคลาส RGB_LED เป็นดังนี้                                          RGB_LED( int red_pin, int_green_pin, int blue_pin );                                                        โดยรับค่ามาเป็นหมายเลขของ I/O pins สำหรับ 3 ขาของ Arduino ที่จะถูกใช้งานเป็นเอาต์พุตแบบ PWM
  • มีเมธอดอย่างเช่น                                                                                                                      void setRed( int duty_cycle ), void setGreen( int duty_cycle ),                                           void setBlue( int duty_cycle ) เพื่อใช้กำหนดค่า duty cycle ของขาเอาต์พุต PWM และใช้ในการกำหนดความสว่างของแต่ละสี ใช้คำสั่ง analogWrite() ในการกำหนดค่า
  • กำหนดสมาชิก instance members ตามความจำเป็น เช่น ค่า duty cycles สำหรับแต่ละสี
  • ใช้คลาสดังกล่าวในการเขียนโค้ด (สร้าง object จากคลาสดังกล่าวและเรียกใช้เมธอด) เพื่อสาธิตการทำงานร่วมกับฮาร์ดแวร์จริง
  • เมื่อกดปุ่ม R, G หรือ B แล้วปล่อยแต่ละครั้ง จะทำให้ค่า duty cycle ของสีดังกล่าวเพิ่มขึ้นทีละ 8 ถ้าค่า duty cycle เกิน 255 ให้วนกลับมาเริ่มที่ 0 ใหม่ (ค่าเริ่มต้นสำหรับ duty cycles เป็น 0)
รูปที่ 1.1 แสดงภาพของวงจร LED จากโปรแกรมeagleโดยต่อแบบ active-low
หมายเหตุ ขาpinที่ต่อวงจรจากโปรแกรมeagle อาจไม่เหมือนกับที่ต่อในวงจรจริง

รูปที่ 1.2 แสดงภาพของวงจร LED โดยต่อแบบ active-low


ส่วนของโค้ด

#include "RGB_LED.h"


RGB_LED led(2,3,4,true);   // สร้าง RGB จาก Class

int IN_IN[] = {7,8,10} ;            // input ข้อมูลที่ pin 7,8 และ 10

int count[] ={0,0,0};                // Light

int ch[] = {1,1,1} ;                   // check status

int val[] = {0,0,0} ;                  // check bottom

void setup() {

  Serial.begin(9600);

  pinMode(IN_IN[0],INPUT);

  pinMode(IN_IN[1],INPUT);

  pinMode(IN_IN[2],INPUT);

}



void loop() {

  

  for(int i = 0 ; i < 3 ; i++){                              // check bottom +light

    val[i] = digitalRead(IN_IN[i]);

    if(val[i] == 0 && ch[i] == 1){ch[i] = 0;}

    if(val[i] == 1 && ch[i] == 0){count[i] = count[i]+8;
    ch[i] = 1;
    if(count[i] > 255) {count[i] = 0;}
   }
 }
    led.setColorR( 254 - count[0] );                    // lightRED
    led.setColorG( 254 - count[1] );                    // lightGreen
    led.setColorB( 254 - count[2] );                    // lightBule
  
  for(int j = 0 ; j < 3 ; j++){
   Serial.println((int) count[j]);  
   Serial.println((int) val[j]); }
  delay(100);


2) เหมือนข้อ 1 แต่เปลี่ยนพฤติกรรมการกดปุ่ม: ถ้ากดปุ่ม R, G หรือ B ค้างไว้อย่างน้อย 100 msec จะเพิ่มค่าขึ้นทีละ 8 (แล้วเริ่มนับเวลาใหม่) ถ้าค่า duty cycle เกิน 255 ให้วนกลับมาเริ่มที่ 0 ใหม่

ส่วนของโค้ด

#include "RGB_LED.h"

RGB_LED led(2,3,4,true);           // สร้าง RGB จาก Class
int IN_IN[] = {7,8,10} ;                    // input ข้อมูลที่ pin 7,8 และ 10
int count[] ={0,0,0};                        // Light
int ch[] = {1,1,1} ;                           // check status
int t[] = {0,0,0} ;                              // check time
int val[] = {0,0,0} ;                          // check bottom
void setup() {
  Serial.begin(9600);
  pinMode(IN_IN[0],INPUT); 
  pinMode(IN_IN[1],INPUT);
  pinMode(IN_IN[2],INPUT);
}

void loop() {
  
  for(int i = 0 ; i < 3 ; i++){               // check bottom +light
    val[i] = digitalRead(IN_IN[i]);
    if(val[i] == 0 && ch[i] == 1){
      t[i] = t[i] + 1;
    if(t[i] > 50){
    count[i] = count[i]+8;
    if(count[i] > 255) {count[i] = 0;}
    ch[i] = 0;
  }
}
    if(val[i] == 1 && ch[i] == 0){
      t[i] = 0;
    ch[i] = 1;
    
  }
}

    led.setColorR( 254 - count[0] );                  // lightRED
    led.setColorG( 254 - count[1] );                  // lightGreen
    led.setColorB( 254 - count[2] );                  // lightBule
  
  for(int j = 0 ; j < 3 ; j++){
   Serial.println((int) count[j]);  
   Serial.println((int) val[j]); }
  delay(100);







 PWM = 0%




LED RGB ดับ




PWM = 100%



LED RGB สว่าง



LED RGB สว่างทุกดวง




Downloadcode


ไม่มีความคิดเห็น:

แสดงความคิดเห็น