วันพุธที่ 14 มีนาคม พ.ศ. 2561

ส่งงานครั้งที่3

Arduino ควบคุมหลอด LED RGB


int RGBPin[] = { 9, 10, 11 }; //กำหนดตัวเเปรRGBPinที่ขา 9,10,11 
void setup() {
  pinMode(RGBPin[0], OUTPUT);
  pinMode(RGBPin[1], OUTPUT);
  pinMode(RGBPin[2], OUTPUT);
}

void SetRGB(int r, int g, int b) { //เปิดฟังก์ชั่น SetRGB กำหนดตัวแปร r,g,b
  analogWrite(RGBPin[0], r); //ส่งค่า r ที่ RGBPin ตำแหน่งที่ 0
  analogWrite(RGBPin[1], g); //ส่งค่า g ที่ RGBPin ตำแหน่งที่ 1
  analogWrite(RGBPin[2], b); //ส่งค่า b ที่ RGBPin ตำแหน่งที่ 2
}

void loop() {
  SetRGB(255, 0, 0); //อ่านค่าจาก SetRGB r=255,g=0,b=0 (Red)
  delay(500); //หน่วงเวลา 5 วินาที
  SetRGB(0, 255, 0); //อ่านค่าจาก SetRGB r=0,g=255,b=0 (Green)
  delay(500);
  SetRGB(0, 0, 255); //อ่านค่าจาก SetRGB r=0,g=0,b=255 (Blue)
  delay(500);
  SetRGB(255, 255, 0); //อ่านค่าจาก SetRGB r=255,g=255,b=0 (Yellow)
  delay(500);
  SetRGB(0, 255, 255); //อ่านค่าจาก SetRGB r=0,g=255,b=255 (Sky blue)
  delay(500);
  SetRGB(255, 0, 255); //อ่านค่าจาก SetRGB r=255,g=0,b=255 (Pink)
  delay(500);
  SetRGB(255, 255, 255); //อ่านค่าจาก SetRGB r=255,g=255,b=255 (White)
  delay(500);
}

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

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

Mini Project

 Control Lamp 220v  With LDR const int lamp = 12; const int ldrPin=A0; #include <Wire.h> #include <LiquidCrystal_I2C.h&g...