Panel Cookies
Arduino turret CODE
Help me by sharing this post

This is the code for the Arduino based servo turret. Make sure you also install the needed library for NRF24 radio module. Download that from below. Install the library, compile and uplaod the code to the Arduino. Downlaod the zip file or copy+paste from below. Read all comments in the code!





schematic arduino servo turret



/*  Connect a NRF24 module: 
    Module // Arduino UNO
    
    GND    ->   GND
    Vcc    ->   3.3V
    CE     ->   D9
    CSN    ->   D10
    CLK    ->   D13
    MOSI   ->   D11
    MISO   ->   D12
Tutorial: https://www.electronoobs.com/eng_arduino_tut89.php
Schematic: https://www.electronoobs.com/eng_arduino_tut89_sch1.php
*/

#include <SPI.h>
#include <nRF24L01.h>   //Download:  https://www.electronoobs.com/eng_arduino_NRF24_lib.php
#include <RF24.h>
#include <Servo.h>  //To create PWM signals we need this lybrary

const uint64_t pipeIn = 0xE8E8F0F0E1LL; //Remember that this code is the same as in the transmitter
RF24 radio(9, 10); 

//We could use up to 32 channels
struct MyData {
byte throttle; //We define each byte of data input, in this case just 6 channels
byte yaw;
byte pitch;
byte roll;
byte AUX1;
byte AUX2;
};

MyData data;

void resetData()
{
//We define the inicial value of each data input
//3 potenciometers will be in the middle position so 127 is the middle from 254
data.throttle = 0;
data.yaw = 127;
data.pitch = 127;
data.roll = 127;
data.AUX1 = 0;
data.AUX2 = 0;
}

Servo channel_1;
Servo channel_2;
int ch1_value = 0;
int ch2_value = 0;

int PWM_PLATE = 90;
int PWM_ARM = 90;
int DELAY = 20;
int FIRE = 3;
int RELAY = 4;
bool fire_state = false;
int fire_delay = 500;


/**************************************************/

void setup()
{
//Attach the servo signal on pins from D2 to D8
channel_1.attach(6);
channel_2.attach(5);

pinMode(FIRE,OUTPUT);
pinMode(RELAY,OUTPUT);
  
Serial.begin(250000); //Set the speed to 9600 bauds if you want.
//You should always have the same speed selected in the serial monitor
resetData();
radio.begin();
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);

radio.openReadingPipe(1,pipeIn);
//we start the radio comunication
radio.startListening();

}

/**************************************************/

unsigned long lastRecvTime = 0;

void recvData()
{
while ( radio.available() ) {
radio.read(&data, sizeof(MyData));
lastRecvTime = millis(); //here we receive the data
}
}

/**************************************************/

void loop()
{
recvData();
unsigned long now = millis();
//Here we check if we've lost signal, if we did we reset the values 
if ( now - lastRecvTime > 1000 ) { 
// Signal lost?
resetData();
}


//Decide the way to rotate the servos according to received data
if(data.roll < 90 && data.pitch < 90)
{
  PWM_PLATE = PWM_PLATE + 1;  
  PWM_ARM = PWM_ARM - 1;
  delay(DELAY);
  PWM_PLATE = constrain(PWM_PLATE, 0, 180);
  PWM_ARM = constrain(PWM_ARM, 0, 180);
}
if(data.roll < 90 && data.pitch > 90 && data.pitch < 150)
{
  PWM_PLATE = PWM_PLATE + 1;   
  delay(DELAY);
  PWM_PLATE = constrain(PWM_PLATE, 0, 180);  
}
if(data.roll < 90 && data.pitch > 150)
{
  PWM_PLATE = PWM_PLATE + 1;  
  PWM_ARM = PWM_ARM + 1;
  delay(DELAY);
  PWM_PLATE = constrain(PWM_PLATE, 0, 180);
  PWM_ARM = constrain(PWM_ARM, 0, 180);
}
if(data.roll > 90 && data.roll < 150 && data.pitch < 90)
{   
  PWM_ARM = PWM_ARM - 1;
  delay(DELAY); 
  PWM_ARM = constrain(PWM_ARM, 0, 180);
}
if(data.roll > 90 && data.roll < 150 && data.pitch > 150)
{   
  PWM_ARM = PWM_ARM + 1;
  delay(DELAY); 
  PWM_ARM = constrain(PWM_ARM, 0, 180);
}
if(data.roll > 150 && data.pitch < 90)
{   
  PWM_PLATE = PWM_PLATE - 1;
  PWM_ARM = PWM_ARM - 1; 
  delay(DELAY); 
  PWM_PLATE = constrain(PWM_PLATE, 0, 180);
  PWM_ARM = constrain(PWM_ARM, 0, 180);
}
if(data.roll > 150 && data.pitch > 90 && data.pitch < 150)
{   
  PWM_PLATE = PWM_PLATE - 1;  
  delay(DELAY); 
  PWM_PLATE = constrain(PWM_PLATE, 0, 180); 
}
if(data.roll > 150 && data.pitch > 150)
{   
  PWM_PLATE = PWM_PLATE - 1; 
  PWM_ARM = PWM_ARM + 1;   
  delay(DELAY); 
  PWM_PLATE = constrain(PWM_PLATE, 0, 180); 
  PWM_ARM = constrain(PWM_ARM, 0, 180);
}


//Enable or not the nichrome wire
if(data.AUX2 == 1)
{  
  digitalWrite(RELAY,HIGH);
}
else
{
  digitalWrite(RELAY,LOW);
  digitalWrite(FIRE,LOW);
}
if(data.AUX1 == 1 && data.AUX2 == 1 && !fire_state)
{  
  digitalWrite(FIRE,HIGH);
  fire_state = true;
  delay(fire_delay);
  digitalWrite(FIRE,LOW);
}
if(data.AUX1 == 0)
{  
  digitalWrite(FIRE,LOW);
  fire_state = false;
}


//Create PWM signals for servos
channel_1.write(PWM_PLATE);  
channel_2.write(PWM_ARM); 


}

/**************************************************/













yt_link
insta_link
fb_link
twitter_link

Code


Affiliate Disclosure

ADVERTISERS



PCBWAY PCB service





Curso Arduino Online nivel bajo