I created this code with the intention of controlling the position of an actuator using only Speed and Time via the serial monitor. The serial monitor would act as an intermediate between a Arduino Mega & Raspberry-Pi later down the project.
Controller: Arduino Mega
Motor Driver: MegaMoto Motor Control Shield
Actuator: GLA200 12V DC Small Linear Actuator
Changing the speed works but I'm stilling having issues with the timing. The main issue at the moment is that even when either of the extension & retraction features have a delay(0) they still move to the ends of the actuators and would keep going if it wasn't for the limit switches when they shouldn't be moving.
Questions from this;
Is there an issue with the delay() function when it comes to Serial commands.
If so why is this the case.
Is the Millis() function a good alternative for the delay() feature for controlling the motor timings.
Is there any other options other than Millis().
When this issue is sorted, I will use a similar Case statement to the speed so I can change the timings on the go from the serial monitor.
//int EnablePin1 = 8;
int PWMPinA1 = 11; // Timer1
int PWMPinB1 = 3; // Timer2
long Speed = 0; //Speed variable which allows me to change the speed later on in the code
long receivedSpeed = 0; //Speed variable coming from the Serial Monitor
char receivedCommand; //Recieved variable from Serial Monitor
bool newData = false; // booleans for new data from serial
void setup() {
Serial.begin(9600);
// pinMode(EnablePin1, OUTPUT);
pinMode(PWMPinA1, OUTPUT);
pinMode(PWMPinB1, OUTPUT);
}//end setup
void loop() {
checkSerial();
}
void checkSerial() //function for receiving the commands
{
if (Serial.available() > 0) //If something comes from the computer
{
receivedCommand = Serial.read(); //Pass the value to the receivedCommad variable
newData = true; //Indicate that there is a new data by setting this bool to true
if (newData == true) //Only enter this long switch-case statement if there is a new command from the computer
{
switch (receivedCommand) //Checks what is the command
{
case 'A': //Move the actuator forwards
Serial.println("Positive direction.");
extendActuator();
break;
case 'B': //Move the actuator backwards
Serial.println("Negative direction.");
retractActuator();
break;
case 'Z': //Stop the actuator
Serial.println("Stop Actuator.");
stopActuator();
break;
case 'V': //Updates Speed for the actuator
receivedSpeed = Serial.parseFloat();
if (receivedSpeed > 0 && receivedSpeed <= 255)
Speed = receivedSpeed;
Serial.println("Speed Updated.");
break;
default:
break;
}
}
newData = false; //after we went through the above tasks, newData is set to false again, so we are ready to receive new commands again.
}
}
void extendActuator() {
// digitalWrite(EnablePin1, HIGH); //enable board
analogWrite(PWMPinA1, Speed); //apply desired speed
analogWrite(PWMPinB1, 0); //apply no speed
delay(0); //Delays the operation for x amount of time
}
void retractActuator() {
// digitalWrite(EnablePin1, HIGH); //enable board
analogWrite(PWMPinA1, 0); //apply no speed
analogWrite(PWMPinB1, Speed); //apply desired speeds
delay(0); //Delays the operation for x amount of time
}
void stopActuator() {
// digitalWrite(EnablePin1, LOW); //disable board
analogWrite(PWMPinA1, 0);
analogWrite(PWMPinB1, 0);//set speeds to 0
delay(0); //Delays the operation for x amount of time
}
Any advice will be appreciated greatly, and I will try to respond as fast as possible, cheers.
Pretty simple. If I put the delay to 0, it does nothing, understandable. But when I put in the serial stuff, and type in the case A, it will extend even though it should'nt be since the delay is set to 0.
The delay is meant to act as a timer for the function, but this only works (as I have been saying) when it is paired with another function straight after (Which is what I've been doing when troubleshooting). I recognise the issue now, when isolating the functions it works how you have been saying as it constantly loops.
I basically need to integrate a stop after the delay inside the extend/retract functions to tell it to actually stop after this time limit until another command comes in, i.e.
void extendActuator() {
// digitalWrite(EnablePin1, HIGH); //enable board
analogWrite(PWMPinA1, Speed); //apply desired speed
analogWrite(PWMPinB1, 0); //apply no speed
delay(2000); //Delays the operation for 2 amount of time
analogWrite(PWMPinA1, 0);
analogWrite(PWMPinB1, 0); //set speeds to 0
}