-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab01_DCMotorSpeed.cpp
More file actions
28 lines (21 loc) · 1.02 KB
/
Lab01_DCMotorSpeed.cpp
File metadata and controls
28 lines (21 loc) · 1.02 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
const int potPin = A0; // Pin connected to the potentiometer wiper (middle terminal)
const int motorPin = 5; // Control pin to the motor driver (Gate Pin of IRF840 or Control pin of a relay)
int potValue = 0; // Variable to store the potentiometer value
int motorSpeed = 0; // Variable to store the motor speed (PWM value)
void setup() {
pinMode(motorPin, OUTPUT); // Set motor pin as an output
Serial.begin(9600); // Start serial communication for debugging (optional)
}
void loop() {
potValue = analogRead(potPin); // Read the potentiometer value (0 to 1023)
// Map the potentiometer value to a PWM range (0 to 255)
motorSpeed = map(potValue, 0, 1023, 0, 255);
// Set the motor speed using PWM
analogWrite(motorPin, motorSpeed);
// Print the potentiometer and motor speed values (for debugging)
Serial.print("Potentiometer Value: ");
Serial.print(potValue);
Serial.print(" | Motor Speed: ");
Serial.println(motorSpeed);
delay(100); // Small delay to stabilize readings
}