Potentiometer regulating motor...noot easy

Hello and thanks for reading.
This is what I've made so far:

A motor that spins according to datas from an ultrasonic sensor. The further an object is, the slower the motor rotates, the closer it gets the faster it spins. It also has a potentiometer to regulate how far the sensor "sees", meaning turning it will will ignore objects after a certain distance.

The problem is:

I'm trying to add a second potentiometer that will let me regulate the speed at which the motor is turning --> the motor will vibrate (as i've added an unbalanced weight) and the goal is to be able to decide how intense that vibration should be.

so, SENSOR will increase how fast the motor rotates (and vibrates) but i would also need the POTENTIOMETER to have some kind of control over the intensity at which the motor rotates.

P.S,: The ultrasound sensor signal range is 0- 19000 (0- 400 cm)
The potentiometer signal range is 0 - 674

Here's the code, thanks if you can help me out!!!

//Sensor info 100-19000 sensor data min-max
const int trigPin = 10;
const int echoPin = 11;
long duration;
int distanceCm, distanceInch;



// Motor A connections
int enA = 9;
int in1 = 8;
int in2 = 7;


void setup() {
  // Set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  

  // pinmode of SCH-04 sensor
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  
  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);

  //initialize communication with console
  Serial.begin(9600);
  delay(100);

}

void loop() {

  speedControl();
  delay(10);



  

  //read DISTANCE from Sensor
  delay(100);    //delay new_cycle lettura sensore
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);   //Temporizza segnale echoPin - ms
  distanceCm = duration*0.034/2;      //Calcolo distanza in CM
  distanceInch = duration*0.0133/2;   //Calcolo distanza in INCHES


  
}



//Controllo velocità motori in base a "distanceCm"
void speedControl() {
  
  // Turn on motors if distance < 150
  
  int pot_value = analogRead(A0);
  
 int motor_speed = map(duration, 0, 9000, 255, 0);
 int pot_valuee = map(pot_value, 0, 675, 0, 19000);
 
  
  if (duration < pot_valuee) {
    
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
 
  analogWrite(enA,  motor_speed); //funciton for SPEED opposite to CM
    

    

  

    Serial.print(duration);
    Serial.print(" -- ");
    Serial.print(motor_speed);
    Serial.print(" -- ");
    Serial.println(pot_value);
    
  
  }
  
  else {
  
    Serial.print("Nessun ostacolo rilevato entro 150cm  -- ");
    Serial.print(duration);
    Serial.print(" -- ");
    Serial.print(motor_speed);
    Serial.print(" -- ");
    Serial.println(pot_value);
    analogWrite(enA, LOW);
  
  }
    }

What is the problem?

the max motor speed, 255, is "mapped" to 9000. what about using the pot value to set the value corresponding to 9000?

First step is not really code related but conceptual.

The motor has only one control "speed" so here speed = intensity.

Perhaps the pot could represent 0 to 100% max speed.
If the pot were at 100% the system would act as it does not.

If the pot were at 50% the system would essentially change the speed at each distance to 1/2 of what it would have been if the pot was at 100%

how would you translate it into code? i'm not an expert yet

Try adding this line......

motor_speed = motor_speed * pot_valuee / 675; 
analogWrite(enA,  motor_speed); //funciton for SPEED opposite to CM

[/quote]

wouldn't this result in a higher motor speed

   int k = 9000 * analogRead(A0) / 1023;
   int motor_speed = map(duration, 0, k, 255, 0);

This should be < 1

Perhaps the variable types need to be changed.

Ok now it works! thankyou

what abbout adding a second potentiometer?
The first one is used to regulate range of the sensor
while the second one would be needed to regulate motor speed

so that even if the sensor range is set to 1/2 i can still turn the second potentiometer and get a faster/slower rotation

to give a practical idea lets say we connect it to a fan, according to how close an object is, the fan spins at x speed, what what if i wanted to increase or decrease fan speed even with the object staying at the same distance?

I would need to increase or decrease fan speed of just a bit, not too much (like 2/10 of motor total speed) only using 2nd potentiometer

Where ever you set the motor speed multiply the above value to the speed value.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.