-
Notifications
You must be signed in to change notification settings - Fork 0
/
gokart.ino
46 lines (38 loc) · 871 Bytes
/
gokart.ino
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 <TimerOne.h>
//definitions
#define PWM_CYCLE 50.0
#define PWM_OUT 9
#define MAX_DUTY 869
#define MIN_DUTY 0
#define inPin 12
//int
int speedLevel;
int ACC = 0;
int reading;
int previous = LOW;
void setup()
{
//init pwmout and timer1
pinMode(PWM_OUT, OUTPUT);
pinMode(10, OUTPUT);
Timer1.initialize(PWM_CYCLE * 1000);
Timer1.pwm(PWM_OUT, 0);
//acc switch
pinMode(inPin, INPUT);
Serial.begin(9600);
}
void loop() {
reading = digitalRead(inPin);
//if switch is in on position, accelerate
if (reading == HIGH && previous == LOW) {
ACC = ACC + 20;
}
//if switch is in off position, decelerate
if (reading == LOW && previous == HIGH) {
ACC = ACC - 20;
}
speedLevel = map(ACC, 0, 1023, 0, MAX_DUTY);
Timer1.setPwmDuty(PWM_OUT, speedLevel);
Serial.print("speed: " + speedLevel);
Serial.print("ACC: " + ACC);
}