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