How to send serial commands

Hi I have asked this question before sometime ago but without success so I will try one more time. I have an Atlas pH stamp which attaches to serial3 of the mega. It has embedded code for the pH function. Now to calibrate the probe you have to go to the serial monitor and type
cal,mid,7 then you hit return. The response back on the monitor is calibrated to 7. No my question is I want to do away with the serial monitor as my project will be boxed away. I would like to add a button that when pressed will send the cal,mid,7 return command. I have tried the following
serialPrint("cal,mid,7\r"); but all that does is print to the serial monitor not to the command line. Does anybody have any insight how to code syntax to write to the command line with a return without actually doing it on the serial monitor?

What command line? If you want help, you need to describe your system in enough detail that people can make sense out of your question. That would include posting the code, using code tags, and links to the devices.

Please see the "How to best the best out of the forum" post.

The command line, top of the serial monitor where you would type a command then send. I have no code, as I stated above I have tried with serial print but that does not work hence my query

At a wild guess, it seems that you want to send a set of local stored variables to a peripheral device which you have clearly attached to serial3. In that event, using Serial3.print(data); will be a good place to start.

Serial.print will send send the data to the serial monitor, as you have found out.

It would thus seem that the most you need is to learn how to put a button on a pin and make its state a condition for sending the Serial3.print command. If this is just routine, putting the command in setup may be all you need - no button, or just use the existing reset button.

Do you load a special sketch before you send the command to the Arduino's serial input? If so, an examination of that sketch will show what the sketch does when it receives that command. Do the same thing but on a button press instead.

As it stands, your code reads from the command line "cal,mid,7" and then does something like call a function...
If you add a button, you have to add code to sense that button state and, when pressed, call the same function.

Without posting your code, that is as far as it goes...

 /*
AquArt ALKTec_Mega

WRITTEN BY MARIANO PROIETTI
16 CRANE AVENUE
ISLEWORTH
MIDDLESEX, TW7 7JL
TEL. 07771 528 969
*/

#include <Wire.h>
#include <AccelStepper.h>

//OUTPUT PINS//
#define DIR 12       //TCM2208 DRIVER PIN
#define STP 11       //TCM2208 DRIVER PIN
#define MS2 10       //TCM2208 DRIVER PIN
#define MS1 9        //TCM2208 DRIVER PIN
#define EN 8         //TCM2208 DRIVER PIN
#define P1 7         //PUMP 1 PIN
#define P3 6         //PUMP 3 PIN
#define STIR 5       //STIRRER PIN
#define REDLED 4
#define BLUELED 3

//INPUT PINS//
int STATELED = 22;
int STIRRER = 23;
int PUMP1 = 24;
int PUMP2 = 25;
int PUMP3 = 26;
int PHCAL2 = 27;
int PHCAL1 = 28;

#define TMC2208 1
AccelStepper stepper = AccelStepper(TMC2208, STP, DIR);


//GLOBAL VARIABLES//
int Speed = 1000;
int StirValue =0;

//PH GLOBALS//
String inputstring = "";                            
String sensorstring = "";                            
boolean input_string_complete = false;               
boolean sensor_string_complete = false;              
float pH;
float pHValue;

void setup()
{
  Serial.begin(9600);
  Serial3.begin(9600);                                
  inputstring.reserve(10);                           
  sensorstring.reserve(30);

  Wire.begin(8);
  Wire.onRequest(SendData);

  stepper.setMaxSpeed(3000);
  pinMode(DIR, OUTPUT);
  pinMode(STP, OUTPUT);
  pinMode(EN, OUTPUT);
  pinMode(MS1, OUTPUT);
  pinMode(MS2, OUTPUT);
  pinMode(P1, OUTPUT);
  pinMode(P3, OUTPUT);
  pinMode(STIR, OUTPUT);
  
  pinMode(A0, INPUT);
  pinMode(PHCAL2, INPUT);
  pinMode(PHCAL1, INPUT);
  pinMode(STATELED, INPUT);
  pinMode(STIRRER, INPUT);
  pinMode(PUMP1, INPUT);
  pinMode(PUMP2, INPUT);
  pinMode(PUMP3, INPUT);

     /*
  * MS1     MS2             RESOLUTION
________________________________________
  * LOW     HIGH            1/2 STEP
  * HIGH    LOW             1/4 STEP
  * LOW     LOW             1/8 STEP
  * HIGH    HIGH            1/16 STEP
*/

  digitalWrite(MS1, LOW);
  digitalWrite(MS2, LOW);
  
  digitalWrite(EN, HIGH);
  digitalWrite(DIR, HIGH);

  digitalWrite(P1, LOW);
  digitalWrite(P3, LOW);
  digitalWrite(STIR, LOW);

  delay(5000);
}

void loop() 
{
  StirrerFunction();
  PumpFunction();
  StatusLedFunction();

  if (input_string_complete == true)
  {               
    Serial3.print(inputstring);                      
    Serial3.print('\r');                             
    inputstring = "";                                 
    input_string_complete = false;                    
  }
  if (sensor_string_complete == true)
  {
    if (isdigit(sensorstring[0]))
    {
      pH = sensorstring.toFloat();
      Serial.println(pH);
    }
    if (digitalRead(PHCAL1) == 1 && digitalRead(PHCAL2) == 0)
    {
      Serial.print("cal,mid,7\r\n");
      //Serial3.print('\r');
    }
    if(digitalRead(PHCAL1) == 0 && digitalRead(PHCAL2) == 1)
    {
      Serial.print("cal,low,4\r\n");
      //Serial3.print('\r');
    }
    if(digitalRead(PHCAL1) == 1 & digitalRead(PHCAL2) == 1)
    {
      Serial.print("cal,high,10\r\n");
      //Serial3.print('\r');
    }
    if(digitalRead(PHCAL1) == 0 && digitalRead(PHCAL2) == 0)
    {
      //DO NOTHING
    }
  }
  sensorstring = "";                                  
  sensor_string_complete = false;
}

void StirrerFunction()
{
  int PotValue = analogRead(A0);
  StirValue = map(PotValue, 0, 1023, 0, 255);
  if (digitalRead (STIRRER) == HIGH)
  {
    analogWrite(STIR, StirValue);
  }
  else
  {
    digitalWrite(STIR, LOW);
  }
}

void PumpFunction()
{
  if (digitalRead(PUMP1) == HIGH)
  {
    digitalWrite(P1, HIGH);
  }
  else if (digitalRead(PUMP1) == LOW)
  {
    digitalWrite(P1, LOW);
  }
  
  if (digitalRead(PUMP2) == HIGH)
  {
    stepper.setSpeed(-Speed);
    stepper.runSpeed();
    digitalWrite(EN, LOW);
  }
  else if (digitalRead(PUMP2) == LOW)
  {
    stepper.setSpeed(0);
    stepper.runSpeed();
    digitalWrite(EN, HIGH);
  }
  
  if (digitalRead(PUMP3) == HIGH)
  {
    digitalWrite(P3, HIGH);
  }
  else if (digitalRead(PUMP3) == LOW)
  {
    digitalWrite(P3, LOW);
  }
}

void StatusLedFunction()
{
  if (digitalRead(STATELED) == HIGH)
  {
    digitalWrite(REDLED, HIGH);
    digitalWrite(BLUELED, LOW);
  }
  else
  {
    digitalWrite(REDLED, LOW);
    digitalWrite(BLUELED, HIGH);
  }
}

void serialEvent3() 
{                                 
  sensorstring = Serial3.readStringUntil(13);         
  sensor_string_complete = true;                      
}

void serialEvent() 
{                                  
  inputstring = Serial.readStringUntil(13);           
  input_string_complete = true;                       
}
  
void SendData()
{
  Wire.write((byte*)&pH, 4);
}

This is my code. As you can see there is not much for the Atlas Scientific pH as the embed their code into their stamp.

So, the current sequence is

  1. receive string on Serial
  2. transmit string to Serial3
  3. let Serial3 response code read/interpret

So add, the button, and when you detect it, transmit the code to Serial3. Wire your button as one side to ground, the other to a pin.
Inside setup(), declare that pin as INPUT_PULLUP.

When that button gets pressed (it will read LOW), Serial3.print("cal,mid,7\r\n");

You will want to do this only when the button GETS pressed, not IS pressed. Look at the State Change Detection example in the IDE (File->examples-02.digital->State Change Detection) to see how to do that.

Shouldn’t you be sending the command to Serial3 instead of Serial?

Yes I should I was just trying something. I think also the calibration procedure as written is in the wrong place. I will post my revised code later

Ok I have managed to get it working. I had the calibration procedure in the wrong place initially.
Here is the part of the corrected code for anybody interested. Thanks to all who helped.

void loop() 
{
  StirrerFunction();
  PumpFunction();
  StatusLedFunction();
  CalibrateFunction();

  if (input_string_complete == true)
  {               
    Serial3.print(inputstring);                      
    Serial3.print('\r');                             
    inputstring = "";                                 
    input_string_complete = false;                    
  }
  if (sensor_string_complete == true)
  {
    if (isdigit(sensorstring[0]))
    {
      pH = sensorstring.toFloat();
      Serial.println(pH);
    }
  }
  if (CAL7 == true)     //(digitalRead(PHCAL1) == 1 && digitalRead(PHCAL2) == 0)
  {
    Serial3.print("cal,mid,7\r\n");
  }
  if (CAL4 == true)     //(digitalRead(PHCAL1) == 0 && digitalRead(PHCAL2) == 1)
  {
    Serial3.print("cal,low,4\r\n");
  }
  if (CAL10 == true)    //(digitalRead(PHCAL1) == 1 & digitalRead(PHCAL2) == 1)
  {
    Serial3.print("cal,high,10\r\n");
  }
  sensorstring = "";                                  
  sensor_string_complete = false;
}

void CalibrateFunction()
{
  if (digitalRead(PHCAL1) == 1 && digitalRead(PHCAL2) == 0)
  {
    CAL7 = true;
    CAL4 = false;
    CAL10 = false;
  }
  else if (digitalRead(PHCAL1) == 0 && digitalRead(PHCAL2) == 1)
  {
    CAL7 = false;
    CAL4 = true;
    CAL10 = false;
  }
  else if (digitalRead(PHCAL1) == 1 & digitalRead(PHCAL2) == 1)
  {
    CAL7 = false;
    CAL4 = false;
    CAL10 = true;
  }
  else
  {
    CAL7 = false;
    CAL4 = false;
    CAL10 = false;
  }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.