diff options
| author | Abel Tim <abel@abel-ms7c95.home> | 2025-03-17 21:43:59 +0100 |
|---|---|---|
| committer | Abel Tim <abel@abel-ms7c95.home> | 2025-03-17 21:43:59 +0100 |
| commit | 9c8f7e2f1101b4bb8e0c5459e14af253f150c8b7 (patch) | |
| tree | f6baef3949190537fb37f3ac21b965f83c8f7407 /CODE/debugprewk/debug/motordriver/softPWM/softPWM.ino | |
| parent | bf474ec55fce14f778efa1c1e8118d69a093bf07 (diff) | |
| download | robotica-9c8f7e2f1101b4bb8e0c5459e14af253f150c8b7.tar.gz robotica-9c8f7e2f1101b4bb8e0c5459e14af253f150c8b7.zip | |
code
Diffstat (limited to 'CODE/debugprewk/debug/motordriver/softPWM/softPWM.ino')
| -rw-r--r-- | CODE/debugprewk/debug/motordriver/softPWM/softPWM.ino | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/CODE/debugprewk/debug/motordriver/softPWM/softPWM.ino b/CODE/debugprewk/debug/motordriver/softPWM/softPWM.ino new file mode 100644 index 0000000..ab74715 --- /dev/null +++ b/CODE/debugprewk/debug/motordriver/softPWM/softPWM.ino @@ -0,0 +1,46 @@ +#include <TimerThree.h> +#include <Arduino.h> + +int pwmPin = 3; + +volatile int PWMval = 0; // PWM value 0-100 + +void setup() { + pinMode(pwmPin, OUTPUT); + Serial.begin(9600); + + Timer3.initialize(800); // 800 microseconds or 1.25kHz + Timer3.attachInterrupt(updatePWM); +} + +void updatePWM() { + static int counter = 0; + + if (counter < PWMval) { + digitalWrite(pwmPin, HIGH); + } else { + digitalWrite(pwmPin, LOW); + } + + counter++; + if (counter >= 100) { + counter = 0; + } +} + +void mcpsoftpwm(int value) { + if (value < 0) value = 0; + if (value > 100) value = 100; + + Serial.print("softPWM pin: 3, value: "); + Serial.println(value); + PWMval = value; +} + +void loop() { + mcpsoftpwm(100); // Set PWM to 100% + delay(1000); + mcpsoftpwm(50); // Set PWM to 50% + delay(1000); + Serial.println("loop"); +} |
