Making my stepper wireless with NRF24L01?

Hi!

I got a project where I´m using a rotary encoder to turn my stepper motor, also as a switch with IN and OUT positions to go back and forth. I also got som LEDs for speed and marking of IN an OUT points.

I got the project running on a arduino UNO but now I want to make it wireless with NRF24L01.

My question is, how can I “split” the code to Transmitter and Reciver and how do you do it the easy way, assuming there is one?

OR: Should I use an other module for this purpose?
AND: Can I use the Arduino Micro for the Reciver?

Thanks for your help!

This is what I got:

#include <Stepper.h>

#include <FastLED.h>

// silent step stick driver connection
#define dir_pin 8
#define step_pin 9
#define MS1 10
#define MS2 11
#define SLEEP 12

//Adafruit RGB LED connection
#define led_pin 5

// IN & OUT switches connection
#define SWITCH_PIN_IN A0
#define SWITCH_PIN_OUT A1

// LED for switches connection
#define BUT_LED_PIN 3
#define BUT_LED2_PIN 5

// Rotary encoder module connections
const int PinCLK=2;
const int PinSW=4;
const int PinDT=6;

#define NUM_LEDS 8 //NUM of LEDs on stick
CRGB leds[NUM_LEDS]; //FastLED library unit

volatile boolean TurnDetected;  // to detect rotation of Rotary Encoder
volatile boolean rotationdirection;  // CW or CCW rotation
volatile boolean rswitch=0;  // Variable to store rotary encoder switch state
volatile boolean led_speed_change; // to detect change of speed
volatile int StepsToTake=2;      // Controls the speed of the Stepper per Rotary click

int direction;   // Variable to set Rotation (CW-CCW) of stepper
int StepperPosition=0;    // To store Stepper Motor Position
int IN_Position=0;  // To store IN point Position
int OUT_Position=0;  // To store OUT point Position

int Time;

void rotarydetect ()  {
   delay(10);

   if (rswitch == 1) {
    if (digitalRead(PinCLK)) {
      if (digitalRead(PinDT)) {
        if (StepsToTake > 2) {
          StepsToTake=StepsToTake-1; }}
          if (!digitalRead(PinDT)) {
            if (StepsToTake < 9) {
              StepsToTake=StepsToTake+1; }}
              led_speed_change = true;
    }
   }
   else {
    if (digitalRead(PinCLK))
    rotationdirection= digitalRead(PinDT);
    else 
    rotationdirection= digitalRead(PinDT);
    TurnDetected = true;
   }
}

void setup ()  {  

  pinMode(BUT_LED_PIN, OUTPUT);
  pinMode(BUT_LED2_PIN, OUTPUT);
  digitalWrite(BUT_LED_PIN,LOW);
  digitalWrite(BUT_LED2_PIN,LOW);
  pinMode(SWITCH_PIN_IN, INPUT);
  pinMode(SWITCH_PIN_OUT, INPUT);
  digitalWrite(SWITCH_PIN_IN,HIGH);
  digitalWrite(SWITCH_PIN_OUT,HIGH);

  FastLED.addLeds<NEOPIXEL,led_pin>(leds, NUM_LEDS); // setup FastLED library
  FastLED.clear();

  for(int x = 0; x != (StepsToTake - 1); x++) {
    if (x < 2) leds[x] = CRGB::Red;
    if (x > 1 & x < 5) leds[x] = CRGB::Orange;
    if (x > 4) leds[x] = CRGB::Green; }

    FastLED.setBrightness(50);
    FastLED.show();

    pinMode(MS1, OUTPUT);
    pinMode(MS2, OUTPUT);
    pinMode(dir_pin, OUTPUT);
    pinMode(step_pin, OUTPUT);
    pinMode(SLEEP, OUTPUT);
    digitalWrite(SLEEP, HIGH); // wake up silent step stick driver
    delay(5);


    digitalWrite(MS1, LOW);
    digitalWrite(MS2, LOW);

    pinMode(PinCLK,INPUT);
    pinMode(PinDT,INPUT);
    pinMode(PinSW,INPUT);
    digitalWrite(PinSW,INPUT_PULLUP);
    attachInterrupt (0,rotarydetect,FALLING);
}


void loop ()  {

  if (digitalRead(SWITCH_PIN_IN) == LOW) // Do if IN switch is pressed
  {
    Time = millis();
    delay(500);//debounce
 
   // check if the switch is pressed for longer than 1 second.
    if(digitalRead(SWITCH_PIN_IN) == LOW && Time - millis() >5000)

    {
      digitalWrite(BUT_LED_PIN,HIGH);
      delay(10000);
      digitalWrite(BUT_LED_PIN,LOW);
      IN_Position=StepperPosition;

    }
    else
    if (StepperPosition == IN_Position) {
    } else { 
      if (StepperPosition > IN_Position) {
        while (StepperPosition > IN_Position) {
          digitalWrite(dir_pin, HIGH);
          for (int x = 1; x < StepsToTake; x++) {
            digitalWrite(step_pin, HIGH);
            delay(1);
            digitalWrite(step_pin, LOW);
            delay(1);
          }
          StepperPosition=StepperPosition-StepsToTake;
          delay(100-(StepsToTake*11));
        }
      }
      else { 
        while (StepperPosition < IN_Position) {
          digitalWrite(dir_pin, LOW);
          for (int x = 1; x < StepsToTake; x++) {
            digitalWrite(step_pin, HIGH);
            delay(1);
            digitalWrite(step_pin, LOW);
            delay(1);
          }
          StepperPosition=StepperPosition+StepsToTake;
          delay(100-(StepsToTake*11));
        }
      }
    }                                         
    
    if (digitalRead(SWITCH_PIN_OUT) == LOW) // Do if IN switch is pressed
    {
      Time = millis();
      delay(500);
      
        // check if the switch is pressed for longer than 1 second.
      if(digitalRead(SWITCH_PIN_OUT) == LOW && Time - millis() >5000)

      {
        digitalWrite(BUT_LED2_PIN,HIGH);
        delay(10000);
        digitalWrite(BUT_LED2_PIN,LOW);
        OUT_Position=StepperPosition;
      }
      else
      if (StepperPosition == OUT_Position) {
      } else {
        if (StepperPosition > OUT_Position) {
          while (StepperPosition > OUT_Position) {
            digitalWrite(dir_pin, HIGH); 
            for (int x = 1; x < StepsToTake; x++) {
              digitalWrite(step_pin, HIGH);
              delay(1);
              digitalWrite(step_pin, LOW);
              delay(1);
            }
          }
        }
          else {
            while (StepperPosition < OUT_Position) {
              digitalWrite(dir_pin, LOW);
              for (int x = 1; x < StepsToTake; x++) {
                digitalWrite(step_pin, HIGH);
                delay(1);
                digitalWrite(step_pin, LOW);
                delay(1);
              }
              StepperPosition=StepperPosition+StepsToTake;
              delay(100-(StepsToTake*11));
            }
          }
        }
      }

      if (!(digitalRead(PinSW))) {
        delay(250);
        if (rswitch == 0) {
          rswitch = 1; }
          else {
            rswitch = 0; }
      }

      if (led_speed_change) {
        led_speed_change = false;

        FastLED.clear();
        for(int x = 0; x != (StepsToTake - 1); x++) {
          if (x < 2) leds[x] = CRGB::Red;
          if (x > 1 & x < 5) leds[x] = CRGB::Orange;
          if (x > 4) leds[x] = CRGB::Green; }
          FastLED.setBrightness(50);
          FastLED.show();
      }

      if (TurnDetected) {
        TurnDetected = false;

        if (rotationdirection) {
          digitalWrite(dir_pin, HIGH);
          for (int x = 1; x < StepsToTake; x++) {
            digitalWrite(step_pin, HIGH);
            delay(1);
            digitalWrite(step_pin, LOW);
            delay(1);
          }
          StepperPosition=StepperPosition-StepsToTake;
        }

        if (!rotationdirection) {
          digitalWrite(dir_pin, LOW);
          for (int x = 1; x < StepsToTake; x++) {
            digitalWrite(step_pin, HIGH);
            delay(1);
            digitalWrite(step_pin, LOW);
            delay(1);
          }
          StepperPosition=StepperPosition+StepsToTake;      
          }
      }
  }
}

Have a look at this Simple nRF24L01+ Tutorial

You need to separate your data collection code (reading the encoder and the switches) from the motor action code. I suggest you start by making a modified version of your existing program (without the wireless) first. The only link between the two parts of the code should be the variables into which the data is stored.

When you have your program organized like that it will be reasonably straightforward to make the wireless version with the data collection on one Arduino and the motor control on the other. And the wireless link will send the data from one to the other.

IMHO any other approach to the problem will just result in an un-maintainable mess.

...R