พอดีว่าผมกำลัง งงกับค่า pwm ที่มันออกมาอะครับ ผมสั่งที่ 255 ปกติมันควรมีค่าที่ 5 V ใช่ไหมครับ เเต่ของผมออก 2.5 V เท่านั้น โดยจับประเด็นได้ว่า เมื่อลด delay ลง ระดับ microsecond ค่า pwm เพี้ยนมาก เมื่อกดสวิตซ์เเล้ว ค่าpwm มันไม่เป็นที่ควรจะได้ครับ ได้โปรดช่วยผมได้หาทางออกมานานเเล้วยังเเก้ไม่หายเลยครับ
ตัวอย่างครับ
//Part Switch
int GATE1=2;
int GATE2=3;
int GATE3=5;
int GATE4=6;
int GATE5=7;
int GATE6=8;
//Part Systems
int Value=A0;
int Speed;
int SpeedValue;
int SpeedPower = 150;
//Switch
int sw=4;
int switchzone;
void setup() {
pinMode(GATE1, OUTPUT);
pinMode(GATE2, OUTPUT);
pinMode(GATE3, OUTPUT);
pinMode(GATE4, OUTPUT);
pinMode(GATE5, OUTPUT);
pinMode(GATE6, OUTPUT);
pinMode(sw,INPUT_PULLUP);
Serial.begin(9600);
}
void loop()
{
int switchzone = digitalRead(sw);
//Speed=analogRead(Value);
// SpeedValue=map(Speed,0,1023,800,10000);
if(switchzone == HIGH)
{
Speed=analogRead(Value);
SpeedValue=map(Speed,0,1023,800,10000);
Serial.println(SpeedValue);
//Step 1
analogWrite(GATE1,LOW);
analogWrite(GATE2,LOW);
analogWrite(GATE3,LOW);
analogWrite(GATE4,SpeedPower);
analogWrite(GATE5,SpeedPower);
analogWrite(GATE6,LOW);
delayMicroseconds(SpeedValue);
//Step 2
analogWrite(GATE1,SpeedPower);
analogWrite(GATE2,LOW);
analogWrite(GATE3,LOW);
analogWrite(GATE4,SpeedPower);
analogWrite(GATE5,LOW);
analogWrite(GATE6,LOW);
delayMicroseconds(SpeedValue);
//Step 3
analogWrite(GATE1,SpeedPower);
analogWrite(GATE2,LOW);
analogWrite(GATE3,LOW);
analogWrite(GATE4,LOW);
analogWrite(GATE5,LOW);
analogWrite(GATE6,SpeedPower);
delayMicroseconds(SpeedValue);
//Step 4
analogWrite(GATE1,LOW);
analogWrite(GATE2,LOW);
analogWrite(GATE3,SpeedPower);
analogWrite(GATE4,LOW);
analogWrite(GATE5,LOW);
analogWrite(GATE6,SpeedPower);
delayMicroseconds(SpeedValue);
//Step 5
analogWrite(GATE1,LOW);
analogWrite(GATE2,SpeedPower);
analogWrite(GATE3,SpeedPower);
analogWrite(GATE4,LOW);
analogWrite(GATE5,LOW);
analogWrite(GATE6,LOW);
delayMicroseconds(SpeedValue);
//Step 6
analogWrite(GATE1,LOW);
analogWrite(GATE2,SpeedPower);
analogWrite(GATE3,LOW);
analogWrite(GATE4,LOW);
analogWrite(GATE5,SpeedPower);
analogWrite(GATE6,LOW);
delayMicroseconds(SpeedValue);
}
else
{
Serial.println(" System OFF");
analogWrite(GATE1,LOW);
analogWrite(GATE2,LOW);
analogWrite(GATE3,LOW);
analogWrite(GATE4,LOW);
analogWrite(GATE5,LOW);
analogWrite(GATE6,LOW);
delayMicroseconds(SpeedValue);
}
}
PWM ใช้คำสั่ง analogWrite(); ในการสั่งงาน
โดยจะได้ไฟ 0-5V จากค่า 0-255
การใช้งานอ้างอิงจากเว็บ arduino.cc ดังนี้
https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/
The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles. This is because of interactions with the millis()
and delay()
functions, which share the same internal timer used to generate those PWM outputs. This will be noticed mostly on low duty-cycle settings (e.g. 0 - 10) and may result in a value of 0 not fully turning off the output on pins 5 and 6.
เอาต์พุตแบบ PWM ที่ขา 5 กับ 6 มีการทำงานเกี่ยวข้องกับฟังชั่น millis() และ delay() ลองเปลี่ยน pwm ไปใช้กับขาอื่นทดลองแทนครับ