-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arduino Debugger.c
69 lines (62 loc) · 2.25 KB
/
Arduino Debugger.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Define the GPIO pins you want to control
const int gpio_pins[] = {2, 3, 4}; // Replace with the actual GPIO pins you want to use
void setup() {
// Initialize GPIO pins as outputs
for (int i = 0; i < sizeof(gpio_pins) / sizeof(gpio_pins[0]); i++) {
pinMode(gpio_pins[i], OUTPUT);
digitalWrite(gpio_pins[i], LOW); // Set pins to LOW (OFF) initially
}
// Initialize Serial communication at 9600 baud rate
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
// Read the incoming message until a newline character is received
String message = Serial.readStringUntil('\n');
message.trim(); // Remove leading/trailing whitespace
// Check if the message starts with "ON "
if (message.startsWith("ON ")) {
// Extract the pin number from the message and convert it to an integer
int pin_number = message.substring(3).toInt();
// Check if the pin number is valid
if (isValidPin(pin_number)) {
// Turn on the specified GPIO pin
digitalWrite(pin_number, HIGH);
// Send "OK" back to the sender
Serial.println("OK");
} else {
// Send "Invalid Command" if the pin is not in the list
Serial.println("Invalid Command");
}
}
// Check if the message starts with "OFF "
else if (message.startsWith("OFF ")) {
// Extract the pin number from the message and convert it to an integer
int pin_number = message.substring(4).toInt();
// Check if the pin number is valid
if (isValidPin(pin_number)) {
// Turn off the specified GPIO pin
digitalWrite(pin_number, LOW);
// Send "OK" back to the sender
Serial.println("OK");
} else {
// Send "Invalid Command" if the pin is not in the list
Serial.println("Invalid Command");
}
}
// If the message format is not recognized
else {
Serial.println("Invalid Command");
}
}
}
// Function to check if a pin number is valid
bool isValidPin(int pin) {
for (int i = 0; i < sizeof(gpio_pins) / sizeof(gpio_pins[0]); i++) {
// Compare the pin number with the elements in the gpio_pins array
if (gpio_pins[i] == pin) {
return true; // The pin is valid
}
}
return false; // The pin is not valid
}