Advertisement
pleasedontcode

**Servo Control** rev_02

May 5th, 2025
334
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 20:48:48
  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 <nRF24L01.h>
  32. #include <RF24.h>
  33. #include <Servo.h>
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38.  
  39. // Define the pins for the NRF24L01 module
  40. #define CE_PIN   9
  41. #define CSN_PIN  10
  42.  
  43. // Create an RF24 object
  44. RF24 radio(CE_PIN, CSN_PIN);
  45.  
  46. // Define the servo motor
  47. Servo myServo;
  48.  
  49. // Define the switch pin
  50. const int switchPin = 2; // Pin for the toggle switch
  51. bool lastSwitchState = LOW; // Last state of the switch
  52. bool currentSwitchState; // Current state of the switch
  53.  
  54. // Define the servo position
  55. int servoPosition = 0; // Start position for the servo
  56.  
  57. void setup(void)
  58. {
  59.     // Initialize the serial communication
  60.     Serial.begin(9600);
  61.    
  62.     // Initialize the servo
  63.     myServo.attach(3); // Attach the servo to pin 3
  64.  
  65.     // Initialize the NRF24L01
  66.     radio.begin();
  67.     radio.openWritingPipe(0xF0F0F0F0E1LL); // Set the address for the transmitter
  68.     radio.setPALevel(RF24_PA_HIGH); // Set power level
  69.     radio.setChannel(76); // Set the channel
  70.     radio.startListening(); // Start listening for incoming data
  71.  
  72.     // Set the switch pin as input
  73.     pinMode(switchPin, INPUT);
  74. }
  75.  
  76. void loop(void)
  77. {
  78.     // Read the current state of the switch
  79.     currentSwitchState = digitalRead(switchPin);
  80.  
  81.     // Check if the switch has been toggled
  82.     if (currentSwitchState == HIGH && lastSwitchState == LOW) {
  83.         // Send a signal via NRF24L01
  84.         const char text[] = "Toggle"; // Message to send
  85.         radio.stopListening(); // Stop listening to send data
  86.         radio.write(&text, sizeof(text)); // Send the message
  87.         radio.startListening(); // Start listening again
  88.  
  89.         // Move the servo slowly
  90.         for (servoPosition = 0; servoPosition <= 180; servoPosition++) {
  91.             myServo.write(servoPosition); // Move to the current position
  92.             delay(15); // Wait for the servo to reach the position
  93.         }
  94.         for (servoPosition = 180; servoPosition >= 0; servoPosition--) {
  95.             myServo.write(servoPosition); // Move to the current position
  96.             delay(15); // Wait for the servo to reach the position
  97.         }
  98.     }
  99.  
  100.     // Update the last switch state
  101.     lastSwitchState = currentSwitchState;
  102. }
  103.  
  104. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement