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 20:48:48
- ********* 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 <nRF24L01.h>
- #include <RF24.h>
- #include <Servo.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- // Define the pins for the NRF24L01 module
- #define CE_PIN 9
- #define CSN_PIN 10
- // Create an RF24 object
- RF24 radio(CE_PIN, CSN_PIN);
- // Define the servo motor
- Servo myServo;
- // Define the switch pin
- const int switchPin = 2; // Pin for the toggle switch
- bool lastSwitchState = LOW; // Last state of the switch
- bool currentSwitchState; // Current state of the switch
- // Define the servo position
- int servoPosition = 0; // Start position for the servo
- void setup(void)
- {
- // Initialize the serial communication
- Serial.begin(9600);
- // Initialize the servo
- myServo.attach(3); // Attach the servo to pin 3
- // Initialize the NRF24L01
- radio.begin();
- radio.openWritingPipe(0xF0F0F0F0E1LL); // Set the address for the transmitter
- radio.setPALevel(RF24_PA_HIGH); // Set power level
- radio.setChannel(76); // Set the channel
- radio.startListening(); // Start listening for incoming data
- // Set the switch pin as input
- pinMode(switchPin, INPUT);
- }
- void loop(void)
- {
- // Read the current state of the switch
- currentSwitchState = digitalRead(switchPin);
- // Check if the switch has been toggled
- if (currentSwitchState == HIGH && lastSwitchState == LOW) {
- // Send a signal via NRF24L01
- const char text[] = "Toggle"; // Message to send
- radio.stopListening(); // Stop listening to send data
- radio.write(&text, sizeof(text)); // Send the message
- radio.startListening(); // Start listening again
- // Move the servo slowly
- for (servoPosition = 0; servoPosition <= 180; servoPosition++) {
- myServo.write(servoPosition); // Move to the current position
- delay(15); // Wait for the servo to reach the position
- }
- for (servoPosition = 180; servoPosition >= 0; servoPosition--) {
- myServo.write(servoPosition); // Move to the current 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