Unit 17: Moving Menace
Code :
#include // Include the servo library
// Define pin numbers
const int servoPin = 9; // Pin for servo signal
const int laserPin = 10; // Pin for laser
const int buttonPin = 2; // Pin for the push button
Servo myServo; // Create a servo object
// Variables to track button state
bool buttonPressed = false;
unsigned long buttonPressTime = 0;
const unsigned long holdThreshold = 2000; // 2 seconds delay before action
bool actionTriggered = false; // Ensures only one action per press
void setup() {
myServo.attach(servoPin);
pinMode(laserPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor for button
digitalWrite(laserPin, LOW); // Start with laser off
myServo.write(0); // Ensure servo starts at 0 degrees
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) { // Button is pressed
if (!buttonPressed) {
buttonPressed = true;
buttonPressTime = millis(); // Record the time of press
actionTriggered = false; // Reset action flag for new press
}
} else if (buttonPressed) {
// Button was released, reset flag but do not trigger action immediately
buttonPressed = false;
}
// If 2 seconds have passed since button press, trigger the action once
if (buttonPressed && !actionTriggered && millis() - buttonPressTime >= holdThreshold) {
actionTriggered = true; // Prevent multiple activations per press
digitalWrite(laserPin, HIGH); // Turn on laser
moveServo(); // Move servo from 0 to 180 and back
digitalWrite(laserPin, LOW); // Turn off laser
}
}
// Function to move the servo from 0 to 180 and back
void moveServo() {
for (int pos = 0; pos <= 180; pos += 5) {
myServo.write(pos);
delay(20);
}
for (int pos = 180; pos >= 0; pos -= 5) {
myServo.write(pos);
delay(20);
}
}
Code (serial output for troubleshooting):
#include // Include the servo library
// Define pin numbers
const int servoPin = 9; // Pin for servo signal
const int laserPin = 10; // Pin for laser
const int buttonPin = 2; // Pin for the push button
Servo myServo; // Create a servo object
// Variables to track button state
bool buttonPressed = false;
unsigned long buttonPressTime = 0;
const unsigned long holdThreshold = 2000; // 2 seconds delay before action
bool actionTriggered = false; // Ensures only one action per press
void setup() {
Serial.begin(9600); // Start serial communication
myServo.attach(servoPin);
pinMode(laserPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor for button
digitalWrite(laserPin, LOW); // Start with laser off
myServo.write(0); // Ensure servo starts at 0 degrees
Serial.println("System Ready");
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) { // Button is pressed
if (!buttonPressed) {
buttonPressed = true;
buttonPressTime = millis(); // Record the time of press
actionTriggered = false; // Reset action flag for new press
Serial.println("Button Pressed");
}
} else if (buttonPressed) {
// Button was released, reset flag but do not trigger action immediately
buttonPressed = false;
Serial.println("Button Released");
}
// If 2 seconds have passed since button press, trigger the action once
if (buttonPressed && !actionTriggered && millis() - buttonPressTime >= holdThreshold) {
actionTriggered = true; // Prevent multiple activations per press
Serial.println("2 Seconds Passed - Activating Sequence");
digitalWrite(laserPin, HIGH); // Turn on laser
Serial.println("Laser ON");
moveServo(); // Move servo from 0 to 180 and back
digitalWrite(laserPin, LOW); // Turn off laser
Serial.println("Laser OFF");
}
}
// Function to move the servo from 0 to 180 and back
void moveServo() {
Serial.println("Moving Servo: 0 to 180");
for (int pos = 0; pos <= 180; pos += 5) {
myServo.write(pos);
delay(20);
}
Serial.println("Moving Servo: 180 to 0");
for (int pos = 180; pos >= 0; pos -= 5) {
myServo.write(pos);
delay(20);
}
Serial.println("Servo Movement Complete");
}