Skip to content
Snippets Groups Projects
Commit 78a409cf authored by Ethan Walters's avatar Ethan Walters
Browse files

Added minor comments to Arduino sketches

parent 58653905
No related branches found
No related tags found
No related merge requests found
/***************************************************************************
* Sketch Name: arduino_usb_servo_buttons
*
* Updated Version: 01/04/2025
*
* Description:
* - USB-controlled servo motor.
* - Receives "SERVO X" to control a servo at any angle (0-180).
* - Includes two Grove LED buttons that send serial messages when pressed.
***************************************************************************/
// Include the Servo library
#include <Servo.h>
// Define pin for Servo
#define SERVO_PIN 5 // 🔌 Grove PWM D5 (Servo motor)
#define SERVO_PIN 5 // Grove Servo (D5)
// Define pins for Grove LED Buttons
// The grove buttons must be connected to the pin below the Arduino pin number
// They are unique in this case, and don't work like other Grove modules
// For example, if the button is connected to D3, the pin number is 4:
#define BUTTON1_PIN 4 // Grove Button 1 (D3)
#define BUTTON2_PIN 7 // Grove Button 2 (D6)
// Create Servo object
Servo myservo;
void setup()
{
Serial.begin(9600); // Debugging serial monitor
myservo.attach(SERVO_PIN); // Attach servo motor to pin 5
myservo.write(90); // Set initial position to 90 degrees
void setup() {
Serial.begin(9600); // Start serial communication at 9600 baud
myservo.attach(SERVO_PIN); // Attach servo motor to the specified pin
myservo.write(90); // Set initial position to 90 degrees, the middle position
// Initialize button pins as input
pinMode(BUTTON1_PIN, INPUT_PULLUP);
......@@ -35,7 +27,8 @@ void setup()
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
digitalWrite(LED1_PIN, LOW); // Ensure LEDs start off
// Ensure LEDs start in an off state (LOW)
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
Serial.println("System Ready");
......@@ -43,36 +36,38 @@ void setup()
void loop()
{
// **Check for USB serial commands from Node-RED**
// Check for USB serial commands from Node-RED (or any other source)
if (Serial.available()) {
String command = Serial.readStringUntil('\n'); // Read full command
String command = Serial.readStringUntil('\n'); // Read full command (commands end with '\n' by default)
command.trim(); // Remove whitespace
Serial.println("Received: " + command); // Debugging
Serial.println("Received: " + command); // Serial output for debugging
// **Check if the command starts with "SERVO "** (e.g., "SERVO 90")
// Check if the command starts with "SERVO " (e.g., "SERVO 90")
// This is the expected format for controlling the servo
if (command.startsWith("SERVO ")) {
int angle = command.substring(6).toInt(); // Extract angle value
// **Ensure the angle is between 0 and 180**
// Ensure the angle is between 0 and 180
if (angle >= 0 && angle <= 180) {
myservo.write(angle); // Move servo to specified angle
myservo.write(angle); // Move servo to specified angle
Serial.print("Servo moved to: ");
Serial.println(angle);
} else {
// Return an error message if the angle is invalid, this will be sent to Node-RED
Serial.println("Invalid angle! Must be 0-180.");
}
}
}
// **Check button presses and send messages**
// Check button presses and send messages
if (digitalRead(BUTTON1_PIN) == HIGH) {
Serial.println("BUTTON1 PRESSED");
delay(300); // Debounce delay
delay(300); // Debounce delay to prevent multiple readings
}
if (digitalRead(BUTTON2_PIN) == HIGH) {
Serial.println("BUTTON2 PRESSED");
delay(300); // Debounce delay
delay(300); // Debounce delay to prevent multiple readings
}
}
// Include the Servo library
#include <Servo.h>
// Define pins
#define PIR_MOTION_SENSOR 2
#define SERVO_PIN 5
// Create Servo object
Servo myservo;
// Motion sensing control flag
bool motionSensingEnabled = true;
void setup()
{
Serial.begin(9600);
pinMode(PIR_MOTION_SENSOR, INPUT);
myservo.attach(SERVO_PIN);
myservo.write(90);
void setup() {
Serial.begin(9600); // Start serial communication at 9600 baud
pinMode(PIR_MOTION_SENSOR, INPUT); // Attach PIR motion sensor to specified pin
myservo.attach(SERVO_PIN); // Attach servo motor to the specified pin
myservo.write(90); // Set initial position to 90 degrees, the middle position
}
void loop()
{
// Check for USB commands
void loop() {
// Check for USB commands from Node-RED (or any other source)
if (Serial.available()) {
String command = Serial.readStringUntil('\n');
command.trim();
String command = Serial.readStringUntil('\n'); // Read full command (commands end with '\n' by default)
command.trim(); // Remove whitespace
// Handle servo command
// Check if the command starts with "SERVO " (e.g., "SERVO 90")
// This is the expected format for controlling the servo
if (command.startsWith("SERVO ")) {
int angle = command.substring(6).toInt();
int angle = command.substring(6).toInt(); // Extract angle value
// Ensure the angle is between 0 and 180
if (angle >= 0 && angle <= 180) {
myservo.write(angle);
myservo.write(angle); // Move servo to specified angle
Serial.print("Servo moved to: ");
Serial.println(angle);
} else {
// Return an error message if the angle is invalid, this will be sent to Node-RED
Serial.println("Invalid angle! Must be 0-180.");
}
}
// Handle motion sensing control
// Handle motion sensing control commands
// Check if the command is "STOP SENSING" or "START SENSING"
// Then enable or disable motion sensing accordingly
// This allows motion sensing to be turned off from Node-Red
else if (command == "STOP SENSING") {
motionSensingEnabled = false;
Seial.println("Motion sensing disabled.");
}
else if (command == "START SENSING") {
motionSensingEnabled = true;
Serial.println("Motion sensing enabled.");
}
}
// Check PIR only if sensing is enabled
if (motionSensingEnabled && digitalRead(PIR_MOTION_SENSOR)) {
Serial.println("Movement");
delay(1000); // Simple debounce
delay(1000); // Debounce delay to prevent multiple readings
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment