Skip to content

Check pin mode is set correctly for analog read and digital write #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/src.ino
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void processCommand() {

if (current_arg.equals("*IDN?")) {
// CMD: *IDN?
Serial.print("SourceBots:Arduino:X:2.0\n");
Serial.print("SourceBots:Arduino:X:2.1\n");
return;
}

Expand Down Expand Up @@ -125,6 +125,16 @@ 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");
}
return;
}
Comment on lines +130 to +137
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Remove the outer if to make it clearer what's happening.

Same below.

Suggested change
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");
}
return;
}
if (mode == INPUT) {
Serial.print("NACK:Digital write is not supported in INPUT\n");
return;
} else if (mode == INPUT_PULLUP) {
Serial.print("NACK:Digital write is not supported in INPUT_PULLUP\n");
return;
}

if (current_arg.equals("1")) {
// CMD: PIN:<n>:DIGITAL:SET:1
digitalWrite(pin, HIGH);
Expand All @@ -144,6 +154,16 @@ 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");
}
return;
}
Serial.print(analogRead(pin));
Serial.print("\n");
return;
Expand Down
Loading