Stepper drivers turn on, steppers don't move

It's a long journey but a fun one.

Back from a good weekend, ready to get this damned car rolling!

So I checked out that dronebotworkshop sketch for a while and decided to use most of it and that library. I pretty happy with my progress, although there are a few hiccups, but that's to be expected lol.

Transmitter:

// Include RadioHead ReliableDatagram & NRF24 Libraries
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
 
// Include dependant SPI Library 
#include <SPI.h>

#include "stdio.h"

// Define addresses for radio channels
#define CLIENT_ADDRESS 1   
#define SERVER_ADDRESS 2
 
// Create an instance of the radio driver
RH_NRF24 RadioDriver;
 
// Sets the radio driver to NRF24 and the client address to 1
RHReliableDatagram RadioManager(RadioDriver, CLIENT_ADDRESS);
 
#define xpin A0  // Arduino pin connected to VRX pin
#define ypin A1  // Arduino pin connected to VRY pin
 
// Define the Message Buffer
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

// Declare unsigned 8-bit joystick array
uint8_t joystick[3]; 

int xValue = 0;  // To store value of the X axis
int yValue = 0;  // To store value of the Y axis
int previousXValue = 0; // Set initial previous values to 0
int previousYValue = 0;

void setup() {

  Serial.begin(9600);  // Start serial communication

  // Initialize RadioManager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!RadioManager.init())
    Serial.println("init failed");

  Serial.println("Setup complete.");
}

void loop() {

  // read analog X and Y analog values
  xValue = analogRead(xpin);
  yValue = analogRead(ypin);

  // remap the x and y values to be 0-7
  int xMap = map(xValue, 0, 1023, 0, 7);
  int yMap = map(yValue, 0, 1023, 0, 7);
  joystick[0] = map(xValue, 0, 1023, 0, 7);
  joystick[1] = map(yValue, 0, 1023, 0, 7);

// turns the two map variables into a single variable by putting the numbers side by side
  String powersCombinedStr = String(xMap) + String(yMap);
  int powersCombined = powersCombinedStr.toInt();

  Serial.print("xMap = ");
  Serial.print(xMap);
  Serial.print(" | yMap = ");
  Serial.print(yMap);
  Serial.print(" | xValue = ");
  Serial.print(xValue);
  Serial.print(" | yValue = ");
  Serial.print(yValue);
  Serial.print(" | Case#: ");
  Serial.println(powersCombinedStr);

  if (xValue != previousXValue || yValue != previousYValue) { // Check if values have changed

    // Update previous values
    previousXValue = xValue;
    previousYValue = yValue;

    //Send a message containing Joystick data to manager_server
  if (RadioManager.sendtoWait(joystick, sizeof(joystick), SERVER_ADDRESS))
  {
    // Now wait for a reply from the server
    uint8_t len = sizeof(buf);
    uint8_t from;
    if (RadioManager.recvfromAckTimeout(buf, &len, 2000, &from))
    {
      Serial.print("got reply from : 0x");
      Serial.print(from, HEX);
      Serial.print(": ");
      Serial.println((char*)buf);
    }
    else
    {
      Serial.println("No reply, is nrf24_reliable_datagram_server running?");
    }
  }
  else
    Serial.println("sendtoWait failed");
 
  delay(100);  // Wait a bit before next transmission
}
}

Serial Output:

xMap = 3 | yMap = 7 | xValue = 505 | yValue = 1023 | Case#: 37
xMap = 3 | yMap = 7 | xValue = 505 | yValue = 1023 | Case#: 37
xMap = 3 | yMap = 7 | xValue = 505 | yValue = 1023 | Case#: 37
xMap = 3 | yMap = 7 | xValue = 505 | yValue = 1023 | Case#: 37
xMap = 3 | yMap = 7 | xValue = 505 | yValue = 1023 | Case#: 37
xMap = 3 | yMap = 7 | xValue = 505 | yValue = 1023 | Case#: 37
xMap = 3 | yMap = 7 | xValue = 505 | yValue = 1023 | Case#: 37
xMap = 3 | yMap = 7 | xValue = 505 | yValue = 1023 | Case#: 37
xMap = 3 | yMap = 7 | xValue = 505 | yValue = 1023 | Case#: 37
xMap = 3 | yMap = 7 | xValue = 505 | yValue = 1023 | Case#: 37
xMap = 3 | yMap = 6 | xValue = 504 | yValue = 1009 | Case#: 36
sendtoWait failed
xMap = 3 | yMap = 6 | xValue = 453 | yValue = 886 | Case#: 36
sendtoWait failed
xMap = 3 | yMap = 0 | xValue = 470 | yValue = 0 | Case#: 30
sendtoWait failed
xMap = 3 | yMap = 0 | xValue = 504 | yValue = 0 | Case#: 30
sendtoWait failed
xMap = 3 | yMap = 0 | xValue = 504 | yValue = 0 | Case#: 30
xMap = 3 | yMap = 0 | xValue = 504 | yValue = 2 | Case#: 30
sendtoWait failed
xMap = 3 | yMap = 0 | xValue = 504 | yValue = 2 | Case#: 30
xMap = 3 | yMap = 0 | xValue = 504 | yValue = 0 | Case#: 30
sendtoWait failed
xMap = 3 | yMap = 0 | xValue = 503 | yValue = 0 | Case#: 30
sendtoWait failed
xMap = 3 | yMap = 3 | xValue = 507 | yValue = 500 | Case#: 33
sendtoWait failed
xMap = 3 | yMap = 3 | xValue = 507 | yValue = 500 | Case#: 33
xMap = 3 | yMap = 3 | xValue = 507 | yValue = 500 | Case#: 33
xMap = 3 | yMap = 3 | xValue = 507 | yValue = 500 | Case#: 33
xMap = 3 | yMap = 3 | xValue = 507 | yValue = 500 | Case#: 33
xMap = 3 | yMap = 3 | xValue = 507 | yValue = 500 | Case#: 33
xMap = 3 | yMap = 3 | xValue = 507 | yValue = 500 | Case#: 33
xMap = 3 | yMap = 3 | xValue = 507 | yValue = 500 | Case#: 33
xMap = 3 | yMap = 3 | xValue = 507 | yValue = 500 

Receiver:

// Include RadioHead ReliableDatagram & NRF24 Libraries
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>

// Include dependant SPI Library
#include <SPI.h>

#include "stdio.h"

// Define addresses for radio channels
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2

// Create an instance of the radio driver
RH_NRF24 RadioDriver;

// Sets the radio driver to NRF24 and the server address to 2
RHReliableDatagram RadioManager(RadioDriver, SERVER_ADDRESS);

// Define a message to return if values received
uint8_t ReturnMessage[] = "JoyStick Data Received";

// Define the Message Buffer
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

void setup() {
  // Setup Serial Monitor
  Serial.begin(9600);

  // Initialize RadioManager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!RadioManager.init())
    Serial.println("init failed");
}

void loop() {
  if (RadioManager.available()) {
    // Wait for a message addressed to us from the client
    uint8_t len = sizeof(buf);
    uint8_t from;
    if (RadioManager.recvfromAck(buf, &len, &from))
    //Serial Print the values of joystick
    {
      Serial.print("got request from : 0x");
      Serial.print(from, HEX);
      Serial.print(": X = ");
      Serial.print(buf[0]);
      Serial.print(" Y = ");
      Serial.print(buf[1]);

      // Send a reply back to the originator client, check for error
      if (!RadioManager.sendtoWait(ReturnMessage, sizeof(ReturnMessage), from))
        Serial.println("sendtoWait failed");

      // turns the two map variables into a single variable by putting the numbers side by side
      String powersCombinedStr = String(buf[0]) + String(buf[1]);
      int powersCombined = powersCombinedStr.toInt();

      switch (powersCombined) {
        case 30:
          Serial.println("UP");
          break;

        case 33:
          Serial.println("NONE");
          break;

        case 37:
          Serial.println("DOWN");
          break;

        case 03:
          Serial.println("LEFT");
          break;

        case 73:
          Serial.println("RIGHT");
          break;

        case 00:
          Serial.println("UP-LEFT");
          break;

        case 70:
          Serial.println("UP-RIGHT");
          break;

        case 07:
          Serial.println("DOWN-LEFT");
          break;

        case 77:
          Serial.println("DOWN-RIGHT");
          break;
      }
    }
  }
}

Serial Output:

got request from : 0x1: X = 3 Y = 3sendtoWait failed

NONE

got request from : 0x1: X = 3 Y = 3NONE

got request from : 0x1: X = 3 Y = 3NONE

got request from : 0x1: X = 3 Y = 3NONE

got request from : 0x1: X = 3 Y = 3NONE

got request from : 0x1: X = 3 Y = 3NONE

got request from : 0x1: X = 3 Y = 3NONE

got request from : 0x1: X = 3 Y = 3NONE

got request from : 0x1: X = 3 Y = 3NONE

got request from : 0x1: X = 3 Y = 3NONE

got request from : 0x1: X = 3 Y = 3sendtoWait failed

NONE

got request from : 0x1: X = 3 Y = 3sendtoWait failed

NONE

got request from : 0x1: X = 3 Y = 3sendtoWait failed

NONE

got request from : 0x1: X = 3 Y = 3sendtoWait failed

NONE

got request from : 0x1: X = 3 Y = 3sendtoWait failed

NONE

got request from : 0x1: X = 3 Y = 3sendtoWait failed

NONE

got request from : 0x1: X = 3 Y = 3sendtoWait failed

NONE

got request from : 0x1: X = 3 Y = 3sendtoWait failed

NONE

got request from : 0x1: X = 3 Y = 7sendtoWait failed

DOWN

got request from : 0x1: X = 3 Y = 6sendtoWait failed

got request from : 0x1: X = 3 Y = 7sendtoWait failed

DOWN

got request from : 0x1: X = 3 Y = 6sendtoWait failed

got request from : 0x1: X = 3 Y = 0sendtoWait failed

UP

got request from : 0x1: X = 3 Y = 0sendtoWait failed

UP

After the receiver successfully receives the data, it's suppose to respond back, but it's not going through. Not really sure why. I'm getting the data correctly, so I might just take that part out, but I feel as though that would be a mistake for future debugging. I also notice that the data rates are staggered. It's not consistent. For example, 3 lines will print to the monitor of the transmitter, then 1 line prints to the receiver, then 1 and 1, then 5 and 3, and so on. Again, not really sure why it does this.

I put the switch statement on the receiver end, that way I can easily use each case to put the corresponding code for said direction, but at the moment it only prints the direction that case is for into the serial monitor.

The printing to the monitor just might be slow so that is to be expected. It is sometimes choppy when there is a lot going on. Good you got a lot done, though. I would just drop the feedback from the rcvr for now and see if the car drives ok.

1 Like

Okay, fair enough. I'll take your advice and just comment that out for now and work on getting stepper motors to work.

I know that with my current code, driving is gonna be sketchy lol. I think I could fix it a bit by adding more case #'s to each case. For example:

      switch (powersCombined) {
       case 30:
       case 31:
       case 32:
          Serial.println("UP");
          break;

That should reduce the amount of deadzones in the joystick. I also think I should increase the baud rate to increase the amount of updates per sec. I'll need a second joystick, so I need to keep that in mind for spinning and the telescopic lift, but for now I need to get the first one to move the car then I can worry about adding the second one.

Here's my receiver sketch now. I'm sure there are errors. I was examining two different sketches to put this together, and I know I got mixed up a few times. I think it's mostly okay though (it at least verifies).

Now I need to wire everything to the Mega (had to switch the receiver to a Mega2560 because I didn't have enough digital pins open.)

// Include RadioHead ReliableDatagram & NRF24 Libraries
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
// Include dependant SPI Library
#include <SPI.h>
#include <AccelStepper.h>
#include "stdio.h"
// Define addresses for radio channels
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2

// Create an instance of the radio driver
RH_NRF24 RadioDriver;

// Sets the radio driver to NRF24 and the server address to 2
RHReliableDatagram RadioManager(RadioDriver, SERVER_ADDRESS);

// Define a message to return if values received
uint8_t ReturnMessage[] = "JoyStick Data Received";

// Define the Message Buffer
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

// Motor pin definitions:
#define motorPinA1 22  // IN1 on the ULN2003 driver
#define motorPinA2 23  // IN2 on the ULN2003 driver
#define motorPinA3 24  // IN3 on the ULN2003 driver
#define motorPinA4 25  // IN4 on the ULN2003 driver
#define motorPinB1 26  // IN1 on the ULN2003 driver
#define motorPinB2 27  // IN2 on the ULN2003 driver
#define motorPinB3 28  // IN3 on the ULN2003 driver
#define motorPinB4 29  // IN4 on the ULN2003 driver
#define motorPinC1 30  // IN1 on the ULN2003 driver
#define motorPinC2 31  // IN2 on the ULN2003 driver
#define motorPinC3 32  // IN3 on the ULN2003 driver
#define motorPinC4 33  // IN4 on the ULN2003 driver
#define motorPinD1 34  // IN1 on the ULN2003 driver
#define motorPinD2 35  // IN2 on the ULN2003 driver
#define motorPinD3 36  // IN3 on the ULN2003 driver
#define motorPinD4 37  // IN4 on the ULN2003 driver

// Define the AccelStepper interface type; 4 wire motor in half step mode:
#define MotorInterfaceType 8

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper library with 28BYJ-48 stepper motor:
AccelStepper LeftFrontWheel = AccelStepper(MotorInterfaceType, motorPinA1, motorPinA3, motorPinA2, motorPinA4);
AccelStepper LeftBackWheel = AccelStepper(MotorInterfaceType, motorPinB1, motorPinB3, motorPinB2, motorPinB4);
AccelStepper RightFrontWheel = AccelStepper(MotorInterfaceType, motorPinC1, motorPinC3, motorPinC2, motorPinC4);
AccelStepper RightBackWheel = AccelStepper(MotorInterfaceType, motorPinD1, motorPinD3, motorPinD2, motorPinD4);

int goSpeed = 1000;

void setup() {
  // Setup Serial Monitor
  Serial.begin(9600);

  // Set the maximum steps per second:
  LeftFrontWheel.setMaxSpeed(1000);
  LeftBackWheel.setMaxSpeed(1000);
  RightFrontWheel.setMaxSpeed(1000);
  RightBackWheel.setMaxSpeed(1000);

  // Set the maximum acceleration in steps per second^2:
  LeftFrontWheel.setAcceleration(200);
  LeftBackWheel.setAcceleration(200);
  RightFrontWheel.setAcceleration(200);
  RightBackWheel.setAcceleration(200);


  // Initialize RadioManager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!RadioManager.init())
    Serial.println("init failed");
}

void loop() {
  if (RadioManager.available()) {
    // Wait for a message addressed to us from the client
    uint8_t len = sizeof(buf);
    uint8_t from;
    if (RadioManager.recvfromAck(buf, &len, &from))
    //Serial Print the values of joystick
    {
      Serial.print("got request from : 0x");
      Serial.print(from, HEX);
      Serial.print(": X = ");
      Serial.print(buf[0]);
      Serial.print(" Y = ");
      Serial.print(buf[1]);

      // Send a reply back to the originator client, check for error
      //  if (!RadioManager.sendtoWait(ReturnMessage, sizeof(ReturnMessage), from))
      //    Serial.println("sendtoWait failed");


      // turns the two map variables into a single variable by putting the numbers side by side
      String powersCombinedStr = String(buf[0]) + String(buf[1]);
      int powersCombined = powersCombinedStr.toInt();


      switch (powersCombined) {
        case 30:
          Serial.println("UP");
          moveForward();
          break;

        case 33:
          Serial.println("NONE");
          stopMoving();
          break;

        case 37:
          Serial.println("DOWN");
          moveBackward();
          break;

        case 03:
          Serial.println("LEFT");
          moveSidewaysLeft();
          break;

        case 73:
          Serial.println("RIGHT");
          moveSidewaysRight();
          break;

        case 00:
          Serial.println("UP-LEFT");
          moveLeftBackward();
          break;

        case 70:
          Serial.println("UP-RIGHT");
          moveRightForward();
          break;

        case 07:
          Serial.println("DOWN-LEFT");
          moveLeftBackward();
          break;

        case 77:
          Serial.println("DOWN-RIGHT");
          moveRightBackward();
          break;
      }
    }
  }
}
void moveForward() {
  LeftFrontWheel.setSpeed(goSpeed);
  LeftBackWheel.setSpeed(goSpeed);
  RightFrontWheel.setSpeed(goSpeed);
  RightBackWheel.setSpeed(goSpeed);
}
void moveBackward() {
  LeftFrontWheel.setSpeed(-goSpeed);
  LeftBackWheel.setSpeed(-goSpeed);
  RightFrontWheel.setSpeed(-goSpeed);
  RightBackWheel.setSpeed(-goSpeed);
}
void moveSidewaysRight() {
  LeftFrontWheel.setSpeed(goSpeed);
  LeftBackWheel.setSpeed(-goSpeed);
  RightFrontWheel.setSpeed(-goSpeed);
  RightBackWheel.setSpeed(goSpeed);
}
void moveSidewaysLeft() {
  LeftFrontWheel.setSpeed(-goSpeed);
  LeftBackWheel.setSpeed(goSpeed);
  RightFrontWheel.setSpeed(goSpeed);
  RightBackWheel.setSpeed(-goSpeed);
}
void rotateLeft() {
  LeftFrontWheel.setSpeed(-goSpeed);
  LeftBackWheel.setSpeed(-goSpeed);
  RightFrontWheel.setSpeed(goSpeed);
  RightBackWheel.setSpeed(goSpeed);
}
void rotateRight() {
  LeftFrontWheel.setSpeed(goSpeed);
  LeftBackWheel.setSpeed(goSpeed);
  RightFrontWheel.setSpeed(-goSpeed);
  RightBackWheel.setSpeed(-goSpeed);
}
void moveRightForward() {
  LeftFrontWheel.setSpeed(goSpeed);
  LeftBackWheel.setSpeed(0);
  RightFrontWheel.setSpeed(0);
  RightBackWheel.setSpeed(goSpeed);
}
void moveRightBackward() {
  LeftFrontWheel.setSpeed(0);
  LeftBackWheel.setSpeed(-goSpeed);
  RightFrontWheel.setSpeed(-goSpeed);
  RightBackWheel.setSpeed(0);
}
void moveLeftForward() {
  LeftFrontWheel.setSpeed(0);
  LeftBackWheel.setSpeed(goSpeed);
  RightFrontWheel.setSpeed(goSpeed);
  RightBackWheel.setSpeed(0);
}
void moveLeftBackward() {
  LeftFrontWheel.setSpeed(-goSpeed);
  LeftBackWheel.setSpeed(0);
  RightFrontWheel.setSpeed(0);
  RightBackWheel.setSpeed(-goSpeed);
}
void stopMoving() {
  LeftFrontWheel.setSpeed(0);
  LeftBackWheel.setSpeed(0);
  RightFrontWheel.setSpeed(0);
  RightBackWheel.setSpeed(0);
}

I'm not asking anyone to just fix my code, but does it look alright? I'm sure there is some unneeded lines and some redundancy in there, but overall... is it correct?

Transmitter:

// Include RadioHead ReliableDatagram & NRF24 Libraries
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
 
// Include dependant SPI Library 
#include <SPI.h>

#include "stdio.h"

// Define addresses for radio channels
#define CLIENT_ADDRESS 1   
#define SERVER_ADDRESS 2
 
// Create an instance of the radio driver
RH_NRF24 RadioDriver;
 
// Sets the radio driver to NRF24 and the client address to 1
RHReliableDatagram RadioManager(RadioDriver, CLIENT_ADDRESS);
 
#define xpin A0  // Arduino pin connected to VRX pin
#define ypin A1  // Arduino pin connected to VRY pin
 
// Define the Message Buffer
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

// Declare unsigned 8-bit joystick array
uint8_t joystick[3]; 

int xValue = 0;  // To store value of the X axis
int yValue = 0;  // To store value of the Y axis
int previousXValue = 0; // Set initial previous values to 0
int previousYValue = 0;

void setup() {

  Serial.begin(9600);  // Start serial communication

  // Initialize RadioManager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!RadioManager.init())
    Serial.println("init failed");

  else Serial.println("Setup complete.");
}

void loop() {

  // read analog X and Y analog values
  xValue = analogRead(xpin);
  yValue = analogRead(ypin);

  // remap the x and y values to be 0-7
  int xMap = map(xValue, 0, 1023, 0, 7);
  int yMap = map(yValue, 0, 1023, 0, 7);
  joystick[0] = map(xValue, 0, 1023, 0, 7);
  joystick[1] = map(yValue, 0, 1023, 0, 7);

// turns the two map variables into a single variable by putting the numbers side by side
  String powersCombinedStr = String(xMap) + String(yMap);
  int powersCombined = powersCombinedStr.toInt();

  Serial.print("xMap = ");
  Serial.print(xMap);
  Serial.print(" | yMap = ");
  Serial.print(yMap);
  Serial.print(" | xValue = ");
  Serial.print(xValue);
  Serial.print(" | yValue = ");
  Serial.print(yValue);
  Serial.print(" | Case#: ");
  Serial.println(powersCombinedStr);

  if (xValue != previousXValue || yValue != previousYValue) { // Check if values have changed

    // Update previous values
    previousXValue = xValue;
    previousYValue = yValue;

    //Send a message containing Joystick data to manager_server
  if (RadioManager.sendtoWait(joystick, sizeof(joystick), SERVER_ADDRESS))
  {
    // Now wait for a reply from the server
    uint8_t len = sizeof(buf);
    uint8_t from;
    if (RadioManager.recvfromAckTimeout(buf, &len, 2000, &from))
    {
      Serial.print("got reply from : 0x");
      Serial.print(from, HEX);
      Serial.print(": ");
      Serial.println((char*)buf);
       }
  //   else
  //   {
  //     Serial.println("No reply, is nrf24_reliable_datagram_server running?");
  //   }
  // }
  // else
  //   Serial.println("sendtoWait failed");
 
  delay(100);  // Wait a bit before next transmission
}
}
}

Serial Output:

xMap = 3 | yMap = 3 | xValue = 507 | yValue = 500 | Case#: 33
xMap = 3 | yMap = 3 | xValue = 507 | yValue = 500 | Case#: 33
xMap = 3 | yMap = 3 | xValue = 507 | yValue = 500 | Case#: 33
xMap = 3 | yMap = 3 | xValue = 507 | yValue = 500 | Case#: 33
xMap = 3 | yMap = 3 | xValue = 507 | yValue = 500 | Case#: 33
xMap = 3 | yMap = 3 | xValue = 507 | yValue = 500 | Case#: 33

Receiver:

// Include RadioHead ReliableDatagram & NRF24 Libraries
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
// Include dependant SPI Library
#include <SPI.h>
#include <AccelStepper.h>
#include "stdio.h"

//RF24 rf_manager(48, 49);   // nRF24L01 (CE, CSN)

// Define addresses for radio channels
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2

#define RF_CE_PIN 48
#define RF_CSN_PIN 49

RH_NRF24 nrf24(RF_CE_PIN, RF_CSN_PIN);
RHReliableDatagram rf_manager(nrf24, 2);

// Define a message to return if values received
uint8_t ReturnMessage[] = "JoyStick Data Received";

// Define the Message Buffer
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

// Motor pin definitions:
#define motorPinA1 22  // IN1 on the ULN2003 driver
#define motorPinA2 23  // IN2 on the ULN2003 driver
#define motorPinA3 24  // IN3 on the ULN2003 driver
#define motorPinA4 25  // IN4 on the ULN2003 driver
#define motorPinB1 26  // IN1 on the ULN2003 driver
#define motorPinB2 27  // IN2 on the ULN2003 driver
#define motorPinB3 28  // IN3 on the ULN2003 driver
#define motorPinB4 29  // IN4 on the ULN2003 driver
#define motorPinC1 30  // IN1 on the ULN2003 driver
#define motorPinC2 31  // IN2 on the ULN2003 driver
#define motorPinC3 32  // IN3 on the ULN2003 driver
#define motorPinC4 33  // IN4 on the ULN2003 driver
#define motorPinD1 34  // IN1 on the ULN2003 driver
#define motorPinD2 35  // IN2 on the ULN2003 driver
#define motorPinD3 36  // IN3 on the ULN2003 driver
#define motorPinD4 37  // IN4 on the ULN2003 driver

// Define the AccelStepper interface type; 4 wire motor in half step mode:
#define MotorInterfaceType 8

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper library with 28BYJ-48 stepper motor:
AccelStepper LeftFrontWheel = AccelStepper(MotorInterfaceType, motorPinA1, motorPinA3, motorPinA2, motorPinA4);
AccelStepper LeftBackWheel = AccelStepper(MotorInterfaceType, motorPinB1, motorPinB3, motorPinB2, motorPinB4);
AccelStepper RightFrontWheel = AccelStepper(MotorInterfaceType, motorPinC1, motorPinC3, motorPinC2, motorPinC4);
AccelStepper RightBackWheel = AccelStepper(MotorInterfaceType, motorPinD1, motorPinD3, motorPinD2, motorPinD4);

int goSpeed = 1000;

void setup() {
  // Setup Serial Monitor
  Serial.begin(9600);

  // Set the maximum steps per second:
  LeftFrontWheel.setMaxSpeed(1000);
  LeftBackWheel.setMaxSpeed(1000);
  RightFrontWheel.setMaxSpeed(1000);
  RightBackWheel.setMaxSpeed(1000);

  // Set the maximum acceleration in steps per second^2:
  LeftFrontWheel.setAcceleration(200);
  LeftBackWheel.setAcceleration(200);
  RightFrontWheel.setAcceleration(200);
  RightBackWheel.setAcceleration(200);

  if (!nrf24.init()) {
    Serial.println("Failed to initialize NRF24L01");
    while (1);
  }

  if (!nrf24.setChannel(1)) {
    Serial.println("Failed to set channel");
    while (1);
  }

  if (!rf_manager.init()) {
    Serial.println("Failed to initialize RFManager");
    while (1);
  }

  Serial.println("Receiver ready!");
}

void loop() {
  if (rf_manager.available()) {
    // Wait for a message addressed to us from the client
    uint8_t len = sizeof(buf);
    uint8_t from;
    if (rf_manager.recvfromAck(buf, &len, &from))
    //Serial Print the values of joystick
    {
      Serial.print("got request from : 0x");
      Serial.print(from, HEX);
      Serial.print(": X = ");
      Serial.print(buf[0]);
      Serial.print(" Y = ");
      Serial.print(buf[1]);

      // Send a reply back to the originator client, check for error
      //  if (!rf_manager.sendtoWait(ReturnMessage, sizeof(ReturnMessage), from))
      //    Serial.println("sendtoWait failed");

      // turns the two map variables into a single variable by putting the numbers side by side
      String powersCombinedStr = String(buf[0]) + String(buf[1]);
      int powersCombined = powersCombinedStr.toInt();


      switch (powersCombined) {
        case 30:
          Serial.println("UP");
          moveForward();
          break;

        case 33:
          Serial.println("NONE");
          stopMoving();
          break;

        case 37:
          Serial.println("DOWN");
          moveBackward();
          break;

        case 03:
          Serial.println("LEFT");
          moveSidewaysLeft();
          break;

        case 73:
          Serial.println("RIGHT");
          moveSidewaysRight();
          break;

        case 00:
          Serial.println("UP-LEFT");
          moveLeftBackward();
          break;

        case 70:
          Serial.println("UP-RIGHT");
          moveRightForward();
          break;

        case 07:
          Serial.println("DOWN-LEFT");
          moveLeftBackward();
          break;

        case 77:
          Serial.println("DOWN-RIGHT");
          moveRightBackward();
          break;
      }
    }
  }
}
void moveForward() {
  LeftFrontWheel.setSpeed(goSpeed);
  LeftBackWheel.setSpeed(goSpeed);
  RightFrontWheel.setSpeed(goSpeed);
  RightBackWheel.setSpeed(goSpeed);
}
void moveBackward() {
  LeftFrontWheel.setSpeed(-goSpeed);
  LeftBackWheel.setSpeed(-goSpeed);
  RightFrontWheel.setSpeed(-goSpeed);
  RightBackWheel.setSpeed(-goSpeed);
}
void moveSidewaysRight() {
  LeftFrontWheel.setSpeed(goSpeed);
  LeftBackWheel.setSpeed(-goSpeed);
  RightFrontWheel.setSpeed(-goSpeed);
  RightBackWheel.setSpeed(goSpeed);
}
void moveSidewaysLeft() {
  LeftFrontWheel.setSpeed(-goSpeed);
  LeftBackWheel.setSpeed(goSpeed);
  RightFrontWheel.setSpeed(goSpeed);
  RightBackWheel.setSpeed(-goSpeed);
}
void rotateLeft() {
  LeftFrontWheel.setSpeed(-goSpeed);
  LeftBackWheel.setSpeed(-goSpeed);
  RightFrontWheel.setSpeed(goSpeed);
  RightBackWheel.setSpeed(goSpeed);
}
void rotateRight() {
  LeftFrontWheel.setSpeed(goSpeed);
  LeftBackWheel.setSpeed(goSpeed);
  RightFrontWheel.setSpeed(-goSpeed);
  RightBackWheel.setSpeed(-goSpeed);
}
void moveRightForward() {
  LeftFrontWheel.setSpeed(goSpeed);
  LeftBackWheel.setSpeed(0);
  RightFrontWheel.setSpeed(0);
  RightBackWheel.setSpeed(goSpeed);
}
void moveRightBackward() {
  LeftFrontWheel.setSpeed(0);
  LeftBackWheel.setSpeed(-goSpeed);
  RightFrontWheel.setSpeed(-goSpeed);
  RightBackWheel.setSpeed(0);
}
void moveLeftForward() {
  LeftFrontWheel.setSpeed(0);
  LeftBackWheel.setSpeed(goSpeed);
  RightFrontWheel.setSpeed(goSpeed);
  RightBackWheel.setSpeed(0);
}
void moveLeftBackward() {
  LeftFrontWheel.setSpeed(-goSpeed);
  LeftBackWheel.setSpeed(0);
  RightFrontWheel.setSpeed(0);
  RightBackWheel.setSpeed(-goSpeed);
}
void stopMoving() {
  LeftFrontWheel.setSpeed(0);
  LeftBackWheel.setSpeed(0);
  RightFrontWheel.setSpeed(0);
  RightBackWheel.setSpeed(0);
}

Serial Output: (I hit the reset which is why "Receiver ready!" appears twice)

Receiver ready!
got request from : 0x1: X = 3 Y = 3NONE
got request from : 0x1: X = 3 Y = 3NONE
got request from : 0x1: X = 3 Y = 3NONE
got request from : 0x1: X = 0 Y = 3LEFT
got request from : 0x1: X = 0 Y = 7DOWN-LEFT
got request from : 0x1: X = 6 Y = 0got request from : 0x1: X = 1 Y = 0got request from : 0x1: X = 3 Y = 3NONE
Receiver ready!
got request from : 0x1: X = 3 Y = 4got request from : 0x1: X = 3 Y = 7DOWN
got request from : 0x1: X = 3 Y = 6got request from : 0x1: X = 3 Y = 3NONE
got request from : 0x1: X = 3 Y = 3NONE
got request from : 0x1: X = 3 Y = 3NONE
got request from : 0x1: X = 3 Y = 3NONE
got request from : 0x1: X = 0 Y = 4got request from : 0x1: X = 0 Y = 3LEFT
got request from : 0x1: X = 3 Y = 3NONE

I would say it more or less functions as it should, however... I'm getting zero response from my steppers/drivers. No lights, no jitter, literally nothing. I'm sort of at a loss. I guess I'll just do some reading about troubleshooting these motors/drivers?

Did you strip everything out and just work getting the motors moving? I would first get the motors working locally with no radio in the mix. Then add the joystick locally and get the motors working . Basically removing the radio from the equation. Simplify and take one small step at a time.

I know it sounds like the long way to do it but its going to be way to solve the problems. Don't ask me how I know. : )

Edit: you can have a bunch of seperate projects going on each one concentrated on getting a piece of your main project working. Then combine them one by one for the Win.

2 Likes

I was thinking more about this last night and I kind of figured that would be the answer. It makes perfect sense, but it feels like taking 4 steps back. Hahahaa.

Okay, so I did just that. I grabbed a new Uno, new stepper motor and driver, and uploaded a simple AccelStepper.h example. Stepper worked fine. Great. Then I used that example to cross examine my code for any errors or inconsistencies. I found some and fixed them.

Now the drivers are clearly coming on, but the motors aren't moving at all. Comparing my code with the example, as far as motor control, the only difference if the fact that my control is initiated with switch statements and void functions, and I use a setAcceleration function. So I figured maybe the setspeed and runspeed functions were only being active for a split second, not giving the motors enough time to move, so I tried adding a delay at the end of the void functions and at the end of the switch statements, before the break. Neither made a difference. I also tried removing the setAcceleration function and that too made no difference.

I've also tried unplugging all steppers and drivers except one, no difference.

Receiver:

#include "RHReliableDatagram.h"
#include "RH_NRF24.h"
#include "SPI.h"
#include "AccelStepper.h"
#include "stdio.h"

// Define addresses for radio channels
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2

#define RF_CE_PIN 48
#define RF_CSN_PIN 49

RH_NRF24 nrf24(RF_CE_PIN, RF_CSN_PIN);
RHReliableDatagram rf_manager(nrf24, 2);

// Define a message to return if values received
uint8_t ReturnMessage[] = "JoyStick Data Received";

// Define the Message Buffer
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

// Motor pin definitions:
#define motorPinA1 22  // IN1 on the ULN2003 driver
#define motorPinA2 23  // IN2 on the ULN2003 driver
#define motorPinA3 24  // IN3 on the ULN2003 driver
#define motorPinA4 25  // IN4 on the ULN2003 driver
#define motorPinB1 26  // IN1 on the ULN2003 driver
#define motorPinB2 27  // IN2 on the ULN2003 driver
#define motorPinB3 28  // IN3 on the ULN2003 driver
#define motorPinB4 29  // IN4 on the ULN2003 driver
#define motorPinC1 30  // IN1 on the ULN2003 driver
#define motorPinC2 31  // IN2 on the ULN2003 driver
#define motorPinC3 32  // IN3 on the ULN2003 driver
#define motorPinC4 33  // IN4 on the ULN2003 driver
#define motorPinD1 34  // IN1 on the ULN2003 driver
#define motorPinD2 35  // IN2 on the ULN2003 driver
#define motorPinD3 36  // IN3 on the ULN2003 driver
#define motorPinD4 37  // IN4 on the ULN2003 driver

// Define the AccelStepper interface type; 4 wire motor in half step mode:
#define MotorInterfaceType 8

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper library with 28BYJ-48 stepper motor:
AccelStepper LeftFrontWheel = AccelStepper(MotorInterfaceType, motorPinA1, motorPinA3, motorPinA2, motorPinA4);
AccelStepper LeftBackWheel = AccelStepper(MotorInterfaceType, motorPinB1, motorPinB3, motorPinB2, motorPinB4);
AccelStepper RightFrontWheel = AccelStepper(MotorInterfaceType, motorPinC1, motorPinC3, motorPinC2, motorPinC4);
AccelStepper RightBackWheel = AccelStepper(MotorInterfaceType, motorPinD1, motorPinD3, motorPinD2, motorPinD4);


void setup() {
  // Setup Serial Monitor
  Serial.begin(9600);

  // Set the maximum steps per second:
  LeftFrontWheel.setMaxSpeed(1000);
  LeftBackWheel.setMaxSpeed(1000);
  RightFrontWheel.setMaxSpeed(1000);
  RightBackWheel.setMaxSpeed(1000);

  // Set the maximum acceleration in steps per second^2:
  //LeftFrontWheel.setAcceleration(200);
  //LeftBackWheel.setAcceleration(200);
  //RightFrontWheel.setAcceleration(200);
  //RightBackWheel.setAcceleration(200);

  if (!nrf24.init()) {
    Serial.println("Failed to initialize NRF24L01");
    while (1);
  }

  if (!nrf24.setChannel(1)) {
    Serial.println("Failed to set channel");
    while (1);
  }

  if (!rf_manager.init()) {
    Serial.println("Failed to initialize RFManager");
    while (1);
  }

  Serial.println("Receiver ready!");
}

void loop() {
  if (rf_manager.available()) {
    // Wait for a message addressed to us from the client
    uint8_t len = sizeof(buf);
    uint8_t from;
    if (rf_manager.recvfromAck(buf, &len, &from))
    //Serial Print the values of joystick
    {
      // Serial.print("got request from : 0x");
      // Serial.print(from, HEX);
      Serial.print(": Case = ");
      Serial.print(buf[0]);
      Serial.println();

      int powersCombined = buf[0];

      // Send a reply back to the originator client, check for error
      //  if (!rf_manager.sendtoWait(ReturnMessage, sizeof(ReturnMessage), from))
      //    Serial.println("sendtoWait failed");

      switch (powersCombined) {
        case 41:
        case 51:
        case 61:
        case 42:
        case 52:
        case 62:
        case 43:
        case 53:
        case 63:
          Serial.println("UP");
          moveForward();
          break;

        case 44:
        case 54:
        case 64:
        case 45:
        case 55:
        case 65:
        case 46:
        case 56:
        case 66:
          Serial.println("NONE");
          stopMoving();
          break;

        case 47:
        case 57:
        case 67:
        case 48:
        case 58:
        case 68:
        case 49:
        case 59:
        case 69:
          Serial.println("DOWN");
          moveBackward();
          break;

        case 14:
        case 24:
        case 34:
        case 15:
        case 25:
        case 35:
        case 16:
        case 26:
        case 36:
          Serial.println("LEFT");
          moveSidewaysLeft();
          break;

        case 74:
        case 84:
        case 94:
        case 75:
        case 85:
        case 95:
        case 76:
        case 86:
        case 96:
          Serial.println("RIGHT");
          moveSidewaysRight();
          break;

        case 11:
        case 21:
        case 31:
        case 12:
        case 22:
        case 32:
        case 13:
        case 23:
        case 33:
          Serial.println("UP-LEFT");
          moveLeftBackward();
          break;

        case 71:
        case 81:
        case 91:
        case 72:
        case 82:
        case 92:
        case 73:
        case 83:
        case 93:
          Serial.println("UP-RIGHT");
          moveRightForward();
          break;

        case 17:
        case 27:
        case 37:
        case 18:
        case 28:
        case 38:
        case 19:
        case 29:
        case 39:
          Serial.println("DOWN-LEFT");
          moveLeftBackward();
          break;

        case 77:
        case 87:
        case 97:
        case 78:
        case 88:
        case 98:
        case 79:
        case 89:
        case 99:
          Serial.println("DOWN-RIGHT");
          moveRightBackward();
          break;
      }
    }
  }
}
void moveForward() {
  LeftFrontWheel.setSpeed(500);
  LeftBackWheel.setSpeed(500);
  RightFrontWheel.setSpeed(500);
  RightBackWheel.setSpeed(500);
  LeftFrontWheel.runSpeed();
  LeftBackWheel.runSpeed();
  RightFrontWheel.runSpeed();
  RightBackWheel.runSpeed();
  delay(1000); 
}
void moveBackward() {
  LeftFrontWheel.setSpeed(-500);
  LeftBackWheel.setSpeed(-500);
  RightFrontWheel.setSpeed(-500);
  RightBackWheel.setSpeed(-500);
  LeftFrontWheel.runSpeed();
  LeftBackWheel.runSpeed();
  RightFrontWheel.runSpeed();
  RightBackWheel.runSpeed();
  delay(1000); 
}
void moveSidewaysRight() {
  LeftFrontWheel.setSpeed(500);
  LeftBackWheel.setSpeed(-500);
  RightFrontWheel.setSpeed(-500);
  RightBackWheel.setSpeed(500);
  LeftFrontWheel.runSpeed();
  LeftBackWheel.runSpeed();
  RightFrontWheel.runSpeed();
  RightBackWheel.runSpeed();
  delay(1000); 
}
void moveSidewaysLeft() {
  LeftFrontWheel.setSpeed(-500);
  LeftBackWheel.setSpeed(500);
  RightFrontWheel.setSpeed(500);
  RightBackWheel.setSpeed(-500);
  LeftFrontWheel.runSpeed();
  LeftBackWheel.runSpeed();
  RightFrontWheel.runSpeed();
  RightBackWheel.runSpeed();
  delay(1000); 
}
void rotateLeft() {
  LeftFrontWheel.setSpeed(-500);
  LeftBackWheel.setSpeed(-500);
  RightFrontWheel.setSpeed(500);
  RightBackWheel.setSpeed(500);
  LeftFrontWheel.runSpeed();
  LeftBackWheel.runSpeed();
  RightFrontWheel.runSpeed();
  RightBackWheel.runSpeed();
  delay(1000); 
}
void rotateRight() {
  LeftFrontWheel.setSpeed(500);
  LeftBackWheel.setSpeed(500);
  RightFrontWheel.setSpeed(-500);
  RightBackWheel.setSpeed(-500);
  LeftFrontWheel.runSpeed();
  LeftBackWheel.runSpeed();
  RightFrontWheel.runSpeed();
  RightBackWheel.runSpeed();
  delay(1000); 
}
void moveRightForward() {
  LeftFrontWheel.setSpeed(500);
  LeftBackWheel.setSpeed(0);
  RightFrontWheel.setSpeed(0);
  RightBackWheel.setSpeed(500);
  LeftFrontWheel.runSpeed();
  LeftBackWheel.runSpeed();
  RightFrontWheel.runSpeed();
  RightBackWheel.runSpeed();
  delay(1000); 
}
void moveRightBackward() {
  LeftFrontWheel.setSpeed(0);
  LeftBackWheel.setSpeed(-500);
  RightFrontWheel.setSpeed(-500);
  RightBackWheel.setSpeed(0);
  LeftFrontWheel.runSpeed();
  LeftBackWheel.runSpeed();
  RightFrontWheel.runSpeed();
  RightBackWheel.runSpeed();
  delay(1000); 
}
void moveLeftForward() {
  LeftFrontWheel.setSpeed(0);
  LeftBackWheel.setSpeed(500);
  RightFrontWheel.setSpeed(500);
  RightBackWheel.setSpeed(0);
  LeftFrontWheel.runSpeed();
  LeftBackWheel.runSpeed();
  RightFrontWheel.runSpeed();
  RightBackWheel.runSpeed();
  delay(1000); 
}
void moveLeftBackward() {
  LeftFrontWheel.setSpeed(-500);
  LeftBackWheel.setSpeed(0);
  RightFrontWheel.setSpeed(0);
  RightBackWheel.setSpeed(-500);
  LeftFrontWheel.runSpeed();
  LeftBackWheel.runSpeed();
  RightFrontWheel.runSpeed();
  RightBackWheel.runSpeed();
  delay(1000); 
}
void stopMoving() {
  LeftFrontWheel.setSpeed(0);
  LeftBackWheel.setSpeed(0);
  RightFrontWheel.setSpeed(0);
  RightBackWheel.setSpeed(0);
  LeftFrontWheel.runSpeed();
  LeftBackWheel.runSpeed();
  RightFrontWheel.runSpeed();
  RightBackWheel.runSpeed();
  delay(1000); 
}

Transmitter:

#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
#include <SPI.h>
#include "stdio.h"

// Define addresses for radio channels
#define CLIENT_ADDRESS 1   
#define SERVER_ADDRESS 2
 
// Create an instance of the radio driver
RH_NRF24 RadioDriver;
 
// Sets the radio driver to NRF24 and the client address to 1
RHReliableDatagram RadioManager(RadioDriver, CLIENT_ADDRESS);
 
#define xpin A0  // Arduino pin connected to VRX pin
#define ypin A1  // Arduino pin connected to VRY pin
 
// Define the Message Buffer
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

// Declare unsigned 8-bit joystick array
uint8_t joystick[3]; 

int xValue = 0;  // To store value of the X axis
int yValue = 0;  // To store value of the Y axis
int previousXValue = 0; // Set initial previous values to 0
int previousYValue = 0;

void setup() {

  Serial.begin(9600);  // Start serial communication

  // Initialize RadioManager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!RadioManager.init())
    Serial.println("init failed");

  else Serial.println("Setup complete.");
}

void loop() {

  // read analog X and Y analog values
  xValue = analogRead(xpin);
  yValue = analogRead(ypin);

  // remap the x and y values to be 0-7
  int xMap = (xValue / 100);
  int yMap = (yValue / 100);

  int xxMap = map(xMap, 0, 10, 1, 9);
  int yyMap = map(yMap, 0, 10, 1, 9);

  // concatenate the x and y values into a single variable
  String powersCombinedStr = String(xxMap) + String(yyMap);
  int powersCombined = powersCombinedStr.toInt();

  joystick[0] = (powersCombined);  

  Serial.print("xMap = ");
  Serial.print(xMap);
  Serial.print(" | yMap = ");
  Serial.print(yMap);
    Serial.print(" | xxMap = ");
  Serial.print(xxMap);
  Serial.print(" | yyMap = ");
  Serial.print(yyMap);
  Serial.print(" | xValue = ");
  Serial.print(xValue);
  Serial.print(" | yValue = ");
  Serial.print(yValue);
  Serial.print(" | Case#: ");
  Serial.println(powersCombined);

  if (xValue != previousXValue || yValue != previousYValue) { // Check if values have changed

    // Update previous values
    previousXValue = xValue;
    previousYValue = yValue;

    //Send a message containing Joystick data to manager_server
  if (RadioManager.sendtoWait(joystick, sizeof(joystick), SERVER_ADDRESS))
  {
    // Now wait for a reply from the server
    uint8_t len = sizeof(buf);
    uint8_t from;
    if (RadioManager.recvfromAckTimeout(buf, &len, 2000, &from))
    {
      Serial.print("got reply from : 0x");
      Serial.print(from, HEX);
      Serial.print(": ");
      Serial.println((char*)buf);
       }
  //   else
  //   {
  //     Serial.println("No reply, is nrf24_reliable_datagram_server running?");
  //   }
  // }
  // else
  //   Serial.println("sendtoWait failed");
 
  delay(100);  // Wait a bit before next transmission
}
}
}




The reason for the extra "xxMap" and "yyMap" is because I was experimenting with different ways to handle my axis data, I just never removed them once I was done.

It should hopefully be a pointless test, but I'd take some working code from the library examples and add it to setup (or replace your loop code entirely) in the receiver to prove that you can move the motor

1 Like

I was trying my damnedest to not have to do that. Luckily, it paid off! YAY!

So I figured out the problem.

When I set up an Uno with only the driver and motor with example code, it runs perfect. So like I said earlier I compared the example code with mine as far as motor controls. That was my mistake, in combination with another discovery I made.
The example code is incredibly short and basic, as you would expect, but what I didn't think about was how fast that code runs compared to something fluffier.
I decided I would tinker with the example code and see what I can figure out, so I added:

global:
int counter = 0

loop:
counter++;
Serial.println(counter);

That way I could actually get a sense of how fast it's running. Wowzers fast. I also tried out different baud rates to see how much of a difference their was. Then I was going to add the same thing on my receiver code, and that's when I noticed a huge difference between the two setups.

When the stepper motor is running well, all four LEDs on the driver are constantly lit up. Or at least they're lighting up so fast in sequence that it looks like they're constantly lit. But the driver on my car has 1 or 2 lights on at a time that's in this sequence.

1
1 2
  2
  2 3
    3
    3 4
      4
1     4

Which is funny because as I was trying to read up on the issue, and I kept finding people with drivers that didn't go in the correct order, so they had to keep trying different combinations until the motor starting running correctly. Meanwhile I'm over here staring at this sequence that takes like 8 seconds to complete thinking, "How is this difficult to figure out?". lmao. (I was holding the joystick up)

So I guess my code is running exactly as it should, for how it's written. I'm pretty sure each time it calls to move in a direction, it's going one single step. So it might be best that I use setCurrentPosition() and runToPosition(), and make it go like 2 full rotations? If it's in the middle of moving, and another transmission is received, but for a different dirrection, I would imagine it would take the most recent call, regardless of what's happening. Right?

We'll see I guess lol. I just need to adjust my movement functions.

I just cannot get these things to move faster.

When I pick this back up tomorrow, I'm gonna try cutting my code down as much as possible, and maybe change to something instead of the switch statements. Great learning experience, but I'm just not sure it's the best thing for using the accelstepper library, but I'm sure I'm not fully grasping the full use of that library yet. Seems finicky as all hell.

IT MOVES!!!

I made a lot of changes, and cut out as much as I could.

Transmitter:

#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
#include <SPI.h>
#include "stdio.h"

// Define addresses for radio channels
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2

// Create an instance of the radio driver
RH_NRF24 RadioDriver;

// Sets the radio driver to NRF24 and the client address to 1
RHReliableDatagram RadioManager(RadioDriver, CLIENT_ADDRESS);

#define xpin A0  // Arduino pin connected to VRX pin
#define ypin A1  // Arduino pin connected to VRY pin

// Define the Message Buffer
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

// Declare unsigned 8-bit joystick array
uint8_t joystick[3];

int xValue = 0;          // To store value of the X axis
int yValue = 0;          // To store value of the Y axis
int previousXValue = 0;  // Set initial previous values to 0
int previousYValue = 0;


void setup() {

  Serial.begin(115200);  // Start serial communication

  // Initialize RadioManager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!RadioManager.init())
    Serial.println("init failed");

  else Serial.println("Setup complete.");
}

void loop() {

  // read analog X and Y analog values
  xValue = analogRead(xpin);
  yValue = analogRead(ypin);
  // Divide the values by 100 to drop some zeros, then remap the values to 1-9
  int xMap = map(xValue / 100, 0, 10, 1, 9);
  int yMap = map(yValue / 100, 0, 10, 1, 9);
  // X and Y value buffers
  joystick[0] = (xMap);
  joystick[1] = (yMap);

  Serial.print(" | xValue = ");
  Serial.print(xValue);
  Serial.print(" | yValue = ");
  Serial.print(yValue);
  Serial.print(" | buffer0: ");
  Serial.print(joystick[0]);
  Serial.print(" | buffer1: ");
  Serial.println(joystick[1]);

  if (xValue != previousXValue || yValue != previousYValue) {  // Check if values have changed

    // Update previous values
    previousXValue = xValue;
    previousYValue = yValue;

    //Send a message containing Joystick data to manager_server
    (RadioManager.sendto(joystick, sizeof(joystick), SERVER_ADDRESS));
  }
}

Receiver:

#include "RHReliableDatagram.h"
#include "RH_NRF24.h"
#include "SPI.h"
#include "AccelStepper.h"
#include "stdio.h"

// Define addresses for radio channels
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2

#define RF_CE_PIN 48
#define RF_CSN_PIN 49

RH_NRF24 nrf24(RF_CE_PIN, RF_CSN_PIN);
RHReliableDatagram rf_manager(nrf24, 2);

// Define a message to return if values received
uint8_t ReturnMessage[] = "JoyStick Data Received";

// Define the Message Buffer
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

// Motor pin definitions:
#define motorPinA1 22  // IN1 on the ULN2003 driver
#define motorPinA2 23  // IN2 on the ULN2003 driver
#define motorPinA3 24  // IN3 on the ULN2003 driver
#define motorPinA4 25  // IN4 on the ULN2003 driver
#define motorPinB1 26  // IN1 on the ULN2003 driver
#define motorPinB2 27  // IN2 on the ULN2003 driver
#define motorPinB3 28  // IN3 on the ULN2003 driver
#define motorPinB4 29  // IN4 on the ULN2003 driver
#define motorPinC1 30  // IN1 on the ULN2003 driver
#define motorPinC2 31  // IN2 on the ULN2003 driver
#define motorPinC3 32  // IN3 on the ULN2003 driver
#define motorPinC4 33  // IN4 on the ULN2003 driver
#define motorPinD1 34  // IN1 on the ULN2003 driver
#define motorPinD2 35  // IN2 on the ULN2003 driver
#define motorPinD3 36  // IN3 on the ULN2003 driver
#define motorPinD4 37  // IN4 on the ULN2003 driver

// Define the AccelStepper interface type; 4 wire motor in half step mode:
#define MotorInterfaceType 8

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper library with 28BYJ-48 stepper motor:
AccelStepper LeftFrontWheel = AccelStepper(MotorInterfaceType, motorPinA1, motorPinA3, motorPinA2, motorPinA4);
AccelStepper LeftBackWheel = AccelStepper(MotorInterfaceType, motorPinB1, motorPinB3, motorPinB2, motorPinB4);
AccelStepper RightFrontWheel = AccelStepper(MotorInterfaceType, motorPinC1, motorPinC3, motorPinC2, motorPinC4);
AccelStepper RightBackWheel = AccelStepper(MotorInterfaceType, motorPinD1, motorPinD3, motorPinD2, motorPinD4);

int yVal = (5);
int xVal = (5);

void setup() {
  // Setup Serial Monitor
  Serial.begin(115200);

  // Set the maximum steps per second:
  LeftFrontWheel.setMaxSpeed(1000);
  LeftBackWheel.setMaxSpeed(1000);
  RightFrontWheel.setMaxSpeed(1000);
  RightBackWheel.setMaxSpeed(1000);

  // Set the maximum acceleration in steps per second^2:
  LeftFrontWheel.setAcceleration(200);
  LeftBackWheel.setAcceleration(200);
  RightFrontWheel.setAcceleration(200);
  RightBackWheel.setAcceleration(200);

  if (!nrf24.init()) {
    Serial.println("Failed to initialize NRF24L01");
    while (1)
      ;
  }

  if (!nrf24.setChannel(1)) {
    Serial.println("Failed to set channel");
    while (1)
      ;
  }

  if (!rf_manager.init()) {
    Serial.println("Failed to initialize RFManager");
    while (1)
      ;
  }

  Serial.println("Receiver ready!");
}

void loop() {

    // Wait for a message addressed to us from the client
    uint8_t len = sizeof(buf);
    uint8_t from;
    (rf_manager.recvfromAck(buf, &len, &from));
    //Serial Print the values of joystick
    // Serial.print("got request from : 0x");
    // Serial.print(from, HEX);
    Serial.print("X = ");
    Serial.print(buf[0]);
    Serial.print(" | Y = ");
    Serial.println(buf[1]);
    int xVal = buf[0];
    int yVal = buf[1];

      // Send a reply back to the originator client, check for error
      //  if (!rf_manager.sendtoWait(ReturnMessage, sizeof(ReturnMessage), from))
      //    Serial.println("sendtoWait failed");

    if (yVal > 6) {    // y axis down
      if (xVal < 4) {  // x axis left
        LeftFrontWheel.setSpeed(-500);
        LeftBackWheel.setSpeed(0);
        RightFrontWheel.setSpeed(0);
        RightBackWheel.setSpeed(-500);
        LeftFrontWheel.runSpeed();
        LeftBackWheel.runSpeed();
        RightFrontWheel.runSpeed();
        RightBackWheel.runSpeed();
        Serial.println("Left Backward");
      } else if (xVal > 6) {  // x axis right
        LeftFrontWheel.setSpeed(0);
        LeftBackWheel.setSpeed(-500);
        RightFrontWheel.setSpeed(-500);
        RightBackWheel.setSpeed(0);
        LeftFrontWheel.runSpeed();
        LeftBackWheel.runSpeed();
        RightFrontWheel.runSpeed();
        RightBackWheel.runSpeed();
        Serial.println("Right Backward");
      } else if (xVal == 5) {  // x axis center
        LeftFrontWheel.setSpeed(-500);
        LeftBackWheel.setSpeed(-500);
        RightFrontWheel.setSpeed(-500);
        RightBackWheel.setSpeed(-500);
        LeftFrontWheel.runSpeed();
        LeftBackWheel.runSpeed();
        RightFrontWheel.runSpeed();
        RightBackWheel.runSpeed();
        Serial.println("Backward");
      }
    }

    else if (yVal < 4) {    // y axis up
      if (xVal < 4) {  // x axis left
        LeftFrontWheel.setSpeed(0);
        LeftBackWheel.setSpeed(500);
        RightFrontWheel.setSpeed(500);
        RightBackWheel.setSpeed(0);
        LeftFrontWheel.runSpeed();
        LeftBackWheel.runSpeed();
        RightFrontWheel.runSpeed();
        RightBackWheel.runSpeed();
        Serial.println("Left Forward");
      } else if (xVal > 6) {  // x axis right
        LeftFrontWheel.setSpeed(500);
        LeftBackWheel.setSpeed(0);
        RightFrontWheel.setSpeed(0);
        RightBackWheel.setSpeed(500);
        LeftFrontWheel.runSpeed();
        LeftBackWheel.runSpeed();
        RightFrontWheel.runSpeed();
        RightBackWheel.runSpeed();
        Serial.println("Right Forward");
      } else if (xVal == 5) {  // x axis center
        LeftFrontWheel.setSpeed(500);
        LeftBackWheel.setSpeed(500);
        RightFrontWheel.setSpeed(500);
        RightBackWheel.setSpeed(500);
        LeftFrontWheel.runSpeed();
        LeftBackWheel.runSpeed();
        RightFrontWheel.runSpeed();
        RightBackWheel.runSpeed();
        Serial.println("Forward");
      }
    }

    else if (yVal == 5) {   // y axis center
      if (xVal < 4) {  // x axis left
        LeftFrontWheel.setSpeed(-500);
        LeftBackWheel.setSpeed(500);
        RightFrontWheel.setSpeed(500);
        RightBackWheel.setSpeed(-500);
        LeftFrontWheel.runSpeed();
        LeftBackWheel.runSpeed();
        RightFrontWheel.runSpeed();
        RightBackWheel.runSpeed();
        Serial.println("Sideways Left");
      } else if (xVal > 6) {  // x axis right
        LeftFrontWheel.setSpeed(500);
        LeftBackWheel.setSpeed(-500);
        RightFrontWheel.setSpeed(-500);
        RightBackWheel.setSpeed(500);
        LeftFrontWheel.runSpeed();
        LeftBackWheel.runSpeed();
        RightFrontWheel.runSpeed();
        RightBackWheel.runSpeed();
        Serial.println("Sideways Right");
      } else if (xVal == 5) {  // x axis center
        LeftFrontWheel.setSpeed(0);
        LeftBackWheel.setSpeed(0);
        RightFrontWheel.setSpeed(0);
        RightBackWheel.setSpeed(0);
        LeftFrontWheel.runSpeed();
        LeftBackWheel.runSpeed();
        RightFrontWheel.runSpeed();
        RightBackWheel.runSpeed();
        Serial.println("STOP");
        }
      }
    }

This project is FAR from over, but I think that's the end of this thread. I really want to thank those that stopped by and offered their words of wisdom, I appreciate you. Especially you noweare! You have been here guiding me along this whole time, and I can't thank you enough!

She's not very fast, but she does move! It still has a few kinks to work out before I really continue onto the next step, but that shouldn't be an issue.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.