Advertisement
pleasedontcode

**Servo Control** rev_01

May 5th, 2025
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Servo Control**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-05-05 17:50:47
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Tengo   2 arduino nano  2 NRF24L01  1 SWITCH */
  21.     /* TOGGLE  1 SERVOMOTOR   Quiero armar un paso a */
  22.     /* nivel que al accionar el switch toggle envie la */
  23.     /* seƱal y el servomotor se accione lenta y */
  24.     /* progresivamente */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <SPI.h>
  31. #include <RF24.h>
  32. #include <Servo.h>
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37.  
  38. // Define pins for NRF24L01
  39. #define CE_PIN   9
  40. #define CSN_PIN  10
  41.  
  42. // Create instances of the libraries
  43. RF24 radio(CE_PIN, CSN_PIN);
  44. Servo myServo;
  45.  
  46. // Define the switch pin
  47. const int switchPin = 2; // Pin where the switch is connected
  48. bool lastSwitchState = LOW; // Previous state of the switch
  49.  
  50. void setup(void)
  51. {
  52.     // Initialize serial communication
  53.     Serial.begin(9600);
  54.    
  55.     // Initialize the servo
  56.     myServo.attach(3); // Attach the servo to pin 3
  57.     myServo.write(0); // Start position of the servo
  58.  
  59.     // Initialize the NRF24 radio
  60.     radio.begin();
  61.     radio.openWritingPipe(0xF0F0F0F0E1LL); // Set the address for writing
  62.     radio.setPALevel(RF24_PA_HIGH); // Set power level
  63.     radio.stopListening(); // Stop listening to send data
  64.  
  65.     // Set the switch pin as input
  66.     pinMode(switchPin, INPUT);
  67. }
  68.  
  69. void loop(void)
  70. {
  71.     // Read the current state of the switch
  72.     bool currentSwitchState = digitalRead(switchPin);
  73.  
  74.     // Check if the switch has been toggled
  75.     if (currentSwitchState == HIGH && lastSwitchState == LOW) {
  76.         // Send a signal via NRF24
  77.         const char text[] = "Toggle Signal";
  78.         radio.write(&text, sizeof(text));
  79.         Serial.println("Signal sent!");
  80.  
  81.         // Move the servo slowly to 90 degrees
  82.         for (int pos = 0; pos <= 90; pos++) {
  83.             myServo.write(pos); // Move to the position
  84.             delay(15); // Wait for the servo to reach the position
  85.         }
  86.        
  87.         // Move the servo back to 0 degrees
  88.         for (int pos = 90; pos >= 0; pos--) {
  89.             myServo.write(pos); // Move to the position
  90.             delay(15); // Wait for the servo to reach the position
  91.         }
  92.     }
  93.  
  94.     // Update the last switch state
  95.     lastSwitchState = currentSwitchState;
  96. }
  97.  
  98. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement