I'm trying to control a motor using two potentiometers. One potentiometer for clockwise motor rotation and the other for anti-clockwise.
My code works so far but it doesnt function the way I want it to. Pot 2 controls the speed and pot 1 controls direction. How do I fix this? I want each to individually control the direction and speed.
Here is the code:
void loop()
{
// Control motor using Potentiometer
int Potentiometer_1 = analogRead(A0); // Read Potentiometer values
int Potentiometer_2 = analogRead(A1); // Read Potentiometer values
//Print Potentiometer values to the serial monitor
Serial.print("Potentiometer_2 Value: ");
Serial.print(Potentiometer_1);
Serial.print("\t");
motorSpeedA = map(Potentiometer_1, 0, 1023, 0, 255); // Reads the value of Potentiometer and converts its range from 0-1023 to 0-255
motorSpeedA = map(Potentiometer_2, 0, 1023, 0, 255); // Reads the value of Potentiometer and converts its range from 0-1023 to 0-255
if (Potentiometer_1 > 0) {
analogWrite(enA, motorSpeedA); // sends motorSpeedA values as PWM values to motor
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
}
else if (Potentiometer_2 > 0){
analogWrite(enA, motorSpeedA); // sends motorSpeedA values as PWM values to motor
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}
Nope, there is part of the code. Please post all the code.
Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.
Nope, there is part of the code. Please post all the code.
Thanks for the reply, this is my first time using the forum so please bear with me.
Here is my code:
#define ENC_COUNT_REV 500 // Encoder cycles per revolution is 500
#define ENC_IN 3 // Encoder output to Arduino Interrupt pin
volatile long encoderValue = 0; //Pulse count from the encoder
// Volatile as the value can change depending on the speed of motor
int interval = millis(); // one-millisecond interval for measurements
//Counters for milliseconds during interval
long previousMillis = 0;
long currentMillis = 0;
//Variable for RPM measurement
int rpm = 0;
// define motor pins
int enA = 9; // Initializing pin 9 as PWM
int in1=4; //To control the direction of rotation of motor
int in2=5; //To control the direction of rotation of motor
int motorSpeedA = 0; // Initial speed of the motor
// Reads the value from Potentiometer pin
int Potentiometer_1 = analogRead(A0); // Range of 0 to 1023
int Potentiometer_2 = analogRead(A1); // Range of 0 to 1023
void setup() {
//Setup Serial Monitor
Serial.begin(9600); // Send data to serial monitor at 9600 bits per second
// Set encoder as input with internal pullup
pinMode(ENC_IN, INPUT);
pinMode(Potentiometer_1,INPUT); //set Potentiometer as input
pinMode(Potentiometer_2,INPUT); //set Potentiometer as input
pinMode(enA, OUTPUT); //set enA as output
pinMode(in1, OUTPUT); //set in1 as output
pinMode(in2, OUTPUT); //set in2 as output
//Attach interrupt - ensure that the program never misses a pulse
attachInterrupt(digitalPinToInterrupt(ENC_IN), updateEncoder, RISING); //Triggered on a rising pulse from Encoder
//This function increments the value of encoderValue when it is triggered by a pulse from the Encoder
//Setup initial values for timer
previousMillis = millis();
} // End of setup function
void loop()
{
// Control motor using Potentiometer
int Potentiometer_1 = analogRead(A0); // Read Potentiometer values
int Potentiometer_2 = analogRead(A1); // Read Potentiometer values
//Print Potentiometer values to the serial monitor
Serial.print("Potentiometer_2 Value: ");
Serial.print(Potentiometer_1);
Serial.print("\t");
Serial.print("Potentiometer_2 Value: ");
Serial.print(Potentiometer_2);
Serial.print("\t");
motorSpeedA = map(Potentiometer_1, 0, 1023, 0, 255); // Reads the value of Potentiometer and converts its range from 0-1023 to 0-255
motorSpeedA = map(Potentiometer_2, 0, 1023, 0, 255); // Reads the value of Potentiometer and converts its range from 0-1023 to 0-255
if (Potentiometer_1 > 60) {
// Set motor forward
analogWrite(enA, motorSpeedA); // sends motorSpeedA values as PWM values to motor
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
}
else if (Potentiometer_2 > 60){
// Set motor forward
analogWrite(enA, motorSpeedA); // sends motorSpeedA values as PWM values to motor
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}
// Update RPM value every millisecond
currentMillis = millis();
if (currentMillis - previousMillis > interval) { // After One-millisecond, read the count and calculate RPM
previousMillis = currentMillis; // Updates the previousMillis to currentMillis
// Calculate RPM
rpm = (float)(encoderValue * 60 / ENC_COUNT_REV);
// Only update display when there is reading
if (motorSpeedA > 0 || rpm > 0) {
Serial.print(rpm);
Serial.println(" RPM");
Serial.print("\t");
} // End of if (motorSpeedA > 0 || rpm > 0)
encoderValue = 0; // reset encoderValue to zero and begin counting again
} // End of if (currentMillis - previousMillis > interval)
// End of loop function
}
void updateEncoder() {// Function for counting pulses from encoder for interrupts
// Increment value for each pulse from encoder
encoderValue++;
}
My code also involves taking data from the encoder while controlling the motor direction and speed.
motorSpeedA = map(Potentiometer_1, 0, 1023, 0, 255); // Reads the value of Potentiometer and converts its range from 0-1023 to 0-255
motorSpeedA = map(Potentiometer_2, 0, 1023, 0, 255); // Reads the value of Potentiometer and converts its range from 0-1023 to 0-255