How can i set continuous current of potentionmeter

so i am doing a small project , in which i am using

  • motor
    *motor driver
    *potentiometer
  • push button
  • arduino nano.

so , when current will come from motor driver to motar i will set it by using potentiometer, as soon as i set current using potentiometer and will press the push button after that if i tried to change current using potentiometer .It should not change untill i press the push button again .

please let me konw how can i turn my code into what i told u.




int enA = 10;

int in1 = 9;

int in2 = 8;

// motor two

int enB = 5;

int in3 = 7;

int in4 = 6;

void setup()

{

  // set all the motor control pins to outputs

  pinMode(enA, OUTPUT);

  pinMode(enB, OUTPUT);

  pinMode(in1, OUTPUT);

  pinMode(in2, OUTPUT);

  pinMode(in3, OUTPUT);

  pinMode(in4, OUTPUT);

}

void demoOne()

{

  // this function will run the motors in both directions at a 
fixed speed

  // turn on motor A

  digitalWrite(in1, HIGH);

  digitalWrite(in2, LOW);

  // set speed to 200 out of possible range 0~255

  analogWrite(enA, 200);

  // turn on motor B

  digitalWrite(in3, HIGH);

  digitalWrite(in4, LOW);

  // set speed to 200 out of possible range 0~255

  analogWrite(enB, 200);

  delay(2000);

  // now change motor directions

  digitalWrite(in1, LOW);

  digitalWrite(in2, HIGH);  

  digitalWrite(in3, LOW);

  digitalWrite(in4, HIGH); 

  delay(2000);

  // now turn off motors

  digitalWrite(in1, LOW);

  digitalWrite(in2, LOW);  

  digitalWrite(in3, LOW);

  digitalWrite(in4, LOW);

}


void demoTwo() {


// this function will run the motors across the range of 
possible speeds

 
 // note that maximum speed is determined by the motor 
itself and the operating voltage


  // the PWM values sent by analogWrite() are fractions of 
the maximum speed possible 

  // by your hardware

  // turn on motors

 
 digitalWrite(in1, LOW);

  digitalWrite(in2, HIGH);  

  digitalWrite(in3, LOW);

  digitalWrite(in4, HIGH); 


  // accelerate from zero to maximum speed


 for (int i = 0; i < 256; i++) { 

analogWrite(enA, i); 

analogWrite(enB, i); 

delay(20); 


} 


// decelerate from maximum speed to zero 


for (int i = 255; i > 0; --i)

  {

    analogWrite(enA, i);

    analogWrite(enB, i);

    delay(20);

  } 



 


  // turn off motors

  digitalWrite(in1, LOW);

  digitalWrite(in2, LOW);  

  digitalWrite(in3, LOW);

  digitalWrite(in4, LOW);  

}

void loop()
{

  demoOne();

  delay(1000);

  demoTwo();

  delay(1000);

}

components :-

L298N motor driver

Potentiometer

PushButton

Arduino Nano

Battery 6v to 12v

Thanks for reading :slight_smile:

Add a button input to the sketch
Read the button state each time through loop() (more on this below ***)
When the button becomes pressed (not is pressed) change the state of a boolean variable
When the boolean is true allow reading of the pot and changing of the motor speed

** However, there is a problem
Your code uses delay()s which add up to at least 4 seconds during which time you cannot read the input button. Is this acceptable ?

Please give a schematic of your project.

i will remove delay and add millis () instead . but what about the potentiometer it can still change value after pressing button also

please,let me know how to solve this ,thanks :slightly_smiling_face:

actually i didn't draw a real schematics

You ask for help and do not give information.
It is not clear from your description why you need a continuous current through a potentiometer and how you are going to change the motor current with a potentiometer. There are other solutions for this, like PWM.

maybe , this will help u

i didn't have added battery because there was no battery of 9V available there

Hi,
Could you please post a circuit diagram, not a Fritzy image.

Please draw with pen(cil) and paper, including component labels and pin names, along with your power supply.

Are you trying to run all your project off a 9V battery?
ab-base_2_142

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

sorry , guys but i don't know how to draw circuit diagram that's i am using circuito.io to know how to connect pins and which components to connect where .

but i have this much only to provide to you : frowning:

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