Skip to content

Commit

Permalink
Check pin mode is set correctly for analog read and digital write
Browse files Browse the repository at this point in the history
  • Loading branch information
WillB97 committed Aug 6, 2024
1 parent abb7f39 commit 94e76df
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/src.ino
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ void processCommand() {
}
else if (current_arg.equals("SET")) {
getSlice();
// Check if the pin is in the correct mode
int mode = getPinMode(pin);
if (mode != OUTPUT) {
if (mode == INPUT) {
Serial.print("NACK:Digital write is not supported in INPUT\n");
} else {
Serial.print("NACK:Digital write is not supported in INPUT_PULLUP\n");
}
Serial.print("\n");
return;
}
if (current_arg.equals("1")) {
// CMD: PIN:<n>:DIGITAL:SET:1
digitalWrite(pin, HIGH);
Expand All @@ -144,6 +155,17 @@ void processCommand() {
getSlice();
if (current_arg.equals("GET?")) {
// CMD: PIN:<n>:ANALOG:GET?
// Check if the pin is in the correct mode
int mode = getPinMode(pin);
if (mode != INPUT) {
if (mode == OUTPUT) {
Serial.print("NACK:Analog read is not supported in OUTPUT\n");
} else {
Serial.print("NACK:Analog read is not supported in INPUT_PULLUP\n");
}
Serial.print("\n");
return;
}
Serial.print(analogRead(pin));
Serial.print("\n");
return;
Expand Down

0 comments on commit 94e76df

Please sign in to comment.