I am able to send a receive variables completely fine when i run functions separately for instance i will send over and i will receive ABC on the serial line just like i want to. if i run another function separately which i am using to set the value of a variable to an integer and i send (250) i will receive 250 just like i expect on the serial monitor. I am trying to control a linear actuator so that it achieves a certain pressure value and then holds that pressure value. I send commands to the actuator using the <> brackets and i send my variable value in (). I don't seem to be able to run these two commands at the same time. I am trying to send the commands in <> to an actuator and keeping the value i send in () saved on the arduino as a variable to reference against the incoming analog read which is my pressure value. If anyone may be able to offer some advice thanks in advance. My next step after figuring this out will be incorporating a PID.
The following is the code that i am trying to get to run:
#include <SoftwareSerial.h>
#define rxPin 10 //define receiver pin
#define txPin 11 //define transmitter pin
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
const byte numCharspress = 32;
char receivedCharspress[numCharspress];
boolean newDatapress = false;
int t = 1000;//delay
int analogPin = 0;
int val = 0;
int pval;
void setup() {
pinMode(rxPin, INPUT); //pin type
pinMode(txPin, OUTPUT); //pin type
Serial.begin(115200); //set baud rate for arduino to pc
mySerial.begin(115200); //set baud rate for arduino to actuator
delay(t);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
mySerial.println("SP1200000"); //set initial speed
mySerial.println("AC3000"); //set initial acceleration
mySerial.println("TA5000"); //cycle through some movements to let me know everythings working
delay(t);
mySerial.println("TA10000"); //cycle through some movements to let me know everythings working
delay(t);
mySerial.println("TA15000"); //cycle through some movements to let me know everythings working
delay(t);
mySerial.println("TR"); //return to start of movement
delay(t);
}
void loop()
{
getPressureval(); // gets pressure value from pressure sensor on apin 0
recvpressWithStartEndMarkers(); // gets pressure value from pc serial input in "()"
showNewDatapress(); //echos received data to serial monitor so i know it was received correctly
recvWithStartEndMarkers(); //gets commands to be sent to actuator in "<>"
showNewData(); //sends commands from pc to actuator that were sent in "<>"
pressureMove(); // Moves actuator so that it maintains pressure value set in "()"
}
void getPressureval()
{
val = analogRead(analogPin); //reads the pressure sensor
Serial.println(val); //sends the pressure values through serial
delay(10);
}
void recvpressWithStartEndMarkers()
{
static boolean recvInProgresspress = false;
static byte ndxpress = 0;
char startMarkerpress = '(';
char endMarkerpress = ')';
char rcpress;
while (Serial.available() > 0 && newDatapress == false) {
rcpress = Serial.read();
if (recvInProgresspress == true) {
if (rcpress != endMarkerpress) {
receivedCharspress[ndxpress] = rcpress;
ndxpress++;
if (ndxpress >= numCharspress) {
ndxpress = numCharspress - 1;
}
}
else {
receivedCharspress[ndxpress] = '\0'; // terminate the string
recvInProgresspress = false;
ndxpress = 0;
newDatapress = true;
}
}
else if (rcpress == startMarkerpress) {
recvInProgresspress = true;
}
}
}
void showNewDatapress()
{
if (newDatapress == true) {
pval = atoi(receivedCharspress);
Serial.println(pval);
newDatapress = false;
}
}
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void showNewData()
{
if (newData == true) {
mySerial.println(receivedChars);
Serial.println(receivedChars);
newData = false;
}
}
void pressureMove ()
{
if (val > pval)
{
mySerial.println("TR");
}
else if (val< pval)
{
mySerial.println("TE");
}
else
{
mySerial.println("TK");
}
}
Since both functions get called, and newDatapress and newData are true ONLY when the end markers arrive, you are reading all available data twice. You are guaranteed to scramble data that way.
It would be far simpler to have ONE function to get serial data, with one kind of start and end markers. Inside the markers, you would have <A:actuatorData> or <P:pressureData>. When newData is true, you would then look at the first character to decide whether it is actuator data or pressure data, and use the remaining data appropriately.
You should just have one receive function and include in the data some identifier so the Arduino knows what the data is for. For example <X, ABC> and <Y,250>
Another (perhaps simpler) option is to send all the data every time even if values have not changed. For example <ABC,250>
I thought about doing something like that as i have done something similar in a previous project, but wasn't able to get it working for this project. I tried it earlier on but what i had before seemed a little bit easier for me to do. I cant seem to get the right ouput though
void showNewData()
{
if (newData == true)
{
char* a = receivedChars;
if (a[0] = A )
{
string1 = String(receivedChars);
Serial.println(string1);
L = string1.length();
command = receivedChars[1,L];
Serial.println(command);
}
newData = false;
}
}
if i print a[0] i get A when i send over
but when i run it through my statement i can't get it to print the rest of the characters
I figured out the first part i forgot to compare it to a character, but I cant seem to figure out how to store the characters that I loop through in my for loop so that i can later issue them in a Serial.println() command. I tried using Serial.print() which sends all the characters on one line and then sending a Serial.print("\n") to terminate it, but the actuator won't pick this up i had this problem before it needs to be sent as Serial.println().
void showNewData()
{
if (newData == true)
{
char* a = receivedChars;
if (a[0] = 'A' )
{
s = a;
L = s.length();
V = L - 1;
for (int i=1; i<L; i++)
{
C = a[i];
}
Serial.println(C);
}
newData = false;
}
}
I only have one arduino. It is hooked up to a PC it sends commands like to set the pressure value that i want to be achieved, acceleration/speed of actuator. I want to have the actuator hold certain pressure values but this is the area that i can't seem to get to work. If i run my code and skip over the reading of the pressure data and the movement of the actuator based on the pressure data i can issue position commands in the brackets i.e but if i turn on the analog read for the pressure value and the loop i have running to try to maintain a pressure is where i run into an issue. I can no longer send position commands and i also can't get the actuator to move to the position i set the pressure to even though i can see that the arduino is receiving my commands.
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
int t = 1000;//delay
int analogPin = 0;
int val;
int pval;
String s = "";
void setup() {
Serial.begin(115200); //set baud rate for arduino to pc
Serial1.begin(115200); //set baud rate for arduino to actuator
delay(t);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial1.println("SP1200000"); //set initial speed
Serial1.println("AC3000"); //set initial acceleration
Serial1.println("TA5000"); //cycle through some movements to let me know everythings working
delay(t);
Serial1.println("TA10000"); //cycle through some movements to let me know everythings working
delay(t);
Serial1.println("TA15000"); //cycle through some movements to let me know everythings working
delay(t);
Serial1.println("TR"); //return to start of movement
delay(t);
}
void loop()
{
getPressureval(); // gets pressure value from pressure sensor on apin 0
recvWithStartEndMarkers(); // gets pressure value from pc serial input in "()"
showNewData(); //sends commands from pc to actuator that were sent in "<>"
pressureMove(); // Moves actuator so that it maintains pressure value set in "()"
}
void getPressureval()
{
val = analogRead(analogPin); //reads the pressure sensor
Serial.println(val); //sends the pressure values through serial
delay(10);
}
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void showNewData()
{
if (newData == true)
{
char* a = receivedChars;
s = a;
if (a[0] = 'A' )
{
s.remove(0,1);
Serial.println(s);
Serial1.println(s);
}
else if (a[0] = 'P')
{
s.remove(0,1);
pval = s.toInt();
Serial.println(pval);
}
newData = false;
}
}
void pressureMove ()
{
if (val > pval)
{
Serial1.println("TR");
}
else if (val< pval)
{
Serial1.println("TE");
}
else
{
Serial1.println("TK");
}
}
Yes a pressure sensor connected to an arduino MEGA 2560 and an actuator connected to the same arduino. I am reading the incoming pressure data on analog pin 0. I am sending commands to the actuator via rx1 and tx1 on the mega board. I am trying to send the pressure values read on the analog pin to the computer via the usb A to usb B cable on the MEGA 2560. I am trying to set the desired pressure value that i want with the computer and also send the actuator certain data like max accel and max speed and also if i want to manually decide how far i want the actuator to extend.
I am using the following materials:
max3232 chip from Sparkfun so that the arduino can communicate with the actuator which is using rs232
Arduino Mega 2560
Omega PX309-015G5V (Pressure Range 0-15psi, 0-5Vdc)
Why do you need to control the actuator speed and acceleration?
Have you tried PID control of the pressure actuator, it will take care of your response times and control errors.
Tom...
PS. Can you begin spacing you answers like in posts replying to your posts please, the forum editor does not do indenting or paragraphs.
So without carriage returns all your sentences run together.
I need to control the actuator speed and acceleration because it minimizes the amount of travel that the actuator will move during a set time.
The actuator needs to be issued a stop command in order to stop its current trajectory and then issued a new command which will reverse its trajectory so that it can go to the set value there is a small delay in this command so if i decrease the speed and acceleration there will be less travel during this delay which will give me pressure values closer to what i'm looking for. which is why i want to take the output of a PID and use it to control the speed and acceleration of the actuator.
//Receiving Variables
const byte numChars = 32; //deifnes size of byte array
char receivedChars[numChars]; //defines receivedChars as character array
boolean newData = false; //Inititalezes boolean value
// timing variables
int t = 1000;//delay
//Pressure Variables and Pins
int analogPin = 0; //sets the analog pin to
//double Input; // value of pressure read by analog pin
double Setpoint1; //value of pressure set by serial
//working variables
unsigned long lastTime;
double Input, Output, Setpoint;
double errSum, lastInput;
double kp, ki, kd;
double Kp = 1; double Ki = 1; double Kd = 1;
void setup() {
Serial.begin(115200); //set baud rate for arduino to pc
Serial1.begin(115200); //set baud rate for arduino to actuator
delay(t);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial1.println("SP12000000"); //set initial speed
Serial1.println("AC3000"); //set initial acceleration
Serial1.println("TA5000"); //cycle through some movements to let me know everythings working
delay(t);
Serial1.println("TA10000"); //cycle through some movements to let me know everythings working
delay(t);
Serial1.println("TA15000"); //cycle through some movements to let me know everythings working
delay(t);
Serial1.println("TR"); //return to start of movement
delay(t);
}
void loop()
{
getPressureval(); // gets pressure value from pressure sensor on apin 0
recvWithStartEndMarkers(); // gets pressure value from pc serial input in "()"
commands(); //sends commands from pc to actuator that were sent in "<>"
Compute();
SetTunings(double Kp, double Ki, double Kd);
pressureMove(); // Moves actuator so that it maintains pressure value set in "()"
}
void getPressureval()
{
Input = analogRead(analogPin); //reads the pressure sensor
//Serial.println(Input1); //sends the pressure values through serial
delay(10);
}
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false; //creates variable visible to only one function with boolean
static byte ndx = 0;
char startMarker = '<'; //sets begin condition
char endMarker = '>'; //sets end condition
char rc; //sets variable type to char
while (Serial.available() > 0 && newData == false) {
rc = Serial.read(); //sets rc equal to serial value
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void commands()
{
if (newData == true)
{
if (receivedChars[0] == 'A')
{
//Serial.println(&receivedChars[1]);
Serial1.println(&receivedChars[1]);
}
else if (receivedChars[0] == 'P')
{
Setpoint1 = atoi(&receivedChars[1]);
Setpoint = Setpoint1;
Serial.println(Setpoint);
}
else if (receivedChars[0] == 'S')
Setpoint = 0;
Serial1.println("TR");
delay(5000);
Serial1.println("TK");
}
newData = false;
}
void Compute()
{
/*How long since we last calculated*/
unsigned long now = millis();
double timeChange = (double)(now - lastTime);
/*Compute all the working error variables*/
double error = Setpoint - Input;
errSum += (error * timeChange);
double dErr = (error - lastErr) / timeChange;
/*Compute PID Output*/
Output = kp * error + ki * errSum + kd * dErr;
/*Remember some variables for next time*/
lastErr = error;
lastTime = now;
}
void SetTunings(double Kp, double Ki, double Kd)
{
kp = Kp;
ki = Ki;
kd = Kd;
}
void SetSampleTime(int NewSampleTime)
{
if (NewSampleTime > 0)
{
double ratio = (double)NewSampleTime
/ (double)SampleTime;
ki *= ratio;
kd /= ratio;
SampleTime = (unsigned long)NewSampleTime;
}
}
void pressureMove ()
{
if (Input > Setpoint + Setpoint*.1)
{
Serial1.println("TK");
//Serial1.println("SP");
//Serial1.println("AC");
Serial1.println("TR");
}
else if (Input< Setpoint - Setpoint*.1)
{
Serial1.println("TK");
//Serial1.println("SP");
//Serial1.println("AC");
Serial1.println("TE");
}
else
{
Serial1.println("TK");
}
}
I Keep getting the error: exit status 1
expected primary-expression before 'double'
So i am trying to figure this out now if you have any suggestions please let me know i am just concerned with getting the PID to work right now and getting the Output from it i will then take that output and use it to adjust the speed and accel.
I fixed that I have added the rest of his program to my arduino I am now just trying to make it so that i can control the speed and get the outputs I would expect. My main concern right now is getting the Outputs and seeing their behavior i want to set my pressure value and then see the output values decrease as i near that pressure value although now i am only seeing the output as zero.
//Receiving Variables
const byte numChars = 32; //deifnes size of byte array
char receivedChars[numChars]; //defines receivedChars as character array
boolean newData = false; //Inititalezes boolean value
// timing variables
int t = 1000;//delay
//Pressure Variables and Pins
int analogPin = 0; //sets the analog pin to
//double Input; // value of pressure read by analog pin
double Setpoint1; //value of pressure set by serial
//working variables
unsigned long lastTime;
double Input, Output, Setpoint;
double ITerm, lastInput;
double kp, ki, kd;
int SampleTime = 10; //.01 sec
double Kp = 1; double Ki = 1; double Kd = 1;
int NewSampleTime;
double Min; double Max;
double outMin = -50000000; double outMax = 50000000;
bool inAuto = false;
int Mode;
int Direction;
#define MANUAL 0
#define AUTOMATIC 1
#define DIRECT 0
#define REVERSE 1
int controllerDirection = DIRECT;
void setup() {
Serial.begin(115200); //set baud rate for arduino to pc
Serial1.begin(115200); //set baud rate for arduino to actuator
delay(t);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial1.println("SP12000000"); //set initial speed
Serial1.println("AC3000"); //set initial acceleration
Serial1.println("TA5000"); //cycle through some movements to let me know everythings working
delay(t);
Serial1.println("TA10000"); //cycle through some movements to let me know everythings working
delay(t);
Serial1.println("TA15000"); //cycle through some movements to let me know everythings working
delay(t);
Serial1.println("TR"); //return to start of movement
delay(t);
}
void loop()
{
getPressureval(); // gets pressure value from pressure sensor on apin 0
recvWithStartEndMarkers(); // gets pressure value from pc serial input in "()"
commands(); //sends commands from pc to actuator that were sent in "<>"
Compute();
SetTunings();
SetSampleTime();
SetOutputLimits();
SetMode();
Initialize();
SetControllerDirection();
pressureMove(); // Moves actuator so that it maintains pressure value set in "()"
Serial.println(Output);
}
void getPressureval()
{
Input = analogRead(analogPin); //reads the pressure sensor
Serial.println(Input); //sends the pressure values through serial
//delay(10);
}
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false; //creates variable visible to only one function with boolean
static byte ndx = 0;
char startMarker = '<'; //sets begin condition
char endMarker = '>'; //sets end condition
char rc; //sets variable type to char
while (Serial.available() > 0 && newData == false) {
rc = Serial.read(); //sets rc equal to serial value
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void commands()
{
if (newData == true)
{
if (receivedChars[0] == 'A')
{
//Serial.println(&receivedChars[1]);
Serial1.println(&receivedChars[1]);
}
else if (receivedChars[0] == 'P')
{
Setpoint1 = atoi(&receivedChars[1]);
Setpoint = Setpoint1;
//Serial.println(Setpoint);
}
else if (receivedChars[0] == 'S')
Setpoint = 0;
Serial1.println("TR");
delay(5000);
Serial1.println("TK");
}
newData = false;
}
void Compute()
{
/*How long since we last calculated*/
if(!inAuto) return;
unsigned long now = millis();
int timeChange = (now - lastTime);
if(timeChange>=SampleTime)
{
/*Compute all the working error variables*/
double error = Setpoint - Input;
ITerm += (ki * error);
if(ITerm> outMax) ITerm= outMax;
else if(ITerm< outMin) ITerm= outMin;
double dInput = (Input - lastInput);
/*Compute PID Output*/
Output = kp * error + ITerm - kd * dInput;
if(Output > outMax) Output = outMax;
else if(Output < outMin) Output = outMin;
//Serial.println(Output);
/*Remember some variables for next time*/
lastInput = Input;
lastTime = now;
}
}
void SetTunings()
{
if (Kp<0 || Ki<0|| Kd<0) return;
double SampleTimeInSec = ((double)SampleTime)/1000;
kp = Kp;
ki = Ki * SampleTimeInSec;
kd = Kd / SampleTimeInSec;
if(controllerDirection ==REVERSE)
{
kp = (0 - kp);
ki = (0 - ki);
kd = (0 - kd);
}
}
void SetSampleTime()
{
if (NewSampleTime > 0)
{
double ratio = (double)NewSampleTime
/ (double)SampleTime;
ki *= ratio;
kd /= ratio;
SampleTime = (unsigned long)NewSampleTime;
}
}
void SetOutputLimits()
{
if(Min > Max) return;
outMin = Min;
outMax = Max;
if(Output > outMax) Output = outMax;
else if(Output < outMin) Output = outMin;
if(ITerm> outMax) ITerm= outMax;
else if(ITerm< outMin) ITerm= outMin;
}
void SetMode()
{
bool newAuto = (Mode == AUTOMATIC);
if(newAuto && !inAuto)
{ /*we just went from manual to auto*/
Initialize();
}
inAuto = newAuto;
}
void Initialize()
{
lastInput = Input;
ITerm = Output;
if(ITerm> outMax) ITerm= outMax;
else if(ITerm< outMin) ITerm= outMin;
}
void SetControllerDirection()
{
controllerDirection = Direction;
}
void pressureMove ()
{
if (Input > Setpoint + Setpoint*.1)
{
Serial1.println("TK");
Serial1.println("TR");
}
else if (Input< Setpoint - Setpoint*.1)
{
Serial1.println("TK");
Serial1.println("TE");
}
else
{
Serial1.println("TK");
}
}
I am just using his as an example controller so i can familiarize myself with PID and implement my own.
I am able to control the piston to send it from one pressure value to another but the more I add to my program the longer the delay appears to be for the actuator to move.
My end goal is to be able to send pressure values from matlab to the arduino and have it move smoothly from pressure value to pressure value.
The basic idea is:
Read the pressure value on the analog pin. Compare the pressure read to the pressure value i set it to go to with the PC
Send the trajectory interrupt command via Serial1("TK")
Set the Speed via Serial1("SP...")
Set the acceleration via Serial1("AC...")
Move the actuator based on the Pressure value via Serial1("TE or TR") TR to decrease TE to increase
I was able to get an ok control system going by just using logic statements that got incrementally smaller. So if the read value is between 10% 9% 8% of setpoint adjust speed and accel to lower and lower values.
Here is my current code: (I have not been able to play with the tuning parameters yet since the system really isnt running smooth and am just printing them to the screen to see how it's working while i have the PID going).
#include <PID_v1.h>
//Receiving Variables
const byte numChars = 32; //deifnes size of byte array
char receivedChars[numChars]; //defines receivedChars as character array
boolean newData = false; //Inititalezes boolean value
// timing variables
int t = 1000;//delay
//Pressure Variables and Pins
int analogPin = 0; //sets the analog pin to
//Define Variables we'll be connecting to
double Setpoint, Setpoint1, Input, Output;
double min, max;
int Y,A,S;
String X;
String Velocity;
String Acceleration;
PID myPID(&Input, &Output, &Setpoint,1,1,1, DIRECT);
void setup() {
Input = analogRead(analogPin); //reads the pressure sensor
myPID.SetMode(AUTOMATIC);
min = -50000000;
max = 50000000;
myPID.SetSampleTime(10);
myPID.SetOutputLimits(min, max);
Serial.begin(115200); //set baud rate for arduino to pc
Serial1.begin(115200); //set baud rate for arduino to actuator
delay(t);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial1.println("SP12000000"); //set initial speed
Serial1.println("AC3000"); //set initial acceleration
Serial1.println("TA5000"); //cycle through some movements to let me know everythings working
delay(t);
Serial1.println("TA10000"); //cycle through some movements to let me know everythings working
delay(t);
Serial1.println("TA15000"); //cycle through some movements to let me know everythings working
delay(t);
Serial1.println("TR"); //return to start of movement
delay(t);
}
void loop()
{
getPressureval(); // gets pressure value from pressure sensor on apin 0
recvWithStartEndMarkers(); // gets pressure value from pc serial input in "()"
commands(); //sends commands from pc to actuator that were sent in "<>"
pressureMove(); // Moves actuator so that it maintains pressure value set in "()"
myPID.Compute();
Y = abs(Output);
A = Y;
S = Y * 1500000;
X = String(Y);
String Speed = "SP";
String Accel = "AC";
String Velocity = Speed + S;
String Acceleration = Accel + A;
Serial.println(Velocity);
Serial.println(Acceleration);
}
void getPressureval()
{
Input = analogRead(analogPin); //reads the pressure sensor
Serial.println(Input); //sends the pressure values through serial
delay(10);
}
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false; //creates variable visible to only one function with boolean
static byte ndx = 0;
char startMarker = '<'; //sets begin condition
char endMarker = '>'; //sets end condition
char rc; //sets variable type to char
while (Serial.available() > 0 && newData == false) {
rc = Serial.read(); //sets rc equal to serial value
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void commands()
{
if (newData == true)
{ Serial1.println("TK");
if (receivedChars[0] == 'A')
{
Serial1.println(&receivedChars[1]);
}
else if (receivedChars[0] == 'P')
{
Setpoint = atoi(&receivedChars[1]);
Setpoint1 = Setpoint;
//Serial.println(Setpoint);
}
else if (receivedChars[0] == 'S')
Serial1.println("TK");
Setpoint = 0;
Serial1.println("TR");
delay(5000);
Serial1.println("TK");
}
newData = false;
}
void pressureMove ()
{
if (Input > Setpoint1)
{
Serial1.println("TK");
//Serial1.println(Velocity);
//Serial1.println(Acceleration);
Serial1.println("TR");
}
else if (Input < Setpoint1)
{
Serial1.println("TK");
//Serial1.println(Velocity);
//Serial1.println(Acceleration);
Serial1.println("TE");
}
else
{
Serial1.println("TK");
}
}
You have delay(5000) in your program. You should not use the delay() function at all as the Arduino can do nothing useful during a delay. In 5 seconds an Arduino can do about 80 million instructions.
The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking.