how to modify the code with HC-12 module

Hi guys,

I am doing a project and I wanna control the stepper motor using a joystick remotely. I already have code to control the stepper motor with joystick( Not remotely). I have HC-12 two modules. I am planing to use one as a transmitter which include the Arduino Nano and the Joystick. For the receiver, I am using Arduino Uno Board, easy driver and stepper motor. I will put the code below for the stepper motor and joystick, and I would like to modify the code which I can control the stepper motor remotely.

Here is the code;(This is the code without using HC-12 Module)

#define step_pin 3 // Pin 3 connected to Steps pin on EasyDriver
#define dir_pin 2 // Pin 2 connected to Direction pin
#define MS1 5 // Pin 5 connected to MS1 pin
#define MS2 4 // Pin 4 connected to MS2 pin
#define SLEEP 7 // Pin 7 connected to SLEEP pin
#define X_pin A0 // Pin A0 connected to joystick x axis

int direction; // Variable to set Rotation (CW-CCW) of the motor
int steps = 1025; // Assumes the belt clip is in the Middle

void setup() {
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
pinMode(dir_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
pinMode(SLEEP, OUTPUT);

digitalWrite(SLEEP, HIGH); // Wake up EasyDriver
delay(5); // Wait for EasyDriver wake up

/* Configure type of Steps on EasyDriver:
// MS1 MS2
//
// LOW LOW = Full Step //
// HIGH LOW = Half Step //
// LOW HIGH = A quarter of Step //
// HIGH HIGH = An eighth of Step //
*/

digitalWrite(MS1, LOW); // Configures to Full Steps
digitalWrite(MS2, LOW); // Configures to Full Steps

}

void loop() {
while (analogRead(X_pin) >= 0 && analogRead(X_pin) <= 100) {
if (steps > 0) {
digitalWrite(dir_pin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps--;
}
}

while (analogRead(X_pin) > 100 && analogRead(X_pin) <= 400) {
if (steps < 512) {
digitalWrite(dir_pin, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps++;
}
if (steps > 512) {
digitalWrite(dir_pin, HIGH);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps--;
}
}

while (analogRead(X_pin) > 401 && analogRead(X_pin) <= 600) {
if (steps < 1025) {
digitalWrite(dir_pin, LOW);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps++;
}
if (steps > 1025) {
digitalWrite(dir_pin, HIGH);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps--;
}
}

while (analogRead(X_pin) > 601 && analogRead(X_pin) <= 900) {
if (steps < 1535) {
digitalWrite(dir_pin, LOW);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps++;
}
if (steps > 1535) {
digitalWrite(dir_pin, HIGH);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps--;
}
}

while (analogRead(X_pin) > 900 && analogRead(X_pin) <= 1024) {
if (steps < 2050) {
digitalWrite(dir_pin, LOW);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps++;
}
}
}

Here is the link for the site, you can get a better idea looking the diagram
https://www.brainy-bits.com/stepper-motor-easy-driver/

Could anyone pls modify the code as I need? I would really appropriate if someone help me.

Thanks Guys!

This is not a free code writing service.

But if you want to make an attempt at writing some transmitting and receiving code we will happily help you to get it working.

Steve

This is what I have done for the coding but not working

Transmitter

int xAxis, yAxis;
void setup() {
Serial.begin(9600); // Default communication rate of the Bluetooth module
}
void loop() {
xAxis = analogRead(A0); // Read Joysticks X-axis
yAxis = analogRead(A1); // Read Joysticks Y-axis

// Send the values via the serial port to the slave HC-05 Bluetooth device
Serial.write(xAxis/4); // Dividing by 4 for converting from 0 - 1023 to 0 - 256, (1 byte) range
Serial.write(yAxis/4);
delay(20);
}

The Receiver;

Serial.begin(9600); // Default communication rate of the Bluetooth module
}
void loop() {
// Default value - no movement when the Joystick stays in the center
xAxis = 510;
yAxis = 510;
// Read the incoming data from the
while (Serial.available() == 0) {}
x = Serial.read();
delay(10);
y = Serial.read();
delay(10);
// Convert back the 0 - 255 range to 0 - 1023, suitable for motor control code below
xAxis = x * 4;
yAxis = y * 4;
/* Configure type of Steps on EasyDriver:
// MS1 MS2
//
// LOW LOW = Full Step //
// HIGH LOW = Half Step //
// LOW HIGH = A quarter of Step //
// HIGH HIGH = An eighth of Step //
*/

digitalWrite(MS1, LOW); // Configures to Full Steps
digitalWrite(MS2, LOW); // Configures to Full Steps

}

void loop() {
while (analogRead(X_pin) >= 0 && analogRead(X_pin) <= 100) {
if (steps > 0) {
digitalWrite(dir_pin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps--;
}
}

while (analogRead(X_pin) > 100 && analogRead(X_pin) <= 400) {
if (steps < 512) {
digitalWrite(dir_pin, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps++;
}
if (steps > 512) {
digitalWrite(dir_pin, HIGH);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps--;
}
}

while (analogRead(X_pin) > 401 && analogRead(X_pin) <= 600) {
if (steps < 1025) {
digitalWrite(dir_pin, LOW);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps++;
}
if (steps > 1025) {
digitalWrite(dir_pin, HIGH);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps--;
}
}

while (analogRead(X_pin) > 601 && analogRead(X_pin) <= 900) {
if (steps < 1535) {
digitalWrite(dir_pin, LOW);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps++;
}
if (steps > 1535) {
digitalWrite(dir_pin, HIGH);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps--;
}
}

while (analogRead(X_pin) > 900 && analogRead(X_pin) <= 1024) {
if (steps < 2050) {
digitalWrite(dir_pin, LOW);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps++;
}
}
}

This is what I have done, But not Working. If somebody can fix the code, I will really appreciate it

The receiver code does not compile. You can start fixing that by putting the variable definitions back in, adding a setup() and not having two loop()s.

Repost it (using </> code tags as described in "How to use this forum - please read at the top of the forum) when you have a version that at least compiles or if you can't get it to compile also post the errors you're getting,

Just saying "not working" is useless. You need to describe what it does and what it doesn't do. But in this case it doesn't matter because code that won't compile never does anything anyway.

Steve

 while (Serial.available() == 0) {}
 x = Serial.read();
 delay(10);
 y = Serial.read();
 delay(10);

If there is no serial data, stuff your head in the sand until there is at least one byte. OK fine. There ARE better ways.

But, once there is AT LEAST ONE BYTE, it is NOT OK to read two bytes.

The 10 millisecond delay does NOT assure the arrival of the second byte.

Robin2 put a lot of work into writing this thread. You should put an equivalent amount of work into reading and understanding it:
http://forum.arduino.cc/index.php?topic=396450.0

I fixed the code

Transmitter

int xAxis, yAxis;
void setup() {
Serial.begin(9600); // Default communication rate of the Bluetooth module
}
void loop() {
xAxis = analogRead(A0); // Read Joysticks X-axis
yAxis = analogRead(A1); // Read Joysticks Y-axis

// Send the values via the serial port to the slave HC-05 Bluetooth device
Serial.write(xAxis/4); // Dividing by 4 for converting from 0 - 1023 to 0 - 256, (1 byte) range
Serial.write(yAxis/4);
delay(20);
}

Receiver

#define step_pin 3 // Pin 3 connected to Steps pin on EasyDriver
#define dir_pin 2 // Pin 2 connected to Direction pin
#define MS1 5 // Pin 5 connected to MS1 pin
#define MS2 4 // Pin 4 connected to MS2 pin
#define SLEEP 7 // Pin 7 connected to SLEEP pin

int direction; // Variable to set Rotation (CW-CCW) of the motor
int steps = 1025; // Assumes the belt clip is in the Middle
int xAxis, yAxis;
int x = 0;
int y = 0;
void setup() {
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
pinMode(dir_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
pinMode(SLEEP, OUTPUT);

digitalWrite(SLEEP, HIGH); // Wake up EasyDriver
delay(5); // Wait for EasyDriver wake up

Serial.begin(9600); // Default communication rate of the Bluetooth module

/* Configure type of Steps on EasyDriver:
// MS1 MS2
//
// LOW LOW = Full Step //
// HIGH LOW = Half Step //
// LOW HIGH = A quarter of Step //
// HIGH HIGH = An eighth of Step //
*/

digitalWrite(MS1, LOW); // Configures to Full Steps
digitalWrite(MS2, LOW); // Configures to Full Steps

}

void loop() {
// Default value - no movement when the Joystick stays in the center
xAxis = 510;
yAxis = 510;
// Read the incoming data from the
while (Serial.available() == 0) {}
x = Serial.read();
delay(10);
y = Serial.read();
delay(10);
// Convert back the 0 - 255 range to 0 - 1023, suitable for motor control code below

xAxis = x * 4;
yAxis = y * 4;

while (xAxis >= 0 && xAxis <= 100) {
if (steps > 0) {
digitalWrite(dir_pin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps--;
}
}

while (xAxis > 100 && xAxis <= 400) {
if (steps < 512) {
digitalWrite(dir_pin, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps++;
}
if (steps > 512) {
digitalWrite(dir_pin, HIGH);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps--;
}
}

while (xAxis > 401 && xAxis <= 600) {
if (steps < 1025) {
digitalWrite(dir_pin, LOW);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps++;
}
if (steps > 1025) {
digitalWrite(dir_pin, HIGH);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps--;
}
}

while (xAxis > 601 && xAxis <= 900) {
if (steps < 1535) {
digitalWrite(dir_pin, LOW);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps++;
}
if (steps > 1535) {
digitalWrite(dir_pin, HIGH);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps--;
}
}

while (xAxis > 900 && xAxis <= 1024) {
if (steps < 2050) {
digitalWrite(dir_pin, LOW);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps++;
}
}
}

Right now there is no error in the both codes. everything is fine and both codes have successfully uploaded the Arduino nano and uno. But the thing is there is no sign from Stepper motor. not moving. Could anybody pls let me know what wrong?

thank you

  // Read the incoming data from the
  while (Serial.available() == 0) {}
  x = Serial.read();
  delay(10);
  y = Serial.read();
  delay(10);

Do nothing until the first byte arrives. Then read two bytes.

Hardly seems like a bright thing to do, to me.

Consider, too, that serial data transmission uses the United States Postal Service delivery model. They do NOT guarantee to deliver the mail. They guarantee to try to deliver the mail. Sometimes, they don't try very hard.

Serial data CAN get lost or corrupted. If the y byte gets lost, you'll read two x bytes as x and y. Then, you'll read two more bytes (a y and an x byte) as x and y, and be hosed when the device using the data flips 90 degrees on you.

Sending binary data is NOT the best way to send serial data. It IS the fastest, but if you were REALLY concerned about speed, you would not have littered your code with delay()s.

Read Robin2's tutorial again. And again, until you get it. Because, right now, you do not get it.

Hi.

Here is the code;(This is the code without using HC-12 Module)

try to input your code with code fiture on left side post toolbars with (</>) icons.