Not doing whats wanted

I have a TX/RX setup. The transmitter sends variuos codes to a receiver that receives the codes and controls movement/direction of a stepper.

I wanted it to rotate the stepper whilst the transmitter button was held down and stop when button released.

However the only way to keep the motor rotating is by pressing/releasing/pressing/releasing etc the transmit button. I thought with a constant reception of a code the receiver code would rotate in a loop (i.e look at code/rotate motor/go back look at code etc but does not do that.

No errors generated in this code, just the process not doing what I want.

I have tried auto format, reduce indent, this is best it would format

//FINAL CAMERA RECEIVER
//HC12CAMERATX
#include <SoftwareSerial.h>
SoftwareSerial mySerial(5, 6); // RX, TX
const int stepPin = 3;
const int dirPin = 4;
int i;
//declare variables for the motor pins
int motorPin1 = A0;    // Blue   - 28BYJ48 pin 1
int motorPin2 = A1;    // Pink   - 28BYJ48 pin 2
int motorPin3 = A2;    // Yellow - 28BYJ48 pin 3
int motorPin4 = A3;   // Orange - 28BYJ48 pin 4

int motorSpeed = 7000; //variable to set stepper speed
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};

int turnDirection = 0;
int turnClockwise = 0;
int turnAnticlockwise = 0;

void setup() {
  mySerial.begin(9600);
  Serial.begin(9600);
  // Set pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);

}

void loop() {

  if (mySerial.available() > 2) {
    int input = mySerial.parseInt();
    Serial.println(input);
    if (input == 1001) {
      int turnClockwise = 1;
      int turnAnticlockwise = 0;
      Pan();
    }

    else if (input == 1002) {
      int turnClockwise = 0;
      int turnAnticlockwise = 1;
      Pan();
    }


  }

  //mySerial.flush();//clear the serial buffer for unwanted inputs

  delay(20);//delay little for better serial communication
}


void Pan()
{

  if (turnDirection == turnClockwise) {
    clockwise();
  } else if ((turnDirection == turnAnticlockwise)) {
    anticlockwise();
  }

}
// ---------------------------------------------------
void clockwise()
{
  for (int i = 0; i < 8; i++) // clockwise
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}
// ---------------------------------------------------

void anticlockwise()
{
  for (int i = 7; i >= 0; i--) // anticlockwise
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);

  }
}

//----------------------------------------------------

void setOutput(int out)
{
  digitalWrite(motorPin1, bitRead(lookup[out], 0));
  digitalWrite(motorPin2, bitRead(lookup[out], 1));
  digitalWrite(motorPin3, bitRead(lookup[out], 2));
  digitalWrite(motorPin4, bitRead(lookup[out], 3));
}

// --------------------------------------------------

Read about variable definition and scope...

if (input == 1001) {
   [color=red][b]int[/b][/color] turnClockwise = 1;
   [color=red][b]int[/b][/color] turnAnticlockwise = 0;
   Pan();
}

You are redefining your variables with a very short lifespan...(same for the other if)

Also you wait for 2 chars but apparently you read 4 (1001 or 1002)

Thanks J-M-L.

"if (mySerial.available() > 2)" should I make >3 ?

int turnClockwise = 1;
int turnAnticlockwise = 0; These two define if the motor is to turn left or right. Sorry, can you clarify on the short lifespan comment, am I doing something wrong.

I just want to keep the motor running in the selected direction all the time while my button is held down.

My incoming codes will be written in to accept 1001 to 1010.

Read about scope

You are not using the variables you have defined at the top, you are redefining new ones that the rest of the code outside the if do not see. => get rid of the int

Thanks, but made no difference at all, still have to pulse the pushbutton to get the motor to rotate.

Just as extra help, this is the transmitter

//HC-12 Momentary Button Send
// HC12CAMERATX
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //RX, TX
int buttonState;
int buttonPins[5] = {5, 6, 7, 8, 9}; // Pins used for buttons
int modePin = 10;
int MyArray1[5] = {1001, 1002, 1003, 1004, 1005};
int MyArray2[5] = {1006, 1007, 1008, 1009, 1010};
int nbts = 5; // Number of buttons
int i;
boolean btgs[5];
boolean onOff = 0;

void setup() {
  for (i = 0; i < nbts; i = i + 1) {
    pinMode(buttonPins[i], INPUT);
    digitalWrite(buttonPins[i], HIGH);
  }
  pinMode(modePin, INPUT);
  digitalWrite(modePin, HIGH);
  mySerial.begin(9600);
  Serial.begin(9600);
}

void loop() {
  for (int i = 0;  i < nbts; i++) {
    if (!btgs[i]) {

      if ((digitalRead(buttonPins[i]) == LOW) && (digitalRead(modePin) == HIGH)) {
        mySerial.println(MyArray1[i]);
        Serial.println(MyArray1[i]);
        btgs[i] = true;
        delay(30);
      }

      if ((digitalRead(buttonPins[i]) == LOW) && (digitalRead(modePin) == LOW)) {
        mySerial.println(MyArray2[i]);
        Serial.println(MyArray2[i]);
        btgs[i] = true;
        delay(30);
      }

    }
    else
    {

      if ( digitalRead(buttonPins[i]) == HIGH) {
        mySerial.println(2222);
        btgs[i] = false;

      }
    }
  }
}

Just thinking although I will have to search for how, but what if I put the transmitter output section into a while loop that keeps sending the code until the button is released?

Thanks J-M-L, I have fixed my problem, changed code in transmitter and all works.

 if ((digitalRead(buttonPins[i]) == LOW) && (digitalRead(modePin) == HIGH)) {
        mySerial.println(MyArray1[i]);
        Serial.println(MyArray1[i]);
        delay(30);
        mySerial.println(2222);
        delay(30);
      }

      if ((digitalRead(buttonPins[i]) == LOW) && (digitalRead(modePin) == LOW)) {
        mySerial.println(MyArray2[i]);
        Serial.println(MyArray2[i]);
        delay(30);
        mySerial.println(2222);
        delay(30);
      }

Alternates between active and non active control code.

Thanks again

To fix Your pb (if you don't want to keep repeating sending the info) you need to remove the motor movements from inside the brackets of the "if I have received 4 digits" block. Check if you have received communication, if so read it and adjust variables. Then outside the if, move the motor based on the value of the variables

Thanks but not quite sure what you mean.

If I look at the serial monitor on the receive end, I see just one line value 1001. When I release the transmitter button, it reads 2222. Unless I break this up in some way at the transmitter end or receiver, it just puts one coil pattern/sequence into the stepper and the stepper freezes there. Now I have broken up whats transmitted the motor keeps being updated and rotates until I let go of my button.

Can you show me an example of code you are suggesting, thanks

I mean just move the Pan(); call outside the if and the else clause -just call Pan(); at every loop(). Of course fix the int issue I mentioned

Thanks again for your help.