-
Notifications
You must be signed in to change notification settings - Fork 0
/
motor_example.ino
55 lines (53 loc) · 1.22 KB
/
motor_example.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
int button = 2;
int motor = 9;
int sensor = A1;
int resistor = A0;
int pitz = 8;
int LED = 6;
int sensorValue;
int highValue = 1023;
int lowValue = 0;
int motorInput;
void setup() {
Serial.begin(9600);
pinMode(motor, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(pitz, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
int reading = digitalRead(button);
int readingLight = analogRead(sensor);
int readingResistor = analogRead(resistor);
motorInput = map(readingResistor,0,1023,0,255);
delay(20);
if (reading == 1) {
if (readingResistor == 0) {
digitalWrite(motor, LOW);
} else if (readingResistor <= 600) {
digitalWrite(motor, HIGH);
delay(200);
digitalWrite(motor, LOW);
delay(200);
digitalWrite(motor, HIGH);
} else {
digitalWrite(motor, motorInput);
}
if (readingLight >= 800) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
digitalWrite(pitz, HIGH);
if (readingLight > highValue) {
highValue = readingLight;
} else if (readingLight < lowValue) {
lowValue = readingLight;
}
digitalWrite(pitz, LOW);
} else {
digitalWrite(motor, LOW);
digitalWrite(LED, LOW);
digitalWrite(pitz, LOW);
}
}