-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduino-Motor-Driver-Sketch
41 lines (38 loc) · 1.12 KB
/
Arduino-Motor-Driver-Sketch
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
const int controlPin1 =2;
const int controlPin2 = 3;
const int enablePin =9;
const int directionSwitchPin =4;
const int onOffSwitchStateSwitchPin = 5;
const int potPin = A0;
int onOffSwitchState = 0;
int previousOnOffSwitchState = 0;
int directionSwitchState = 0;
int previousDirectionSwitchState = 0;
int motorEnabled = 0;
int motorSpeed = 0;
int motorDirection = 1;
void setup() {
// put your setup code here, to run once:
pinMode(directionSwitchPin, INPUT);
pinMode(onOffSwitchStateSwitchPin, INPUT);
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin);
delay(1);
directionSwitchState = digitalRead(directionSwitchPin);
motorSpeed = analogRead(potPin)/4;
if(onOffSwitchState != previousOnOffSwitchState){
if(onOffSwitchState == HIGH){
motorEnabled = !motorEnabled;
}
}
if(directionSwitchState != previousDirectSwitchState) {
if(directionSwitchState == HIGH) {
motorDirection = !motorDirection;
}
}