speakerSwitch/Arduino/speakerSwitch.ino

82 lines
2.0 KiB
C++

#include "Arduino.h"
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void setup() {
Serial.begin(9600);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);
Serial.println("mc8051.de Speaker Switch");
}
void loop() {
recvWithEndMarker();
if (newData == true) {
newData = false;
if (strcmp(receivedChars, "ch00-l-0") == 0) {
digitalWrite(A0, LOW);
} else if (strcmp(receivedChars, "ch00-l-1") == 0) {
digitalWrite(A0, HIGH);
} else if (strcmp(receivedChars, "ch00-r-0") == 0) {
digitalWrite(A3, LOW);
} else if (strcmp(receivedChars, "ch00-r-1") == 0) {
digitalWrite(A3, HIGH);
} else if (strcmp(receivedChars, "ch01-l-0") == 0) {
digitalWrite(A1, LOW);
} else if (strcmp(receivedChars, "ch01-l-1") == 0) {
digitalWrite(A1, HIGH);
} else if (strcmp(receivedChars, "ch01-r-0") == 0) {
digitalWrite(A4, LOW);
} else if (strcmp(receivedChars, "ch01-r-1") == 0) {
digitalWrite(A4, HIGH);
} else if (strcmp(receivedChars, "ch02-l-0") == 0) {
digitalWrite(A2, LOW);
} else if (strcmp(receivedChars, "ch02-l-1") == 0) {
digitalWrite(A2, HIGH);
} else if (strcmp(receivedChars, "ch02-r-0") == 0) {
digitalWrite(A5, LOW);
} else if (strcmp(receivedChars, "ch02-r-1") == 0) {
digitalWrite(A5, HIGH);
} else {
Serial.print("Unknown command ");
Serial.println(receivedChars);
}
}
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
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;
}
}
}