วันศุกร์ที่ 29 สิงหาคม พ.ศ. 2557

Assignment1# Arduino LED::RGB


1# LED-RGB



Part.

  1. LED-RGB Commond Cathods          1               ตัว
  2. r 330 ohm                                      6               ตัว
  3. push_button                                   3               ตัว
  4. Arduino Broad                                1               บอร์ด
  5. สายไฟสำหรับต่อวงจร
  6. บอร์ดต่อทดลอง

Schematic.






Code.

1. TEST_SW : ทดสอบการทำงานของปุ่มกดทั้ง 3 โดย เมื่อปุ่มประจำสีใดๆ(R,G,B)ถูกกด จะทำให้ เพิ่มค่า Duty ของสีๆนั้นขึ้น ในช่วง 0-255 เมื่อครบแล้วจึงกลับไปเริ่มที่ 0 ใหม่

// file : Test_Push_Button.ino

#define red_in 10
#define green_in 9
#define blue_in 8

uint8_t red_duty = 0;   // RED_Duty (0-255)
uint8_t green_duty = 0; // GREEN_Duty (0-255)
uint8_t blue_duty = 0;  // BLUE_Duty (0-255)
boolean s1 = true;      // State_1
boolean s2 = false;     // State_2

void setup(){
  Serial.begin(9600);
  pinMode(red_in, INPUT);
  pinMode(green_in, INPUT);
  pinMode(blue_in, INPUT);
}

void loop(){
  if( s1 ){
    if((digitalRead(red_in) == LOW)||(digitalRead(green_in) == LOW)||(digitalRead(blue_in) == LOW)){ 
      delay(10); 
      s1 = false; 
      s2 = true; 
    }
  }
  if( s2 ){
    // Increase Duty ..
    if(digitalRead(red_in) == LOW){ delayMicroseconds(100); red_duty += 8; }
    else if(digitalRead(green_in) == LOW){ delayMicroseconds(100); green_duty += 8; }
    else if(digitalRead(blue_in) == LOW){ delayMicroseconds(100); blue_duty += 8; }
    
    if((digitalRead(red_in) == HIGH)||(digitalRead(green_in) == HIGH)||(digitalRead(blue_in) == HIGH)){ 
      delay(10); 
      s2 = false; 
      s1 = true; 
    }
  }
  
  // Check
  Serial.print("R");
  Serial.print( red_duty );
  Serial.print(", G");
  Serial.print( green_duty );
  Serial.print(", B");
  Serial.println(blue_duty );
}


( รูปการต่อวงจรทดสอบปุ่มกด )

( แสดงค่า Duty ของแต่ละสี ณ เวลานั้น )



2. Class RGB_LED : เป็น Class ที่เก็บหมายเลข Pin ของแต่ละสีเอาไว้ และมี Mathods สำหรับเขียนค่าให้ RGB_LED เปล่งแสงแต่ละสีออกมา

- ประกอบด้วย 
   - Constructors
      RGB_LEDint red_pin, int green_pin, int blue_pin )
      * ไว้ใช้เก็บหมายเลข Pin แต่ละสี
   -  Methods 
      set_Coloruint8_t redDuty, uint8_t greenDutyuint8_t blueDuty )
      * ไว้ใช้สำหรับกำหนดค่าสีของ LED_RGB ตามพารามิตเตอร์ที่รับมา

// file : RGB_LED.h

#include <Arduino.h>
#include <String.h>
#include <iostream>
using namespace std;

class RGB_LED {
 // Attribute
 private:
  int _red_pin;
  int _green_pin;
  int _blue_pin;
 public:
  RGB_LED(int red_pin, int green_pin, int blue_pin );
  void set_Color(uint8_t redDuty, uint8_t greenDuty, uint8_t blueDuty);
};

// file : RGB_LED.cpp

#include "RGB_LED.h"

RGB_LED::RGB_LED(int red_pin, int green_pin, int blue_pin ){ // Constructors Set Pin R,G and B
 _red_pin = red_pin;
 _green_pin = green_pin;
 _blue_pin = blue_pin;
 pinMode( _red_pin, OUTPUT );
   pinMode( _green_pin, OUTPUT );
   pinMode( _blue_pin, OUTPUT );
}

// Method Set_Color of RGB LED
void RGB_LED::set_Color(uint8_t redDuty, uint8_t greenDuty,uint8_t blueDuty){
 analogWrite( _red_pin, redDuty );
   analogWrite( _green_pin, greenDuty );
   analogWrite( _blue_pin, blueDuty );
}


3. Arduino_CODE : นำ 2 ส่วนที่กล่าวไปด้านบน มารวมกัน และใช้ Delay จากการนับตัวแปร Timer_1 ตั้ง Interrupt ไว้ที่ 1 ms

// file : RGB_LED_Ver1.ino 

#include <RGB_LED.h>

// Set_pin
#define red_in 10                            // input_SW_RED
#define green_in 9                           // input_SW_GREEN
#define blue_in 8                            // input_SW_BLUE

int redPin = 6;                              // output_RED
int greenPin = 5;                            // output_GREEN
int bluePin = 3;                             // output_BLUE

uint8_t red_duty = 0;                        // RED_Duty (0-255)
uint8_t green_duty = 0;                      // GREEN_Duty (0-255)
uint8_t blue_duty = 0;                       // BLUE_Duty (0-255)
boolean s1 = true;                           // State_1
boolean s2 = false;                          // State_2

// Delay
boolean state_debounce = false;
boolean debounce = false;
uint8_t count10ms = 10;
boolean state_100ms = false;
boolean past100ms = false;
uint8_t count100ms = 100;

RGB_LED obj( redPin, greenPin, bluePin );    // Create object from class RGB_LED

void setup(){
  T1_init();                                 // Set Timer_1 OverflowInterrupt 1 ms
  pinMode(red_in, INPUT);                    // Input red_sw
  pinMode(green_in, INPUT);                  // Input green_sw
  pinMode(blue_in, INPUT);                   // Input blue_sw
  Serial.begin(9600);
}

void loop(){
  
  // State_Input_SW
  if( s1 ){
    if((digitalRead(red_in) == LOW)||(digitalRead(green_in) == LOW)||(digitalRead(blue_in) == LOW)){
      state_debounce = true; 
      if( debounce ){                        // Debounce SW => Delay 10 ms
        s1 = false; 
        s2 = true;
        debounce = false;
      }
    }
  }
  if( s2 ){
    // Increase Duty ..(With Delay 100 ms)
    if(digitalRead(red_in) == LOW){
      state_100ms = true;                    
      if( past100ms ){ red_duty += 8; past100ms = false;}
    }
    else if(digitalRead(green_in) == LOW){ 
      state_100ms = true;
      if( past100ms ){ green_duty += 8; past100ms = false;}
    }
    else if(digitalRead(blue_in) == LOW){
      state_100ms = true;
      if( past100ms ){ blue_duty += 8; past100ms = false;}
    }
    
    if((digitalRead(red_in) == HIGH)||(digitalRead(green_in) == HIGH)||(digitalRead(blue_in) == HIGH)){ 
      state_debounce = true; 
      if( debounce ){                        // Debounce SW => Delay 10 ms
        s2 = false; 
        s1 = true;
        debounce = false;
      } 
    }
  }
  
  // Check
  Serial.print("R");
  Serial.print( red_duty );
  Serial.print(", G");
  Serial.print( green_duty );
  Serial.print(", B");
  Serial.println(blue_duty );
  
  obj.set_Color( red_duty, green_duty, blue_duty);      // Set Color
}

/* T1_init from Jannarong */
void T1_init()
{
   TCNT1  = 0;
   OCR1A  = 0;
   OCR1B  = 0;
   ICR1   = 0;
   TCCR1A = 0;
   TCCR1B = 0;
   TIMSK1 = 0; /* DISABLE ALL INTERRUPTS */
   TIFR1  = (1<<ICF1) | (1<<OCF1B) | (1<<OCF1A) | (1<<TOV1);
   
   TCNT1  = 65286; /* 2^16 - 250 = 65286 => 1 msec */
   /* USE TIMER/COUNT1 IN NORMAL MODE(NO OUTPUT COMPARE, NO PWM) */
   TCCR1B |= (1<<CS11) | (1<<CS10); /* PRESCALER = 64 */
   TIMSK1 = (1<<TOIE1); /* Timer/Countern, Overflow Interrupt Enable */
}

ISR(TIMER1_OVF_vect){
   TCNT1 = 65286;
   // Delay => debounce_SW
   if( state_debounce ){
     if( count10ms <= 0 ){
       count10ms = 10;
       state_debounce = false;
       debounce = true;
     }else{ count10ms -= 1; }
   }else{ count10ms = 10; }
   
   // Delay => increase_Duty
   if( state_100ms ){
     if( count100ms <= 0 ){
       count100ms = 100;
       state_100ms = false;
       past100ms = true;
     }else{ count100ms -= 1; }
   }else{ count100ms = 100; }
   
}



( รูปภาพแสดงการทำงาน )








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

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