1

I would like to control my dc motor using the Arduino Uno WiFi Rev2 using an html range slider. Now I can control the motors speed and direction using push buttons just fine. My code is posted below of what I've done so far.

client.println("<style>");
client.println(".class-name{writing-mode: bt-lr; webkit-appearance: slider-vertical;width: 8px;height: 175px;padding: 0 5px;}</style>");
client.println("<center><input type=\"range\" class=\"class-name\" orient=\"vertical\" id=\"range\" min=\"0\" max=\"255\" step=\"0\" onchange=\"motor1(this.value)\" value=\"0\"></center>");

This code below is the current button code. I need the below code to be used by the slider code above:

if (currentLine.endsWith("GET /MONFWD")) {
digitalWrite(MOTOR_A_ONE, HIGH);  // Establishes forward direction of Channel A
digitalWrite(MOTOR_A_TWO, HIGH);  // ENGAGE BRAKE
delay(1000);                      // DELAY BEFORE RELEASING BRAKE
digitalWrite(MOTOR_A_TWO, LOW);   // Disengage the Brake for Channel A
analogWrite(MOTOR_A_ENGAGE, 255); // Spins the motor forward on Channel A at full speed
1
  • 1
    And what is your question? You didn't ask one. Please describe, what exactly your problem is in writing the needed code.
    – chrisl
    Commented Oct 18, 2019 at 1:05

1 Answer 1

2

Here's an example of slider that changes a Servo speed, In html page code you have to add this:

  <div class="slidecontainer">
  <input type="range" min="0" max="1023" value="512" class="slider" id="myRange">
  <p>Value: <span id="demo"></span></p>
</div>

<script>
var slider = document.getElementById("myRange");

var output = document.getElementById("demo");

output.innerHTML = slider.value;

slider.onchange = function() {
  output.innerHTML = this.value;
  pwm_change(output.innerHTML);

}
function pwm_change(val) {
  var xhttp = new XMLHttpRequest();
  xhttp.open("GET", "setPWM?PWMval="+val, true);
     xhttp.send();
}
</script>

In arduino you have to define handler like this:

void handle_pwm() {
 String  pwm_val = server.arg("PWMval"); // reading from slider on html pagae

 Serial.print("slider val: ");
 Serial.println(pwm_val);
int pwm_value = map(pwm_val.toInt(), 0, 1023, 0, 180); 
Servo.write(pwm_value);

server.send(200,"text/plane","0"); // handling the webpage update

}

And define servo and handler in Setup() section:

void setup(void){
....
Servo.attach(MotorPin,1000,2000);
server.on("/setPWM", handle_pwm);
....
 }
5
  • I'm not using the servo.h library...I'm using a motor shield which already works with the push buttons....if I ignore the servo.write and servo .attach arguments the code should work fine correct? Commented Oct 18, 2019 at 18:14
  • Also the above code is not compatible with the Arduino Uno WiFi Rev 2. It does not use server.on or server.arg Commented Oct 18, 2019 at 20:44
  • no difference, you can follow the above example for both motor shield and Uno WIFI board, just replace servo.write() command with motor.setSpeed() for changing DC motor's speed in motor shield.just need to map slider value (0-1023) to your motor speed range,that is usually (0-255). And by using libraries like WiFiNINA , you can have server.xyz arguments for Uno Wifi board so.
    – M sayeh
    Commented Oct 18, 2019 at 21:46
  • I am using the WiFiNINA library and there are no equivalents for server.arg and server.on except for client.read and client.println. Also no matter where I put the function handle_pwm it throws errors. The slider works fine as I did change the values there. Its getting the function to line up with the slider that its not working. I currently have 3 push buttons that work. Motor FWD, MotorREV and MotorBrake those work fine. I just need a slider to control the motor speed smoothly Commented Oct 18, 2019 at 23:04
  • the above example is originally written for Nodemcu board,It enjoys much easier libraries to parse http requests ,like ESP8266 WebServer lib including server.arg command and etc... for Arduino Uno WiFi Rev 2,it is not seemingly as various as nodemcu,so,you should deal with the http requests with commands like client.read() , currentLine.endsWith() and equivalents like this.
    – M sayeh
    Commented Oct 19, 2019 at 0:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.