diff options
| author | Abel Tim <abel@main.home> | 2024-07-03 12:32:55 +0200 |
|---|---|---|
| committer | Abel Tim <abel@main.home> | 2024-07-03 12:32:55 +0200 |
| commit | 370fdabd4f8f95b2b9bf1d973d9d974f9491c13d (patch) | |
| tree | ffa2c48eaf09f84ce80ff51f6b72400a33f34753 /debug/motordriver/softPWM/softPWM.ino | |
| parent | 59db64a6e61d8c863f76a4cf8f366b65184192dd (diff) | |
| download | robotica-370fdabd4f8f95b2b9bf1d973d9d974f9491c13d.tar.gz robotica-370fdabd4f8f95b2b9bf1d973d9d974f9491c13d.zip | |
soft PWM added
Diffstat (limited to 'debug/motordriver/softPWM/softPWM.ino')
| -rw-r--r-- | debug/motordriver/softPWM/softPWM.ino | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/debug/motordriver/softPWM/softPWM.ino b/debug/motordriver/softPWM/softPWM.ino new file mode 100644 index 0000000..ab74715 --- /dev/null +++ b/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"); +} |
