ความรู้แน่น ฟรีสำหรับชุมชน ArduinoAll ที่นี่เท่านั้น

ฟรีและดีที่สุด คอร์สอบรม Arduino + NodeMCU
ทำเพื่อแบ่งปัน ห้ามนำไปจำหน่าย หรือเก็บเงินค่าเรียน
  !!!


  


AllNewStep รับประกันคุณภาพทุกชิ้น วันจันทร์-ศุกร์แจ้งชำระสินค้าก่อน 14.00 จัดส่งทันทีวันนี้ค่ะ

กรุงเทพ /ภาคกลาง ได้พรุ่งนี้


*** สินค้าทุกชิ้น ถ้าสามารถทำรายการสั่งซื้อได้ แสดงว่ามีครบทุกรายการค่ะ *** 

พิมพ์ค้นหาบทความ หัวข้อกระทู้ และสินค้าในเว็บ AllNewStep ได้ที่นี่
QUOTE 

บอร์ดmega 2560 กับค่า PWM ที่เพี้ยน

สัณทณัฐ อินพรม

พอดีว่าผมกำลัง งงกับค่า 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);  

  }

}  

QUOTE 
ความคิดเห็นที่ #1
เจ้าของร้าน

PWM ใช้คำสั่ง analogWrite(); ในการสั่งงาน

โดยจะได้ไฟ 0-5V จากค่า 0-255

การใช้งานอ้างอิงจากเว็บ arduino.cc  ดังนี้

https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/

Notes and Warnings

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 ไปใช้กับขาอื่นทดลองแทนครับ

 

 

แสดงความคิดเห็นที่ 1-1 จากทั้งหมด 1 ความคิดเห็น
ขาย ARDUINO
คุณภาพ อันดับ 1

ได้รับรางวัลร้านยอดเยี่ยม
ตั้งแต่ปี 2558
ขาย Arduino
วีดีโอสอน Arduino

สอน esp8266

สอน Arduino IoT
สอน Arduino แบบเร็ว

สอน NodeMCU

อุปกรณ์ Arduino

MEMBER ZONE

พูดคุย-สอบถาม