I am having some issues and hope you guys here might be able to help me, i am writing a program that takes a command from a PC and toggles outputs high or low based off the command the arduino receives. I struggled to find a working example to use to start from but eventually found something that works. However, the example i found simply takes that data inputed to the serial port and prints it back out, i did get it to control output pins but i need it to be a bit more selective by if it gets this command these pins go high and these pins go low but the statement where the data is sent to be printed and where i have the digitalwrite for the pins wont allow me to add a && (receivedChars == "CH002") at the bottom under void shownewdata the program doesnt work, if you enter CH002; in the terminal it doesnt print or toggle the pins like its supposed to, if you get rid of && (receivedChars == "CH002") and enter CH002 it works but then its not selective and soaly prints out what input it received . Ultimately im going to have several commands like CH002 that are going to toggle pins and would like to get the code to be more selective.
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
const int cs1pin = 2; //
const int cs2pin = 3; //
const int cs3pin = 4; //
const int cs4pin = 5; //
const int cs5pin = 6; //
boolean newData = false;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
pinMode(cs1pin, OUTPUT);
pinMode(cs2pin, OUTPUT);
pinMode(cs3pin, OUTPUT);
pinMode(cs4pin, OUTPUT);
pinMode(cs5pin, OUTPUT);
}
void loop() {
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = ';';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if ((newData == true) && (receivedChars == "CH002")) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
digitalWrite(cs1pin, LOW);
digitalWrite(cs2pin, HIGH);
digitalWrite(cs3pin, LOW);
digitalWrite(cs4pin, LOW);
digitalWrite(cs5pin, LOW);
newData = false;
}
}