summaryrefslogtreecommitdiff
path: root/code/debug/motors/PWM/src/main.cpp
blob: c7d8fe0ec1bbbb393d984cc31ff69d6304fa6c9f (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
47
48
#include <Arduino.h>

IntervalTimer myTimer;

int pwmPin = 3; //pin for PWM output
int pwmacc = 1000; //how accurate the PWM is, means you can enter value from 0 to the value of pwmacc
int PWMval = 0; //value of the PWM, means you can enter value from 0 to the value of pwmacc


void setup() {
}
void setup() {
  
  Timer3.initialize(800);  // 800 microseconds or 1.25Hz
  Timer3.attachInterrupt(updatePWM);
}

void updatePWM() {
  static int counter = 0;
  if PWMval < 0) {
    PWMval = 0;
  }
  if (PWMval > pwmacc) {
    PWMval = pwmacc;
  }

  if (counter < PWMval) {
    digitalWrite(pwmPin, HIGH);
  } else {
    digitalWrite(pwmPin, LOW);
  }

  counter++;
  if (counter >= pwmacc) {
    counter = 0;
  }
}

void loop() {
  for i (0; i < pwmacc; i++) {
    PWMval = i;
    delay(10);
  }
  for i (pwmacc; i > 0; i--) {
    PWMval = i;
    delay(10);
  }
}