user72595: 
I am using the potentiometer to control the duty cycle of a PWM signal to the MOSFET trigger switch module, thus controlling the motor speed.
In my code, I have used the map function to utilize the full resolution of my potentiometer (0 - 1023) and thus create a proportional PWM signal using analogWrite function (0 - 255).
However, in my setup I want to limit the speed of the motor from going beyond a MAX_RPM value, despite any further increase in analog value input from the potentiometer.
This means I have to continously check the RPM value (the RPM code works fine, so I will not include it).
If the motor speed hits the MAX_RPM value, any further potentiometer input should be ignored. To achieve this, I figured I need to hold the analogWrite function value constant thus holding the speed constant.
In the event that I turn the potentiometer in the reverse direction, this should decrease the PWM value thus also decreasing the speed.
I have written the code to achieve the process described above to no avail i.e., increasing potentiometer input causes the speed to go beyond the MAX_RPM value which is undesired.
In this project, I am developing a rev limiter of sorts.
Please help me figure out the problem in my code.
#define MAX_RPM 500
    #define MAX_RPM_PWM_MAPPED_VALUE 15
    
    //In this case, I first ran the motor while monitoring the speed and the potentiometer value
    //While doing this, I noted the potentiometer value at which the MAX_RPM is obtained
    //I then defined this potentiometer value as MAX_RPM_PWM_MAPPED_VALUE
    //With MAX_RPM_PWM_MAPPED_VALUE, I would directly limit the value written to the analogWrite function
    // 
    
    MOSFETMODULE = 4;
    potentiometer = AO;
    
    void setup()
    {
        pinMode(MOSFETMODULE, OUTPUT);
    }
    
    void loop()
    {
        val = analogRead(potentiometer);  //Read potentiometer value - represents accelerator input
        mappedValue = map(val, 0, 1023, 0, 255); //Map the 10 bit value to 8 bit equivalent
        
        if(RPM >= MAX_RPM)
        {
            analogWrite(MOSFETMODULE, MAX_RPM_PWM_MAPPED_VALUE);
        }
            else
            {
              mappedValue = constrain(mappedValue, 0, MAX_RPM_PWM_MAPPED_VALUE);
              analogWrite(MOSFETMODULE, mappedValue); //Output a PWM signal to the MOSFET trigger module
            }
    }


user4143: Your problem is that you don't know what ADC value maps to the maximum RPM. If you did you wouldn't need to know the RPM.
What you have here is a "feedback loop", and what you need is some method of using the detected RPM to control the speed of the motor.
You need to decouple the potentiometer from the analogWrite completely - the potentiometer should be setting the desired RPM, not the value being sent to set the RPM.
So if you want an RPM range from, say, 0 to 100RPM you can map() the analogRead() to the values 0-100. Then you can examine the actual RPM and adjust the value you send to the PWM accordingly - if the motor is too fast then slow it down. If it's too slow then speed it up.
There's a special method that is used for this: PID. And there's a library that does it for you too:

https://playground.arduino.cc/Code/PIDLibrary/

The potentiometer provides the "set point", the RPM detection provides the "input", and the PID library gives you the "output" to feed to the analogWrite().  You need to then provide the special magic "tuning" parameters to give the best control over your motor.

72595: <p><a href="https://i.stack.imgur.com/LDEk1.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LDEk1.jpg" alt="My Arduino motor speed controller setup" /></a></p>
<p>I am using the potentiometer to control the duty cycle of a PWM signal to the MOSFET trigger switch module, thus controlling the motor speed.</p>
<p>In my code, I have used the map function to utilize the full resolution of my potentiometer (0 - 1023) and thus create a proportional PWM signal using analogWrite function (0 - 255).</p>
<p>However, in my setup I want to limit the speed of the motor from going beyond a MAX_RPM value, despite any further increase in analog value input from the potentiometer.</p>
<p>This means I have to continously check the RPM value (the RPM code works fine, so I will not include it).</p>
<p>If the motor speed hits the MAX_RPM value, any further potentiometer input should be ignored. To achieve this, I figured I need to hold the analogWrite function value constant thus holding the speed constant.</p>
<p>In the event that I turn the potentiometer in the reverse direction, this should decrease the PWM value thus also decreasing the speed.</p>
<p>I have written the code to achieve the process described above to no avail i.e., increasing potentiometer input causes the speed to go beyond the MAX_RPM value which is undesired.</p>
<p>In this project, I am developing a <strong>rev limiter</strong> of sorts.</p>
<p>Please help me figure out the problem in my code.</p>
<pre><code>#define MAX_RPM 500
    #define MAX_RPM_PWM_MAPPED_VALUE 15
    
    //In this case, I first ran the motor while monitoring the speed and the potentiometer value
    //While doing this, I noted the potentiometer value at which the MAX_RPM is obtained
    //I then defined this potentiometer value as MAX_RPM_PWM_MAPPED_VALUE
    //With MAX_RPM_PWM_MAPPED_VALUE, I would directly limit the value written to the analogWrite function
    // 
    
    MOSFETMODULE = 4;
    potentiometer = AO;
    
    void setup()
    {
        pinMode(MOSFETMODULE, OUTPUT);
    }
    
    void loop()
    {
        val = analogRead(potentiometer);  //Read potentiometer value - represents accelerator input
        mappedValue = map(val, 0, 1023, 0, 255); //Map the 10 bit value to 8 bit equivalent
        
        if(RPM &gt;= MAX_RPM)
        {
            analogWrite(MOSFETMODULE, MAX_RPM_PWM_MAPPED_VALUE);
        }
            else
            {
              mappedValue = constrain(mappedValue, 0, MAX_RPM_PWM_MAPPED_VALUE);
              analogWrite(MOSFETMODULE, mappedValue); //Output a PWM signal to the MOSFET trigger module
            }
    }
</code></pre>
 4143: <p>Your problem is that you don't know what ADC value maps to the maximum RPM. If you did you wouldn't need to know the RPM.</p>
<p>What you have here is a &quot;feedback loop&quot;, and what you need is some method of using the detected RPM to control the speed of the motor.</p>
<p>You need to decouple the potentiometer from the analogWrite completely - the potentiometer should be setting the desired RPM, not the value being sent to set the RPM.</p>
<p>So if you want an RPM range from, say, 0 to 100RPM you can <code>map()</code> the <code>analogRead()</code> to the values 0-100. Then you can examine the actual RPM and adjust the value you send to the PWM accordingly - if the motor is too fast then slow it down. If it's too slow then speed it up.</p>
<p>There's a special method that is used for this: PID. And there's a library that does it for you too:</p>
<ul>
<li><a href="https://playground.arduino.cc/Code/PIDLibrary/" rel="nofollow noreferrer">https://playground.arduino.cc/Code/PIDLibrary/</a></li>
</ul>
<p>The potentiometer provides the &quot;set point&quot;, the RPM detection provides the &quot;input&quot;, and the PID library gives you the &quot;output&quot; to feed to the <code>analogWrite()</code>.  You need to then provide the special magic &quot;tuning&quot; parameters to give the best control over your motor.</p>
