Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* 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 NOT compiled for: Arduino Uno
- - Source Code created on: 2025-05-05 17:50:47
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Tengo 2 arduino nano 2 NRF24L01 1 SWITCH */
- /* TOGGLE 1 SERVOMOTOR Quiero armar un paso a */
- /* nivel que al accionar el switch toggle envie la */
- /* seƱal y el servomotor se accione lenta y */
- /* progresivamente */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <SPI.h>
- #include <RF24.h>
- #include <Servo.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- // Define pins for NRF24L01
- #define CE_PIN 9
- #define CSN_PIN 10
- // Create instances of the libraries
- RF24 radio(CE_PIN, CSN_PIN);
- Servo myServo;
- // Define the switch pin
- const int switchPin = 2; // Pin where the switch is connected
- bool lastSwitchState = LOW; // Previous state of the switch
- void setup(void)
- {
- // Initialize serial communication
- Serial.begin(9600);
- // Initialize the servo
- myServo.attach(3); // Attach the servo to pin 3
- myServo.write(0); // Start position of the servo
- // Initialize the NRF24 radio
- radio.begin();
- radio.openWritingPipe(0xF0F0F0F0E1LL); // Set the address for writing
- radio.setPALevel(RF24_PA_HIGH); // Set power level
- radio.stopListening(); // Stop listening to send data
- // Set the switch pin as input
- pinMode(switchPin, INPUT);
- }
- void loop(void)
- {
- // Read the current state of the switch
- bool currentSwitchState = digitalRead(switchPin);
- // Check if the switch has been toggled
- if (currentSwitchState == HIGH && lastSwitchState == LOW) {
- // Send a signal via NRF24
- const char text[] = "Toggle Signal";
- radio.write(&text, sizeof(text));
- Serial.println("Signal sent!");
- // Move the servo slowly to 90 degrees
- for (int pos = 0; pos <= 90; pos++) {
- myServo.write(pos); // Move to the position
- delay(15); // Wait for the servo to reach the position
- }
- // Move the servo back to 0 degrees
- for (int pos = 90; pos >= 0; pos--) {
- myServo.write(pos); // Move to the position
- delay(15); // Wait for the servo to reach the position
- }
- }
- // Update the last switch state
- lastSwitchState = currentSwitchState;
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement