I've got a project that I'm working on and need to control a relay via serial commands from an audio DSP. I've written the code for a Nano and, using the IDE serial monitor, can send the commands and the relay responds correctly.
I assume though, that I need to use a "second" serial interface on the nano so that I can use the serial monitor to test and debug the code. I've been messing with SoftwareSerial, but it's acting strange. I've tried sending serial commands to the nano (to the SoftwareSerial designated pins) with a USB-> Serial (DB9) adapter, and then wired from the DB9 to the nano. And I've also tried with the Audio DSP. In both cases, I get consistent but inaccurate data. For example, if I send "OFF\n", I see 58 FFFFFFAE FFFFFFAE 3D. Every time. If I send "ON\n", I see 58 FFFFFFAC 3D. So the \n is coming across as 0x3D, and the letter 'O' is always 0x58.
Here's the code:
#include <SoftwareSerial.h>
// constants won't change
const int RELAY_PIN = 8; // the Arduino pin, which connects to the IN pin of relay
const int MOMENTARY_TIME = 100;
const int CORE_RX_PIN = 2;
const int CORE_TX_PIN = 3;
const int CORE_BAUD = 9600;
// Data we get from the serial interface
String rxData;
// Software serial endpoint
SoftwareSerial coreSerial(CORE_RX_PIN, CORE_TX_PIN); // RX, TX
//
// setup()
// Runs once when you press reset or power the board
//
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Native USB only
}
Serial.println("coreSerialRelay - starting up");
// set the data rate for the SoftwareSerial port
coreSerial.begin(CORE_BAUD);
coreSerial.println("ready");
// initialize digital pin as an output for the relay
pinMode(RELAY_PIN, OUTPUT);
Serial.println("ready");
relayMomentary();
}
//
// loop()
// Runs over and over again forever
//
void loop() {
coreSerial.listen();
while (coreSerial.available() > 0) {
char received = coreSerial.read();
Serial.print(received, HEX);
Serial.print(" ");
rxData += received;
// Process message when new line character is recieved
if (received == '\n')
{
Serial.print("Arduino Received: ");
Serial.print(rxData);
parseData();
rxData = ""; // Clear recieved buffer
}
}
}
//
// parseData()
// Parse the serial info we just recieved and respond
//
void parseData() {
// Uppercase the string - keep it easy on the use
rxData.toUpperCase();
// Turn on the relay
if (rxData == "ON\n") {
Serial.println("on");
relayClose();
coreSerial.println("OK");
}
// Turn off the relay
if (rxData == "OFF\n") {
Serial.println("off");
relayOpen();
coreSerial.println("OK");
}
// Momentary on
if (rxData == "MOMENTARY\n") {
Serial.println("mom");
relayMomentary();
coreSerial.println("OK");
}
// User wants current status
if (rxData == "STATUS\n") {
if (digitalRead(RELAY_PIN)) {
coreSerial.println("ON");
}
else {
coreSerial.println("OFF");
}
coreSerial.println("OK");
}
}
//
// relayClose()
//
void relayClose() {
digitalWrite(RELAY_PIN, HIGH);
}
//
// relayOpen()
//
void relayOpen() {
digitalWrite(RELAY_PIN, LOW);
}
//
// relayMomentary
//
void relayMomentary() {
digitalWrite(RELAY_PIN, HIGH);
delay(MOMENTARY_TIME);
digitalWrite(RELAY_PIN, LOW);
}