Continuous servo concern

Hello all. To start, I am brand new to arduino and coding and this may be a very simple question but i am somewhat stuck. Concerning a continuous servo, I have checked the code (verified in the IDE) i am using and it appears to be correct; however, i cannot get the servo to speed up (it only goes one speed) nor can i get it to reverse. here is the exeCmd code I have uploaded regarding the servo. Please forgive me if this seems petty or beneath anyone here, again, I am new and self-taught. I am controlling the servo via RoboRemo. Any help would be appreciated and it may just be that i have powering issues (using 3V CR123A) or my settings in the app aren't correct (set the ID to be "th" and min 1000/max2000 and send space after ID). I can send the entire coding (this is edited to what i believe is just the servo) to be used but i have seen before in forums where one can be blasted for not being 100% pure protocol and i apologize in advance.

#include <Servo.h>
#define motor_d1 4
#define motor_d2 15

int motor_speed = 0;
int servo_angle = 0;
int val_motor;

char cmd[100];
int cmdIndex = 0;

Servo handle;

void setup() {
pinMode(motor_d1, OUTPUT);
pinMode(motor_d2, OUTPUT);
pinMode(led_lighting, OUTPUT);

analogWrite(motor_d1, 0);
analogWrite(motor_d2, 0);

void exeCmd() {

if(cmdStartsWith("th") ) {
motor_speed = atoi(cmd+3);
}

if(motor_speed>1500) {  
   val_motor = map(motor_speed, 1500, 2000, 0, 1023);       
   analogWrite(motor_d2, val_motor);

// Serial.print("th 1500 > ");
// Serial.println(val_motor);
}
if(motor_speed<1000) {
val_motor = map(motor_speed, 1500, 1000, 0, 1023);
analogWrite(motor_d1, val_motor);
// Serial.print("th 1500 < ");
// Serial.println(val_motor);
}
if(motor_speed == 1500 ) {
analogWrite(motor_d1, 0);
analogWrite(motor_d2, 0);
}
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Your incomplete and improperly posted code will not compile. Read the forum guidelines to see how to properly post code and some hints on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

You instantiate a Servo object but never use it. There is no code for attaching or operating a servo.

The exeCmd() function appears to be inside of the setup() function which is not allowed.

There is no, required, loop() function.

Where is the led_lighting variable declared?

I really doubt that a 3V CR123A will run a servo.

You may want to spend some time reading the Servo library reference and running some example code from the Servo library.

Also do a search for "Arduino continuous servo tutorial" for lots of information.

Thank you all, hopefully this is better for the guidelines (again, just got my noob badge!). If I am posting on an incorrect forum, please provide guidance.
What I am attempting to do (and have done somewhat successfully so far) is upload the code (acquired from "3dfuns" (credit where it's due yet cannot get in touch with them) and posted in it's entirety below) to an ESP8266 D1 Mini, connect an LED (to be used as on/off light) and two servos (SG90 for the 180 and FS90R for the 360) which are to be used for steering left/right (180) and to go forward/reverse with increasing/decreasing speed (360) and are set to be controlled by an app (RoboRemo). I am believing the coding is correct for my intentions and the 3V battery that's in use powers everything (as I can get both servos working simultaneously). My concern is that somewhere in the coding the ability to accelerate/decelerate on the 360 servo is incorrect and I am still kinda green to programming to locate it, if that is the case. The app or power supply may be where the disconnect is. In the app, I have set the min/max to 1000/2000, but the servo just goes counterclockwise only for both the 4 and 15 pin (D2/D8 respectively on the ESP). hopefully this answers all questions and adheres to community guidelines. Thank you.

// Pin number --> neopixel led 13/ servo 5/ motor 4,15
// Range --> motor 1000~2000 / Servo 60~120 degree

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <Servo.h>
#include "Adafruit_NeoPixel.h"

#define motor_d1 4
#define motor_d2 15
#define led_lighting 13
#define led_num 1
#define servo_streeing 5

Adafruit_NeoPixel lighting = Adafruit_NeoPixel(led_num, led_lighting, NEO_GRB + NEO_KHZ800);

const char *ssid = "CMs_autobike";
const char *pw = "";
IPAddress ip(192, 168, 0, 1);
IPAddress netmask(255, 255, 255, 0);
const int port = 9876;

int led_state;
int motor_speed = 0;
int servo_angle = 0;
int val_motor;

char cmd[100];
int cmdIndex = 0;

WiFiServer server(port);
WiFiClient client;

Servo handle;

void setup() {
  pinMode(motor_d1, OUTPUT);
  pinMode(motor_d2, OUTPUT);
  pinMode(led_lighting, OUTPUT);

  analogWrite(motor_d1, 0);
  analogWrite(motor_d2, 0);

  handle.attach(servo_streeing);
  // handle.write(50);

  lighting.begin();
  lighting.setBrightness(100);
  lighting.setPixelColor(0, 0, 0, 0);
  lighting.show();

  WiFi.softAPConfig(ip, ip, netmask);
  WiFi.softAP(ssid, pw);

  server.begin();
  Serial.begin(115200);
}

boolean cmdStartsWith(const char *st) {
  for (int i = 0;; i++) {
    if (st[i] == 0) return true;
    if (cmd[i] == 0) return false;
    if (cmd[i] != st[i]) return false;
  }
  return false;
}

void exeCmd() {

  if (cmdStartsWith("th") ) {
    motor_speed = atoi(cmd + 3);
  }

  if (motor_speed > 1000) {
    val_motor = map(motor_speed, 1000, 2000, 0, 1023);
    analogWrite(motor_d2, val_motor);
    //       Serial.print("th 1500 > ");
    //       Serial.println(val_motor);
  }
  if (motor_speed < 1000) {
    val_motor = map(motor_speed, 1000, 0, 0, 1023);
    analogWrite(motor_d1, val_motor);
    //       Serial.print("th 1500 < ");
    //       Serial.println(val_motor);
  }
  if (motor_speed == 1000 ) {
    analogWrite(motor_d1, 0);
    analogWrite(motor_d2, 0);
  }

  if (cmdStartsWith("st") ) {
    servo_angle = atoi(cmd + 3);
    handle.write(servo_angle);
    //    Serial.println(servo_angle);
  }

  if (cmdStartsWith("w")) {
    led_state = (led_state == LOW) ? HIGH : LOW;
    if (led_state == HIGH) {
      lighting.setPixelColor(0, 255, 255, 255);
      lighting.show();
    } else {
      lighting.setPixelColor(0, 0, 0, 0);
      lighting.show();
    }
  }
}

void loop() {
  if (!client.connected()) {
    client = server.available();
    return;
  }

  if (client.available()) {
    char c = (char)client.read();

    if (c == '\n') {
      cmd[cmdIndex] = 0;
      exeCmd();
      cmdIndex = 0;
    } else {
      cmd[cmdIndex] = c;
      if (cmdIndex < 99) cmdIndex++;
    }
  }
}

You only have one Servo object defined: "handle". It looks like you are using an H-Bridge DC motor driver for some purpose. You should be using a second Servo object to control the second servo.

O.k. basic question - what pins are your servos connected to? Can't tell from the code and you've provided no schematic/circuit diagram.

You only have one thing being driven as a servo and that's called 'handle'. Which of the servos is that?

Steve

I was going by code posted in the project by 3dfuns. The pins used on the ESP8622 are the D2 (correlating to the 4 pin in the code) for the reverse direction and D8 (correlating to the 15 pin in the code) for the forward direction. I searched online to find the corresponding pin numbers. those pins are connected to the data line on my continuous servo, FS90R. I realize it says handle for the servo, which is attached to the 5 pin to make the 180 servo (SG90) turn the front wheel left/right between 60 to 120 degrees and the term motor is used, but motor is what Range was defined as and was supposed to make the rear wheel attached to the continuous servo go forward when between 1500 and 2000 and reverse when between 1500 and 1000 (i had that wrong above, apologies). I assume it was because if servo was listed twice, it would cause issues? Thank you all for this help, it really is helping me see some things.

// Pin number --> neopixel led 13/ servo 5/ motor 4,15
// Range --> motor 1000~2000 / Servo 60~120 degree

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <Servo.h>
#include "Adafruit_NeoPixel.h"
                                                                                                                                                                
#define motor_d1 4
#define motor_d2 15
#define led_lighting 13
#define led_num 1
#define servo_streeing 5

Adafruit_NeoPixel lighting = Adafruit_NeoPixel(led_num, led_lighting, NEO_GRB + NEO_KHZ800);

const char *ssid = "CMs_autobike";
const char *pw = "";   
IPAddress ip(192, 168, 0, 1);
IPAddress netmask(255, 255, 255, 0);
const int port = 9876;

int led_state;
int motor_speed = 0;
int servo_angle = 0;
int val_motor;

char cmd[100];
int cmdIndex = 0;

WiFiServer server(port);
WiFiClient client;

Servo handle;

void setup() {
    pinMode(motor_d1, OUTPUT);
    pinMode(motor_d2, OUTPUT);
    pinMode(led_lighting, OUTPUT);

    analogWrite(motor_d1, 0);
    analogWrite(motor_d2, 0);

    handle.attach(servo_streeing);
    // handle.write(50);

    lighting.begin(); 
    lighting.setBrightness(100);
    lighting.setPixelColor(0, 0, 0, 0);
    lighting.show(); 

    WiFi.softAPConfig(ip, ip, netmask);
    WiFi.softAP(ssid, pw);

    server.begin();
    Serial.begin(115200);
}

boolean cmdStartsWith(const char *st) {
    for (int i = 0;; i++) {
        if (st[i] == 0) return true;
        if (cmd[i] == 0) return false;
        if (cmd[i] != st[i]) return false;
    }
    return false;
}

void exeCmd() {

    if(cmdStartsWith("th") ) {
    motor_speed = atoi(cmd+3);
    }

    if(motor_speed>1500) {  
       val_motor = map(motor_speed, 1500, 2000, 0, 1023);       
       analogWrite(motor_d2, val_motor);
//       Serial.print("th 1500 > ");
//       Serial.println(val_motor);     
    }
    if(motor_speed<1500) {  
       val_motor = map(motor_speed, 1500, 1000, 0, 1023);       
       analogWrite(motor_d1, val_motor);
//       Serial.print("th 1500 < ");
//       Serial.println(val_motor);  
    }
    if(motor_speed == 1500 ) {
      analogWrite(motor_d1, 0);
      analogWrite(motor_d2, 0);
    }

    if (cmdStartsWith("st") ) {
    servo_angle = atoi(cmd+3);
    handle.write(servo_angle);
//    Serial.println(servo_angle);
    }

    if (cmdStartsWith("w")) {
        led_state = (led_state == LOW) ? HIGH : LOW;
        if(led_state == HIGH) {
          lighting.setPixelColor(0, 255, 255, 255);
          lighting.show(); 
        } else{
          lighting.setPixelColor(0, 0, 0, 0);
          lighting.show(); 
        }
    }
}

void loop() {
    if (!client.connected()) {
        client = server.available();
        return;
    }

    if (client.available()) {
        char c = (char)client.read();

        if (c == '\n') {
            cmd[cmdIndex] = 0;
            exeCmd();
            cmdIndex = 0;
        } else {
            cmd[cmdIndex] = c;
            if (cmdIndex < 99) cmdIndex++;
      }
 }

lemme give that a go, i assume the Servo.H library
will cover both servos without any conflict and just replace the term motor to servo throughout the code will suffice ?

No, a continuous servo is not controlled by analogWrite as you try to control your motor now. You must use the servo.write() command to control your continuous servo. Like

Servo handle;
Servo motor;
...
motor.attach(motorPin);
...
motor.write(motor_speed);

You have only one pin for this motor just like your 'handle' servo and mapping doesn't seem to be necessary. The values for 'motor_speed' are ok to drive the continuous servo directly.

Note that using motor.write() you will need values of motor_speed approximately 90 = stop, 0-89 reverse and 91-180 forward. The stop value may not be exactly 90 but it will be close (continuous servos are not precision devices).

If instead of write() you use motor.writeMicroseconds() your original values 1000 -1500 reverse and 1500-2000 forward should work where approx 1500 is stopped.

Give it a try, post your new code and let us know any problems and we can work from there.

Steve

You safely can use write() instead of writeMicroseconds(). Parameter values greater than MIN_PULSE_WIDTH will be treated as microseconds by the library. But of course writeMicroseconds() is possible too.

Yes I know but it's not a good idea to rely on that. It will confuse anyone looking at the code who sees from the servo library documentation that write() takes values 0-180 and wonders why you're doing write(1500). Anyway lets see how far the OP gets with the suggestions before we confuse him even more.

Steve

Thank you, all. I will give these a try this week and reply back with my findings. You guys have been great in helping me out and I appreciated it. Be Blessed!

AGAIN: You don't use two analogWrite() pins to control a continuous-rotation servo. You control it like any other servo. The values 1000 to 2000 represent servo control pulse widths in microseconds (similar to using angles from 0 to 180 but with finer control). On a continuous-rotation servo, the 'angles' represent speeds from full reverse (1000), to stop (about 1500), to full forward (2000).

Here is my best guess about what you intended your sketch to do:

// Pin numbers -->
// Neopixel : Pin 13
// Steering servo: Pin 5 (60~120 degree)
// Drive servo: Pin 4 (1000~2000 microsecond pulses)

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <Servo.h>
#include "Adafruit_NeoPixel.h"

Servo SteeringServo;
Servo DriveServo;

const byte NeopixelPin = 13;
const byte SteeringServoPin = 5;
const byte DriveServoPin = 4;

const byte LEDCount = 1;
boolean LEDState = false;  // false=Black, true=White

Adafruit_NeoPixel lighting = Adafruit_NeoPixel(LEDCount, NeopixelPin, NEO_GRB + NEO_KHZ800);

const char *ssid = "CMs_autobike";
const char *pw = "";
IPAddress ip(192, 168, 0, 1);
IPAddress netmask(255, 255, 255, 0);
const int port = 9876;

WiFiServer server(port);
WiFiClient client;

void setup()
{
  Serial.begin(115200);
  delay(200);

  SteeringServo.attach(SteeringServoPin);
  DriveServo.attach(DriveServoPin);

  lighting.begin();
  lighting.setBrightness(100);
  lighting.setPixelColor(0, 0, 0, 0);
  lighting.show();

  WiFi.softAPConfig(ip, ip, netmask);
  WiFi.softAP(ssid, pw);

  server.begin();
}

void loop()
{
  if (!client.connected())
  {
    client = server.available();
    return;
  }

  if (client.available())
  {
    String command = client.readString();
    if (command.startsWith("th"))
    {
      command = command.substring(3);
      int driveSpeed = command.toInt();
      DriveServo.writeMicroseconds(driveSpeed);
    }

    if (command.startsWith("st"))
    {
      command = command.substring(3);
      int servo_angle = command.toInt();

      SteeringServo.write(servo_angle);
    }

    if (command.startsWith("w"))
    {
      LEDState = !LEDState; // Toggle

      if (LEDState)
        lighting.setPixelColor(0, 255, 255, 255);
      else
        lighting.setPixelColor(0, 0, 0, 0);
      lighting.show();
    }
  }
}