#include <SoftwareSerial.h>
// Define the pins for RX and TX
#define RX_PIN 2
#define TX_PIN 3
SoftwareSerial pumpSerial(RX_PIN, TX_PIN);
void setup() {
// Initialize hardware serial for debugging
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect
}
// Initialize software serial for pump communication
pumpSerial.begin(9600);
Serial.println("Arduino ready to communicate with syringe pump.");
}
void loop() {
// Command to send
String command = "run\r\n";
// Send the command
Serial.print("Sending command: ");
Serial.print(command);
pumpSerial.print(command);
// Wait for 1 second
delay(1000);
// Read and print the response
String response = "";
while (pumpSerial.available() > 0) {
char inChar = (char)pumpSerial.read();
response += inChar;
}
// Print the response if not empty
if (response != "") {
Serial.print("Response received: ");
Serial.println(response);
} else {
Serial.println("No response received.");
}
// Wait before sending the next command (adjust as needed)
delay(5000);
}