summaryrefslogtreecommitdiff
path: root/debug/motordriver/softPWM/softPWM.ino
blob: ab74715a84a3123915fbd304712c428434a6ae7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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");
}