-
Notifications
You must be signed in to change notification settings - Fork 0
/
motorcontrol.ino
56 lines (46 loc) · 902 Bytes
/
motorcontrol.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
47
48
49
50
51
52
53
54
55
56
int enablePin = 11;
int in1Pin = 10;
int in2Pin = 9;
int button1Pin = 3;
int button1State;
boolean reverse = 0;
int motorSpeed = 0;
int prevState;
void setup()
{
pinMode(button1Pin, INPUT);
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
}
void loop()
{
button1State = digitalRead(button1Pin);
if (button1State == HIGH && prevState != button1State)
{
reverse = 0;
motorSpeed = 0;
}
else if (button1State == LOW && prevState != button1State)
{
reverse = 1;
motorSpeed = 0;
}
motorSpeed += 5;
if (motorSpeed > 100)
{
motorSpeed = 100;
}
if (motorSpeed < 0)
{
motorSpeed = 0;
}
prevState = button1State;
setMotor(motorSpeed, reverse);
}
void setMotor(int speed, boolean reverse)
{
analogWrite(enablePin, speed);
digitalWrite(in1Pin, ! reverse);
digitalWrite(in2Pin, reverse);
}