-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain
70 lines (54 loc) · 2.02 KB
/
Main
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
57
58
59
60
61
62
63
64
65
66
67
68
// MAIN FILE FOR RC CAR
#define enA 9 // Connects the right wheel to the PWM signal
#define enB 3 // Connects the left wheel to the PWM signal
#define inA 6 // inA & inB are inputs for the right wheel
#define inB 7
#define inC 5 // inC & inD are inputs for left wheel
#define inD 4
void setup() {
// Sets up pins as output pins
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(inA, OUTPUT);
pinMode(inB, OUTPUT);
// Set initial rotation direction
digitalWrite(inA, LOW);
digitalWrite(inB, HIGH);
digitalWrite(inC, LOW);
digitalWrite(inD, HIGH);
// HC Setup
Serial.begin(9600);
}
int input;
String readString;
int outputPower = 5; // Sets the initial power
void loop() {
int potValue = outputPower; //analogRead(A0); // Read potentiometer value
int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Map the potentiometer value from 0 to 255
analogWrite(enA, pwmOutput); // Send PWM signal to enable pin
analogWrite(enB, pwmOutput);
while (Serial.available()) { // While there is something to read in...
delay(2); // delay to allow byte to arrive in input buffer
char c = Serial.read(); // pulls ina character
readString += c; // and adds it to the string
}
if(readString.length()) { // Checks to make sure readString isn't empty
input = readString.toInt();
}
if(input == 1) { // 1 - Spins the wheels in one direction
digitalWrite(inA, HIGH);
digitalWrite(inB, LOW);
digitalWrite(inC, HIGH);
digitalWrite(inD, LOW);
}else if (input == 0) { // 0 - Spins the wheels in another direction
digitalWrite(inA, LOW);
digitalWrite(inB, HIGH);
digitalWrite(inC, LOW);
digitalWrite(inD, HIGH);
}else if (input == 2) { // 2 - Stops the wheels
outputPower = 0;
}else if (input == 3 && outputPower <= 900) { // 3 - Increases the power, until it is at max
outputPower += 100;
}
delay(500);
}