/********* Pleasedontcode.com **********
Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- Terms and Conditions:
You have a non-exclusive, revocable, worldwide, royalty-free license
for personal and commercial use. Attribution is optional; modifications
are allowed, but you're responsible for code maintenance. We're not
liable for any loss or damage. For full terms,
please visit pleasedontcode.com/termsandconditions.
- Project: "Servo Control"
- Source Code compiled for: Arduino Nano
- Source Code created on: 2024-03-30 19:58:42
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* servo go 90 degres if button is presed? led on */
/****** SYSTEM REQUIREMENT 2 *****/
/* When the push button is pressed, the servo motor */
/* should rotate to 90 degrees and the LED should */
/* turn on. If button not pressed servo go 180 */
/* degrees after 2 seconds and led off */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include // https://github.com/arduino-libraries/Servo
#include // https://github.com/evert-arias/EasyButton
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void onPressed(void); // Function prototype for button press event
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t button_PushButton_PIN_D2 = 2;
/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t led_LED_PIN_D4 = 4;
/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t servo_Servomotor_PWMSignal_PIN_D3 = 3;
/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool led_LED_PIN_D4_rawData = false;
uint8_t servo_Servomotor_PWMSignal_PIN_D3_rawData = 0;
/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float led_LED_PIN_D4_phyData = 0.0;
float servo_Servomotor_PWMSignal_PIN_D3_phyData = 0.0;
/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Servo myservo; // Servo object instance
EasyButton button(button_PushButton_PIN_D2); // EasyButton object instance
void setup(void)
{
// put your setup code here, to run once:
myservo.attach(servo_Servomotor_PWMSignal_PIN_D3); // Attaching servo to the PWM pin
pinMode(button_PushButton_PIN_D2, INPUT_PULLUP);
pinMode(led_LED_PIN_D4, OUTPUT);
pinMode(servo_Servomotor_PWMSignal_PIN_D3, OUTPUT);
button.begin(); // Initialize the EasyButton library
button.onPressed(onPressed); // Set the onPressed callback function for the button press event
}
void loop(void)
{
// put your main code here, to run repeatedly:
updateOutputs(); // Refresh output data
button.read(); // Read the button state
// Servo control based on button state
if (button.isPressed()) {
myservo.write(90); // Rotate servo to 90 degrees
digitalWrite(led_LED_PIN_D4, HIGH); // Turn on the LED
} else {
delay(2000); // Wait for 2 seconds
myservo.write(180); // Rotate servo to 180 degrees
digitalWrite(led_LED_PIN_D4, LOW); // Turn off the LED
}
}
void updateOutputs()
{
digitalWrite(led_LED_PIN_D4, led_LED_PIN_D4_rawData);
analogWrite(servo_Servomotor_PWMSignal_PIN_D3, servo_Servomotor_PWMSignal_PIN_D3_rawData);
}
void onPressed()
{
// Button press event handler
led_LED_PIN_D4_rawData = !led_LED_PIN_D4_rawData; // Toggle the LED state
}