Need help controlling motor with arduino

int1 = 3;  

int2 = 4;

const byte buttonPin = 4;  // const because they won't 
change

const byte KNOB = A0;  // use A notation for analog inputs



bool buttonState = 0;         // current state of the button

bool lastButtonState = 0;     // previous state of the button



bool adjustspeed= true;  // create the flag variable



void setup()

{

   Serial.begin(115200);


   pinMode(int1, OUTPUT);
  
 pinMode(int2, OUTPUT);

   pinMode(buttonPin, INPUT_PULLUP);  

}



void loop()

{

   readButton();

   if (adjustspeed == true)

   {

      // Read the value of the potentiometer knob

      int knobValue = analogRead(KNOB);

      // Map the potentiometer value to 1-255

      int intensity = map(knobValue, 1, 1024, 1, 255);

      // Output the respective value to the motor

      analogWrite( " ------------------ ", intensity);  // how can i mention int1 and int2 of motor in analog write

   }

}



void readButton()

{

   static unsigned long timer = 0;

   unsigned long interval = 50;  // check switch 20 times per 

second

   if (millis() - timer >= interval)

   {

      timer = millis();

      // read the pushbutton input pin:

      buttonState = digitalRead(buttonPin);

      // compare the new buttonState to its previous state

      if (buttonState != lastButtonState)

      {

         if (buttonState == LOW)

         {

            // if the current state is LOW then the button

            // went from off to on:

            adjustspeed = !adjustspeed;  // toggle value of adustspeed

            //Serial.println(adjustspeed);

         }

      }

      // save the current state as the last state,

      //for next time through the loop

      lastButtonState = buttonState;

   }

}

hi :slight_smile: , i am doing a small but interesting project , will u help me.

i want that , when i press the push button the current flowing to motor controlled by potentiometer should not not be changed after the push button is pressed .” In simple ways arduino should not take any order from potentiometer until the button is pressed again to give it back it's control .

But i don't know how to disables it's command for a while and give it back after the button is pressed.

please let me know how can i turn my code into what i told u. :grinning:

1 Like

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

ok , but what i did wrong , please tell me

This tutorial wraps up button debounce in a class for easier use
Debouncing Switches in Arduino

The StateChangeDetection example sketch shows how to perform an action only when a button changes state, which is what you want I think - only sample the pot and change output when the button becomes depressed, not while it is depressed.

consider. a button press results in the pot being read and the PWM output correspondingly set

#define butPin  A1
#define outPin  10
#define potPin  A0

byte butLst;

void setup()
{
    Serial.begin (9600);

    pinMode (butPin, INPUT_PULLUP);
    butLst = digitalRead (butPin);
}

void loop()
{
    byte but = digitalRead (butPin);
    if (butLst != but)  {
        butLst = but;
        delay (10);     // debounce

        if (LOW == but)  {
            int potVal = analogRead (potPin);
            int outVal = map (potVal, 1, 1024, 1, 255);
            analogWrite (outPin, outVal);

            Serial.println (outVal);
        }
    }
}

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