-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArduinoMotor.ino
More file actions
54 lines (45 loc) · 1.2 KB
/
Copy pathArduinoMotor.ino
File metadata and controls
54 lines (45 loc) · 1.2 KB
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
int motorPin1 = 2;
int motorPin2 = 3;
int buttonPin = 10;
int state = LOW;
int reading;
int previous = LOW;
long time = 0;
long timer;
long debounce = 200;
long stateTime;
int rotation = HIGH;
void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
reading = digitalRead(buttonPin);
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == LOW) {
state = HIGH;
stateTime = millis();
} else {
state = LOW;
}
time = millis();
}
if (state == HIGH) {
if (timer - stateTime <= 3500) {
digitalWrite(motorPin1, rotation);
digitalWrite(motorPin2, !rotation);
} else if (timer - stateTime <= 10000 and timer - stateTime > 3500) {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
} else {
rotation = !rotation;
stateTime = millis();
}
} else {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
}
previous = reading;
timer = millis();
}