-1

I'm experimenting with classes in Arduino code. I have the following small code snippet:

#include "Motor.h"
#include "Arduino.h"
Motor::Motor()
{
}
void Motor::Configure(uint16_t inA_pin, uint16_t inB_pin, uint16_t speed_pin, Stream* SerPort)
{
 uint16_t _inA_pin = inA_pin;
 uint16_t _inB_pin = inB_pin;
 uint16_t _speed_pin = speed_pin;
 gl_pSerPort = SerPort;
 gl_pSerPort->printf("...._inA_pin set to %d\n", _inA_pin);
 gl_pSerPort->printf("...._inB_pin set to %d\n", _inB_pin);
 gl_pSerPort->printf("...._speed_pin set to %d\n", _speed_pin);
 pinMode(_inA_pin, OUTPUT);
 pinMode(_inB_pin, OUTPUT);
 pinMode(_speed_pin, OUTPUT);
}
void Motor::RunMsec(bool bisFwd, uint16_t onMsec, uint16_t speed)
{
 gl_pSerPort->printf("RunMsec(%d, %d, %d\n", bisFwd, onMsec, speed);
 gl_pSerPort->printf("RunMsec: _inB_pin = %d\n", _inB_pin);
 gl_pSerPort->printf("RunMsec: _speed_pin = %d\n", _speed_pin);
 gl_pSerPort->printf("%lu: Calling Run(%d, %d)\n",millis(), bisFwd,speed);
 Run(bisFwd, speed);
 //Step 2: Delay timsec seconds
 delay(onMsec);
 //Step3: Stop motors added 04/12/21
 gl_pSerPort->printf("%lu: Calling Stop()\n",millis());
 Stop();
}

The Motor.h file is:

#pragma once
#include "Arduino.h"
const uint16_t MOTOR_SPEED_FULL = 200; //range is 0-255
const uint16_t MOTOR_SPEED_MAX = 255; //range is 0-255
const uint16_t MOTOR_SPEED_HALF = 127; //range is 0-255
const uint16_t MOTOR_SPEED_QTR = 75; //added 09/25/20
const uint16_t MOTOR_SPEED_LOW = 50; //added 01/22/15
const uint16_t MOTOR_SPEED_MIN = 15; //added 12/24/23 for 'mirrored sfc' detection
const uint16_t MOTOR_SPEED_OFF = 0; //range is 0-255
const uint16_t MOTOR_SPEED_CAPTURE_OFFSET = 75; //added 06/21/20 for offset capture
const uint16_t TURN_START_SPEED = MOTOR_SPEED_QTR; //added 11/14/21
class Motor
{
public:
 Motor();
 void Configure(uint16_t inA_pin, uint16_t inB_pin, uint16_t speed_pin, Stream* gl_pSerPort);
 void RunMsec(bool bIsFwd = true, uint16_t onMsec = 1000, uint16_t speed = MOTOR_SPEED_HALF);
 void Run(bool bIsFwd = true, uint16_t speed = MOTOR_SPEED_HALF);
 void Stop();
private:
 uint16_t _inA_pin;
 uint16_t _inB_pin;
 uint16_t _speed_pin;
 Stream* gl_pSerPort;
 void SetDirAndSpeed(bool bIsFwd, uint16_t speed);
};

The code in the corresponding .ino file is:

#include "Motor.h"
#include <Wire.h>
#include "FlashTxx.h" // TLC/T3x/T4x flash primitives
#pragma region MOTOR PINS
//11/04/21 Now using Pololu VNH5019 drivers for all 4 motors
const uint16_t InA_Left = 22;
const uint16_t InB_Left = 21;
const uint16_t Spd_Left = 23;
const uint16_t InA_Right = 34;
const uint16_t InB_Right = 33;
const uint16_t Spd_Right = 35;
#pragma endregion MOTOR PINS
Motor LeftMotor;
Motor RightMotor;
 Stream* gl_pSerPort = 0; //09/26/22 made global so can use everywhere.
void setup()
{
#pragma region SERIAL_PORTS
 Serial.begin(115200);
 delay(2000); //10/06/21 - just use fixed delay instead
 Serial1.begin(115200);
 delay(2000); //11/20/21 use fixed delay instead of waiting
 if (Serial)
 {
 Serial.printf("Serial port active\n");
 gl_pSerPort = (Stream*)&Serial;
 }
 else if (Serial1)
 {
 Serial.printf("Serial1 port active\n");
 gl_pSerPort = (Stream*)&Serial1;
 }
 gl_pSerPort->printf("gl_pSerPort now points to active Serial (USB or Wixel)\n");
 //02/25/23 added for Garmin LIDAR-Lite V4/LED use
 Serial2.begin(115200);
 delay(2000); //11/20/21 use fixed delay instead of waiting
 LeftMotor.Configure(InA_Left, InB_Left, Spd_Left, gl_pSerPort);
 RightMotor.Configure(InA_Right, InB_Right, Spd_Right, gl_pSerPort);
#pragma endregion SERIAL_PORTS
}
void loop()
{
 //redLED.Blink(2000);
 //delay(200);
 //yelLED.Blink(2000);
 //delay(1000);
 gl_pSerPort->printf("%lu: LeftMotor.RunMsec()\n", millis());
 LeftMotor.RunMsec(5000); //fwd for 1sec
 delay(500);
 gl_pSerPort->printf("%lu: RightMotor.RunMsec()\n", millis());
 RightMotor.RunMsec(5000); //fwd for 1sec
 delay(500);
}

When this is run, I get the following output from the embedded print statements:

Opening port
Port open
Serial port active
gl_pSerPort now points to active Serial (USB or Wixel)
...._inA_pin set to 22
...._inB_pin set to 21
...._speed_pin set to 23
...._inA_pin set to 34
...._inB_pin set to 33
...._speed_pin set to 35
6300: LeftMotor.RunMsec()
RunMsec(1, 1000, 127
RunMsec: _inB_pin = 0
RunMsec: _speed_pin = 0
6300: Calling Run(1, 127)
In SetDirAndSpeed(true, 127)
In TRUE block of SetRighttMotorDirAndSpeed(true, 127)
....Pin 0 set to 1
....Pin 0 set to 1
7300: Calling Stop()
In SetDirAndSpeed(true, 0)
In TRUE block of SetRighttMotorDirAndSpeed(true, 0)
....Pin 0 set to 0
....Pin 0 set to 0
7800: RightMotor.RunMsec()

From the printouts, I can see that Motor::Configure() properly initializes the private member variables, but Motor::RunMsec() reports that _inB_pin = 0, and _speed_pin = 0, so something is preventing RunMsec from seeing the values set by Configure().

Can anyone see what I'm doing wrong here?

TIA,

Frank

asked Jun 3, 2024 at 22:48
3
  • I tried making changing the private variables to 'public' (by commenting out the 'private:' line), but this had no effect - the variable values were still shown as '0' in the RunMsec() function Commented Jun 3, 2024 at 23:04
  • 1
    why are you commenting on your own question? ... if you have anything to add, then please edit the question instead Commented Jun 3, 2024 at 23:55
  • duplicate of arduino.stackexchange.com/questions/57515/… Commented Jun 4, 2024 at 7:11

1 Answer 1

3

In your configure function:

void Motor::Configure(uint16_t inA_pin, uint16_t inB_pin, uint16_t speed_pin, Stream* SerPort)
{
 uint16_t _inA_pin = inA_pin;
 uint16_t _inB_pin = inB_pin;
 uint16_t _speed_pin = speed_pin;
 gl_pSerPort = SerPort;

You are creating new local variables named _inA_pin and _inB_pin and _speed_pin. These new local variables shadow the member variables. You have multiple variables with the same name, and the more local scope is the one that will be used.

I don't think you want to have the uint16_t there. I think you want to put those values into the member variables in the class.

void Motor::Configure(uint16_t inA_pin, uint16_t inB_pin, uint16_t speed_pin, Stream* SerPort)
{
 _inA_pin = inA_pin;
 _inB_pin = inB_pin;
 _speed_pin = speed_pin;
 gl_pSerPort = SerPort;
answered Jun 4, 2024 at 0:09
1
  • DUH!! Thanks for pointing out the obvious (to everyone but me!) error. Commented Jun 4, 2024 at 0:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.