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));
}
// --------------------------------------------------
"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.
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
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?
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
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