รูปแบบ | คำอธิบาย |
---|---|
pinMode(pin, mode); | pin: หมายเลขขาที่ต้องการเซตโหมด mode: INPUT, OUTPUT, INPUT_PULLUP |
digitalWrite(pin,value); | pin: หมายเลข ขาที่ต้องการเขียนลอจิกออกพอร์ต value: ค่าลอจิกที่ต้องการส่งออก HIGH or LOW |
delay(ms); | ms: ตัวเลขที่หยุดค้างของเวลาหน่วยมิลลิวินาที (unsigned long) |
รูปแบบ | คำอธิบาย |
---|---|
ledcSetup(channel,freq,resolution); | ฟังก์ชั่นกำหนดค่าใน Timer -channel หมายเลขช่องของ Timer ใช้งานได้ 16 ช่องค่า 0-15 -freq ค่าความถี่ที่ใช้สร้างสัญญาณ PWM -resolution ค่าความละเอียดของ Duty cycle 1-16 bit เช่นถ้าใช้ 8 bit ค่า Duty cycle ที่กำหนดจะมีค่า 0-255 หมายถึง 0-100% |
ledcAttachPin(GPIO, channel); | ฟังก์ชั่นกำหนดขาพอร์ตที่ใช้งานกับช่องของ Timer -GPIO หมายเลขขาพอร์ตที่ใช้งาน -channel หมายเลขช่องของ Timer ที่เลือกใช้งานกับขาพอร์ตที่ระบุ |
ledcWrite(channel, dutycycle) | ฟังก์ชั่นสั่งการให้ Timer สร้างสัญญาณ PWM -channel หมายเลขช่องของ Timer ที่ต้องการสั่งการ -dutycycle ค่า Duty cycle ที่ต้องการให้ Timer สร้างขึ้น |
#define LED1 21
/* setting PWM properties */
const int freq = 5000;
const byte ledChannel = 0;
const byte resolution = 8;
void setup(){
ledcSetup(ledChannel, freq, resolution);
ledcAttachPin(LED1, ledChannel);
}
void loop(){
for(int duty = 0; duty <= 255; duty++){
ledcWrite(ledChannel, duty);
delay(15);
}
for(int duty = 255; duty >=0; duty--){
ledcWrite(ledChannel, duty);
delay(15);
}
}
#define LED1 21
#define LED2 19
#define LED3 18
#define LED4 5
byte fade[] = {0,0,0,0,200,255, 200, 150, 100, 50, 10, 0, 0, 0,0};
void setup() {
ledcSetup(0, 5000, 8); /*ledcSetup(Channel,Freq,Resolution) */
ledcSetup(1, 5000, 8);
ledcSetup(2, 5000, 8);
ledcSetup(3, 5000, 8);
ledcAttachPin(LED1, 0); /*ledcAttachPin(Pin,Channel) */
ledcAttachPin(LED2, 1);
ledcAttachPin(LED3, 2);
ledcAttachPin(LED4, 3);
}
void loop() {
for (int i = 0; i < 11; i++) {
ledcWrite(0, fade[i]);
ledcWrite(1, fade[i + 1]);
ledcWrite(2, fade[i + 2]);
ledcWrite(3, fade[i + 3]);
delay(75);
}
}