Guest User

sPWM

a guest
Jul 13th, 2017
313
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. This code was based on Swagatam SPWM code with changes made to remove errors. Use this code as you would use any other Swagatam’s works.
  3. Atton Risk 2017
  4. */
  5. const int sPWMArray[] = {500,500,750,500,1250,500,2000,500,1250,500,750,500,500}; // This is the array with the SPWM values change them at will
  6. const int sPWMArrayValues = 13; // You need this since C doesn’t give you the length of an Array
  7. // The pins
  8. const int sPWMpin1 = 10;
  9. const int sPWMpin2 = 9;
  10. // The pin switches
  11. bool sPWMpin1Status = true;
  12. bool sPWMpin2Status = true;
  13.  
  14. void setup()
  15. {
  16. pinMode(sPWMpin1, OUTPUT);
  17. pinMode(sPWMpin2, OUTPUT);
  18. }
  19.  
  20. void loop()
  21. {
  22. // Loop for pin 1
  23. for(int i(0); i != sPWMArrayValues; i++)
  24. {
  25. if(sPWMpin1Status)
  26. {
  27. digitalWrite(sPWMpin1, HIGH);
  28. delayMicroseconds(sPWMArray[i]);
  29. sPWMpin1Status = false;
  30. }
  31. else
  32. {
  33. digitalWrite(sPWMpin1, LOW);
  34. delayMicroseconds(sPWMArray[i]);
  35. sPWMpin1Status = true;
  36. }
  37. }
  38.  
  39. // Loop for pin 2
  40. for(int i(0); i != sPWMArrayValues; i++)
  41. {
  42. if(sPWMpin2Status)
  43. {
  44. digitalWrite(sPWMpin2, HIGH);
  45. delayMicroseconds(sPWMArray[i]);
  46. sPWMpin2Status = false;
  47. }
  48. else
  49. {
  50. digitalWrite(sPWMpin2, LOW);
  51. delayMicroseconds(sPWMArray[i]);
  52. sPWMpin2Status = true;
  53. }
  54. }
  55. }
RAW Paste Data