Dual Direction motor (minimal inputs)

Hi
Im new to C+ and Arduino stuff and I'm trying to give the Arduino a single analog signal and have it dictate direction and speed to a motor driver.

I figured out the direction but the problem is the speed, I wanted to split the analog signal into a high and low (as if using a joystick) and have the the extreme ends of the signal be higher speed but I cant figure out how to add it without getting errors.

i cant attach the program because im new.....
below are some of the things i have tried

//example 1
  analogWrite(enablePin, pwmValue);

  // Map the potentiometer value to a PWM duty cycle (0-255)

  if (potValue > 600) {
    map(potValue, 600, 1023, 0, 255);
  }
  if (potValue < 400) {
    map(potValue, 400, 0, 0, 255);
  })


//example 2
  int pwmValue = map((potValue, 1023, 600, 0, 255)||(potValue,0, 500, 0, 255));

any advice would be appreciated.

Hi, @jstabyle
Welcome to the forum.

You should be able to copy it with code tags like you have already done with your examples.

Post your complete code?

Post a schematic of your project?

What model Arduino are you using?

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

// Define pins for the L298N motor driver
const int enablePin = 3;  // Enable pin for PWM speed control
const int input1Pin = 2;  // IN1 pin for direction control
const int input2Pin = 4;  // IN2 pin for direction control

// Potentiometer pin
const int potPin = A0;

void setup() {
  // Initialize serial communication for debugging
  Serial.begin(9600);
  // Set pin modes
  pinMode(enablePin, OUTPUT);
  pinMode(input1Pin, OUTPUT);
  pinMode(input2Pin, OUTPUT);
}

void loop() {
  // Read the potentiometer value
  int potValue = analogRead(potPin);

  // Control the motor speed using PWM

  analogWrite(enablePin, pwmValue);

  // Map the potentiometer value to a PWM duty cycle (0-255)

  if (potValue > 600) {
    map(potValue, 600, 1023, 0, 255);
  }
  if (potValue < 400) {
    map(potValue, 400, 0, 0, 255);
  })





  Serial.println(pwmValue);
  // Control the motor direction
  if (potValue > 600) {  // Motor moves forward if PWM is greater than 0
    digitalWrite(input1Pin, HIGH);
    digitalWrite(input2Pin, LOW);
  }
  if (potValue < 450) {  // Motor moves backward if PWM is greater than 0
    digitalWrite(input1Pin, LOW);
    digitalWrite(input2Pin, LOW);
  }
  if (potValue > 450 && potValue < 550) {  // Stop the motor
    digitalWrite(input1Pin, LOW);
    digitalWrite(input2Pin, HIGH);
  }


  // Optional: Add a delay
  delay(100);
}

This line is code

    map(potValue, 600, 1023, 0, 255);

calculates a value between 0 and 255 and then discards the result. What's the point of that?

Did you mean to write

    pwmValue = map(potValue, 600, 1023, 0, 255);

I missed a spot in all my chopping and shuffling


  if (potValue > 600) {
    pwmValue= map(potValue, 600, 1023, 0, 255);
  }
  if (potValue < 400) {
    pwmValue= map(potValue, 400, 0, 0, 255);
  })


So I started with

int pwmValue= map(potValue, 0, 1023, 0, 255)

I had to re-add the "int" line as 0 to start and mess with the punctuation a bit.

// Define pins for the L298N motor driver
const int enablePin = 3;  // Enable pin for PWM speed control
const int input1Pin = 2;  // IN1 pin for direction control
const int input2Pin = 4;  // IN2 pin for direction control

// Potentiometer pin
const int potPin = A0;
int pwmValue = 0;
void setup() {
  // Initialize serial communication for debugging
  Serial.begin(9600);
  // Set pin modes
  pinMode(enablePin, OUTPUT);
  pinMode(input1Pin, OUTPUT);
  pinMode(input2Pin, OUTPUT);
}

void loop() {
  // Read the potentiometer value
  int potValue = analogRead(potPin);

  // Control the motor speed using PWM

  analogWrite(enablePin, pwmValue);

  // Map the potentiometer value to a PWM duty cycle (0-255)

  if (potValue > 600) {
    pwmValue= map(potValue, 600, 1023, 0, 255);
  };
  if (potValue < 400) {
    pwmValue= map(potValue, 400, 0, 0, 255);
  };





  Serial.println(pwmValue);
  // Control the motor direction
  if (potValue > 600) {  // Motor moves forward if PWM is greater than 0
    digitalWrite(input1Pin, HIGH);
    digitalWrite(input2Pin, LOW);
  }
  if (potValue < 450) {  // Motor moves backward if PWM is greater than 0
    digitalWrite(input1Pin, LOW);
    digitalWrite(input2Pin, LOW);
  }
  if (potValue > 450 && potValue < 550) {  // Stop the motor
    digitalWrite(input1Pin, LOW);
    digitalWrite(input2Pin, HIGH);
  }


  // Optional: Add a delay
  delay(100);
}

thanks for the quick answer I guess I just needed a nudge.

I would handle the speed & direction control like this

const byte PIN_ANALOG = A0;

const int min_speed = 10;
const int max_speed = 1000;
const int dead_band = 20;
const float alpha = 0.05f;

float filtered_analog;
int speed;
int direction;

void setup() 
{
  Serial.begin (115200);
}


void get_speed (int adc_raw)
{
  filtered_analog = (alpha * adc_raw) + (1-alpha) * filtered_analog;

  int abs_analog = abs(filtered_analog - 512);
  direction = (filtered_analog > 512) ? 1 : -1;
  
  if (abs_analog < dead_band) 
    speed = 0;
  else
    speed = map (abs_analog, dead_band, 512, min_speed, max_speed);

  Serial.print (filtered_analog);
  Serial.print (',');
  Serial.print (direction);
  Serial.print (',');
  Serial.print (speed);
  Serial.println();
}

void loop() 
{
  get_speed ( analogRead (PIN_ANALOG) );
}

It looks like strange things would happen around 575 or 425 -- pwmValue would be negative, and the motors would be on.

I'd put the outputs at the end, and use a mode variable to separate the different modes of operation.

Completely untested:

// Define pins for the L298N motor driver
const int enablePin = 3;  // Enable pin for PWM speed control
const int input1Pin = 2;  // IN1 pin for direction control
const int input2Pin = 4;  // IN2 pin for direction control

// Potentiometer pin
const int potPin = A0;
int pwmValue = 0;
void setup() {
  // Initialize serial communication for debugging
  Serial.begin(9600);
  // Set pin modes
  pinMode(enablePin, OUTPUT);
  pinMode(input1Pin, OUTPUT);
  pinMode(input2Pin, OUTPUT);
}

void loop() {
  // Read the potentiometer value
  int potValue = analogRead(potPin);

 
  // Map the potentiometer value to a PWM duty cycle (0-255)

  int dirMode = 0;

  if (potValue > 600) {
    pwmValue= map(potValue, 600, 1023, 0, 255);
    dirMode = 1;
  };
  if (potValue < 400) {
    pwmValue= map(potValue, 400, 0, 0, 255);
    dirMode = -1;
  };





  Serial.println(pwmValue);
  // Control the motor direction
  if (dirMode == 1) {  // Motor moves forward if PWM is greater than 0
    digitalWrite(input1Pin, HIGH);
    digitalWrite(input2Pin, LOW);
  }
  if (dirMode == -1) {  // Motor moves backward if PWM is greater than 0
    digitalWrite(input1Pin, LOW);
    digitalWrite(input2Pin, LOW);
  }
  if (dirMode ==0) {  // Stop the motor
    pwmValue = 0;
    digitalWrite(input1Pin, LOW);
    digitalWrite(input2Pin, HIGH);
  }
 // Control the motor speed using PWM

  analogWrite(enablePin, pwmValue);

  // Optional: Add a delay
  delay(100);
}