DC motor + RemoteXY

I'm trying to use the slider function in RemoteXY to turn a motor both backwards and forwards. Yet i've only been able to do one way and it seems to only do 1 speed. I want the motor to turn faster the further away the slider button is from the middle of the slider. Slider_01 is for a servo and slider_02 is for the motor. The contraption uses an Arduino UNO connected to a motor shield Rev 3 and an ESP8622.

//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////


// you can enable debug logging to Serial at 115200
//#define REMOTEXY__DEBUGLOG    

// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__ESP8266_HARDSERIAL_POINT


// RemoteXY connection settings
#define REMOTEXY_SERIAL Serial
#define REMOTEXY_SERIAL_SPEED 115200
#define REMOTEXY_WIFI_SSID "RemoteXY"
#define REMOTEXY_WIFI_PASSWORD "12345678"
#define REMOTEXY_SERVER_PORT 6377


#include <RemoteXY.h>


// RemoteXY GUI configuration  
#pragma pack(push, 1)  
uint8_t RemoteXY_CONF[] =   // 35 bytes
  { 255,2,0,0,0,28,0,17,0,0,0,99,1,200,84,1,1,2,0,4,
  46,26,147,31,176,40,26,4,9,0,28,84,48,61,26 };
 
// this structure defines all the variables and events of your control interface
struct {


    // input variables
  int8_t slider_01; // =-100..100 slider position
  int8_t slider_02; // =-100..100 slider position


    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0


} RemoteXY;  
#pragma pack(pop)
 
/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////


#include <Servo.h>  

Servo myservo;


int directionPin = 12;
int pwmPin = 3;
int brakePin = 9;


void setup()
{
  RemoteXY_Init ();
 
    myservo.attach(5);
  RemoteXY.slider_01 = 0;
  // TODO you setup code


pinMode(directionPin, OUTPUT);
pinMode(pwmPin, OUTPUT);
pinMode(brakePin, OUTPUT);
}

void loop()
{
yield();

  RemoteXY_Handler ();
   
    int ms = RemoteXY.slider_01*8+1530;
  myservo.writeMicroseconds(ms);

  delay(2);
  // TODO you loop code
  // use the RemoteXY structure for data transfer
  // do not call delay(), use instead RemoteXY_delay()
  int motor = RemoteXY.slider_02;
  if(RemoteXY.slider_02 < 0){
  digitalWrite(directionPin, -100);
}
else{
  digitalWrite(directionPin, 100);
}

analogWrite(pwmPin, RemoteXY.slider_02);

}

does the slider return negative values or do you need to subtract an offset?

It goes from -100 to a 100

These commands are equivalent. By the way, your logic together with

is wrong.

//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////


// you can enable debug logging to Serial at 115200
//#define REMOTEXY__DEBUGLOG    

// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__ESP8266_HARDSERIAL_POINT


// RemoteXY connection settings
#define REMOTEXY_SERIAL Serial
#define REMOTEXY_SERIAL_SPEED 115200
#define REMOTEXY_WIFI_SSID "RemoteXY"
#define REMOTEXY_WIFI_PASSWORD "12345678"
#define REMOTEXY_SERVER_PORT 6377


#include <RemoteXY.h>


// RemoteXY GUI configuration  
#pragma pack(push, 1)  
uint8_t RemoteXY_CONF[] =   // 35 bytes
  { 255,2,0,0,0,28,0,17,0,0,0,99,1,200,84,1,1,2,0,4,
  46,26,147,31,176,40,26,4,9,0,28,84,48,61,26 };
 
// this structure defines all the variables and events of your control interface
struct {


    // input variables
  int8_t slider_01; // =-100..100 slider position
  int8_t slider_02; // =-100..100 slider position


    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0


} RemoteXY;  
#pragma pack(pop)
 
/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////


#include <Servo.h>  

Servo myservo;


int directionPin = 12;
int pwmPin = 3;
int brakePin = 9;


void setup()
{
  RemoteXY_Init ();
 
    myservo.attach(5);
  RemoteXY.slider_01 = 0;
  // TODO you setup code


pinMode(directionPin, OUTPUT);
pinMode(pwmPin, OUTPUT);
pinMode(brakePin, OUTPUT);
}

void loop()
{
yield();

  RemoteXY_Handler ();
   
    int ms = RemoteXY.slider_01*8+1530;
  myservo.writeMicroseconds(ms);

  delay(2);
  // TODO you loop code
  // use the RemoteXY structure for data transfer
  // do not call delay(), use instead RemoteXY_delay()
  int motor = RemoteXY.slider_02;
  analogWrite(pwmPin, RemoteXY.slider_02);
}

better?

still only goes 1 way though. When i'm above 0 on the slider the motor still sounds but the shaft doesn't move. Also, the shaft seems to be moving faster the closer i'm to 0

shouldn't it be

    int motor = RemoteXY.slider_02;

    if(RemoteXY.slider_02 < 0)
        digitalWrite(directionPin, 0);
    else
        digitalWrite(directionPin, 1);

    analogWrite(pwmPin, abs(RemoteXY.slider_02));   // can't be negative

So i put in your code, the shaft twitches a bit but doesn't start spinning. Maybe it's because the arduino is only connected to 5v from my computer while the dc motor needs 6-14v. It span before though so i don't understand why it wouldn't now.

and also, wouldn't the motor only be able to spin in one direction with your code?

why do you think that? the direction pin is now given two proper values.

but isn't 0 just stop

stop is when the PWM pin is set to zero.
the direction pin is forward or reverse

ok, i guess the motor just needs more power then.

Not necessarily. You said your variable goes from -100 to +100 and

analogWrite ( PWMpin, PWMvalue );

takes from 0 to 255. Use the @gcjr code to scale the variable to the appropriate value.

  int motor = RemoteXY.slider_02;
      if(RemoteXY.slider_02 < 0)
        digitalWrite(directionPin, 0);
    else
        digitalWrite(directionPin, 1);
    int pwmValue = (abs(RemoteXY.slider_02)/255)*100;
    analogWrite(pwmPin, pwmValue);   // can't be negative

shouldn't this work then?

doesn't this reduce the max PWM value from 255 to 100

i want the slider position to match the dc motor speed

then

int pwmValue = 255 * abs (RemoteXY.slider_02) /100;

thank you so much, it works!

Why did you not follow up with your other topic, and start a new topic?