/********* 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: "Sensor-Driven Actions"
- Source Code NOT compiled for: ESP32 DevKit V1
- Source Code created on: 2024-06-17 10:33:34
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* Rotate servo1 to 4 from 0 to 180 degree, when */
/* pushbutto light led for 200ms, if ir1 to ir4 > 50% */
/* rotate servo 1 to servo 4 to 0 degree */
/****** SYSTEM REQUIREMENT 2 *****/
/* Rotate Servo1 from 0 to 180 degrees when */
/* LaserButton (D4) is pressed. Light up Laser1 LED */
/* (D16) for 200ms if IR1 (D14) and IR2 (D26) */
/* readings are above 50%. Then, reset Servo1 to 0 */
/* degrees. */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include //https://github.com/evert-arias/EasyButton
#include //https://github.com/deneyapkart/deneyap-servo-arduino-library
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void onLaserButtonPressed(); // Function prototype for button press callback
void updateOutputs(); // Function prototype for updating outputs
void rotateServo(int start, int end); // Function prototype for rotating servo
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t LaserButton_PushButton_PIN_D4 = 4;
/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t IR1_Potentiometer_Vout_PIN_D14 = 14;
const uint8_t IR2_Potentiometer_Vout_PIN_D26 = 26;
/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t Laser1_LED_PIN_D16 = 16;
/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t Servo1_Servomotor_PWMSignal_PIN_D13 = 13;
/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool Laser1_LED_PIN_D16_rawData = 0;
uint8_t Servo1_Servomotor_PWMSignal_PIN_D13_rawData = 0;
/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float Laser1_LED_PIN_D16_phyData = 0.0;
float Servo1_Servomotor_PWMSignal_PIN_D13_phyData = 0.0;
/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton laserButton(LaserButton_PushButton_PIN_D4); // Initialize EasyButton instance
Servo myservo; // Initialize Servo instance
void setup(void)
{
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LaserButton_PushButton_PIN_D4, INPUT_PULLUP);
pinMode(IR1_Potentiometer_Vout_PIN_D14, INPUT);
pinMode(IR2_Potentiometer_Vout_PIN_D26, INPUT);
pinMode(Laser1_LED_PIN_D16, OUTPUT);
pinMode(Servo1_Servomotor_PWMSignal_PIN_D13, OUTPUT);
laserButton.begin(); // Initialize the button
laserButton.onPressed(onLaserButtonPressed); // Set the callback for button press
myservo.attach(Servo1_Servomotor_PWMSignal_PIN_D13); // Attach the servo to pin D13
}
void loop(void)
{
// put your main code here, to run repeatedly:
laserButton.read(); // Read the button state
updateOutputs(); // Refresh output data
}
void onLaserButtonPressed() {
Serial.println("Laser button pressed");
// Read IR sensor values
int ir1Value = analogRead(IR1_Potentiometer_Vout_PIN_D14);
int ir2Value = analogRead(IR2_Potentiometer_Vout_PIN_D26);
// Check if IR sensor values are above 50%
if (ir1Value > 2048 && ir2Value > 2048) {
// Light up Laser1 LED for 200ms
digitalWrite(Laser1_LED_PIN_D16, HIGH);
delay(200);
digitalWrite(Laser1_LED_PIN_D16, LOW);
// Rotate Servo1 from 0 to 180 degrees
rotateServo(0, 180);
delay(200); // Wait for 200ms
// Reset Servo1 to 0 degrees
rotateServo(180, 0);
}
}
void updateOutputs()
{
digitalWrite(Laser1_LED_PIN_D16, Laser1_LED_PIN_D16_rawData);
myservo.write(Servo1_Servomotor_PWMSignal_PIN_D13_rawData); // Update servo position
}
void rotateServo(int start, int end) {
if (start < end) {
for (int pos = start; pos <= end; pos += 15) {
myservo.write(pos);
delay(100);
}
} else {
for (int pos = start; pos >= end; pos -= 15) {
myservo.write(pos);
delay(100);
}
}
}
/* END CODE */