salut
je suis un debutant en arduino et j'ai un petit projet a realiser c'est transeformer un jouet tractopelle en RC . POUR ça je besoin de 2 module transmittion nrf24l01, un pont H l293 , 3 joystik ,2 petit servomoteur ,et un dc moteur ,et en cherchant sur la toile je trouver un code pour ça mais le probleme que le code trouvé manque le servomoteur .
donc est ce que il y a quelqu'un qui peut m'aider a ajouter la commande servo dans le code sachant que je essayer pas mal de fois mais j'arrive pas .
voici le code tx
/*
Controller Code
*/
// Define variables
int Joy_X = 0x00;
int Joy_Y = 0x00;
int Joy_X_Old = 0x00;
int Joy_Y_Old = 0x00;
int Data_Check_Timer = 0;
int Active_Vehicle = 0;
int Num_Vehicle = 1;
int Controller_Mode = 0;
int A_Button = LOW;
int B_Button = LOW;
int C_Button = LOW;
int D_Button = LOW;
int E_Button = LOW;
int F_Button = LOW;
int Joy_Button = LOW;
int X_Hold = 0;
int Y_Hold = 0;
#include <SPI.h>
#include "RF24.h"
RF24 radio(9,10);
// Controller Address
const uint64_t pipe = 0xE8E8F0F0E1LL;
/-----( Declare Variables )-----/
uint8_t command[8]; // 2 element array of unsigned 8-bit type
void setup()
{
pinMode(2, INPUT_PULLUP); // A Button
pinMode(3, INPUT_PULLUP); // B Button
pinMode(4, INPUT_PULLUP); // C Button
pinMode(5, INPUT_PULLUP); // D Button
pinMode(6, INPUT_PULLUP); // E Button
pinMode(7, INPUT_PULLUP); // F Button
pinMode(8, INPUT_PULLUP); // Joystick button
radio.begin();
radio.openWritingPipe(pipe);
}
void loop() {
Check_Buttons();
Check_Pots();
Regular_Data_Check();
}
void Check_Buttons(){
// Check Joystick Switches
A_Button = digitalRead(2);
B_Button = digitalRead(3);
C_Button = digitalRead(4);
D_Button = digitalRead(5);
E_Button = digitalRead(6);
F_Button = digitalRead(7);
Joy_Button = digitalRead(8);
if (A_Button==LOW)
{
if (command[3] != 1){
command[3]=1; // Command to turn on magnet
}
else {
command[3]=0; // Command to turn off magnet
}
// Wait for switch to be released
while (A_Button == LOW)
{
A_Button = digitalRead(2);
}
Send_Data();
}
if (C_Button==LOW)
{
if (command[4] != 1){
command[4]=1; // Command to turn on lights
}
else {
command[4]=0; // Command to turn off lights
}
// Wait for switch to be released
while (C_Button == LOW)
{
C_Button = digitalRead(4);
}
Send_Data();
}
if (B_Button==LOW)
{
//Add B button code here
// Wait for switch to be released
while (B_Button == LOW)
{
B_Button = digitalRead(3);
}
Send_Data();
}
if (D_Button==LOW)
{
// Add D button code here
// Wait for switch to be released
while (D_Button == LOW)
{
D_Button = digitalRead(5);
}
Send_Data();
}
if (E_Button==LOW) //E button cycles through controller modes
{
if(Controller_Mode != 1){
Controller_Mode = 1; //Enter mode 1
}
else{
Controller_Mode = 0; //Enter mode 0
}
// Wait for switch to be released
while (E_Button == LOW)
{
E_Button = digitalRead(6);
}
Send_Data();
}
if (F_Button==LOW) //F button cycles through vehicles
{
// Add F button code here
// Wait for switch to be released
while (F_Button == LOW)
{
F_Button = digitalRead(7);
}
Send_Data();
}
if (Joy_Button==LOW)
{
// Add joystick button code here
// Wait for switch to be released
while (Joy_Button == LOW)
{
Joy_Button = digitalRead(8);
}
Send_Data();
}
}
void Check_Pots(){
Joy_X = analogRead(A0);
Joy_X= map(Joy_X, 0, 1024, 0, 255);
Joy_Y = analogRead(A1);
Joy_Y= map(Joy_Y, 0, 1024, 0, 255);
if(Joy_X != Joy_X_Old || Joy_Y != Joy_Y_Old){
if(Controller_Mode == 0){
command[0]=Joy_Y;
command[1]=Joy_X;
}
else{
command[2]=Joy_Y;
}
Send_Data();
Joy_X_Old=Joy_X;
Joy_Y_Old=Joy_Y;
}
}
// Function to send the data
void Send_Data(){
radio.write(command, sizeof(command));
Data_Check_Timer = 0;
}
// Send data in regular intervals to prevent tractor loosing control
void Regular_Data_Check(){
Data_Check_Timer=Data_Check_Timer+1;
if(Data_Check_Timer=1024){
Send_Data();
}
}
code rx
/*
Reciever Code
*/
// Variables used in the program
int Motor_1_Speed = 127;
int Motor_2_Speed = 127;
int Motor_3_Speed = 127;
int Magnet_State = 1;
int LED_State = 0;
int move1 = 0;
uint8_t buf[8];
#include <SPI.h>
#include "RF24.h"
RF24 radio(9,10);
// Controller Address
const uint64_t Controller_1 = 0xE8E8F0F0E1LL;
void setup()
{
pinMode(2, OUTPUT); // Motor 1 Direction 1
pinMode(3, OUTPUT); // Motor 1 PWM
pinMode(4, OUTPUT); // Motor 1 Direction 2
pinMode(5, OUTPUT); // Motor 2 PWM
pinMode(5, OUTPUT); // Motor 3 PWM
pinMode(7, OUTPUT); // Motor 2 Direction 1
pinMode(8, OUTPUT); // Motor 2 Direction 2
pinMode(A0, OUTPUT); // Motor 3 Direction 1
pinMode(A1, OUTPUT); // Motor 3 Direction 2
pinMode(A2, OUTPUT); // Magnet Pin
pinMode(A3, OUTPUT); // LED Pin
radio.begin(); // Initialize the NRF24 Radio Module
radio.openReadingPipe(1,Controller_1); // Set Address of Controller 1
radio.startListening(); // Start listening for commands from the controllers
}
void loop()
{
if (radio.available())radio.read(buf, 8);{
Motor_1_Speed = buf[0];
Motor_2_Speed = buf[1];
Motor_3_Speed = buf[2];
Magnet_State = buf[3];
LED_State = buf[4];
update_motor(Motor_1_Speed,3,2,4);
update_motor(Motor_2_Speed,5,7,8);
update_motor(Motor_3_Speed,6,A0,A1);
//Turn on or off the magnet
if(Magnet_State==1){
digitalWrite(A2,HIGH);
}
else{
digitalWrite(A2,LOW);
}
//Turn on or off the LEDs
if(LED_State==1){
digitalWrite(A3,HIGH);
}
else{
digitalWrite(A3,LOW);
}
}
}
void update_motor(int drive_val, int motor_pwm, int motor_dir1, int motor_dir2){
if (drive_val < 120){ // These if functions leave a buffer zone of 15 bits above and below the drive center value
move1 = map(drive_val, 120, 0, 0,255);//drive_min, drive_max); // scale the data value for use with the motor
analogWrite(motor_pwm,move1); // Set the PWM vlue on pin 5 to move the motor
digitalWrite(motor_dir1, HIGH); // Set the direction with pins 7 and 8
digitalWrite(motor_dir2, LOW);
}
else if (drive_val > 135){
move1 = map(drive_val, 135, 255, 0,255);//drive_min, drive_max); // scale the data value for use with the motor
analogWrite(motor_pwm,move1); // Set the PWM vlue on pin 5 to move the motor
digitalWrite(motor_dir2, HIGH); // Set the direction with pins 7 and 8
digitalWrite(motor_dir1, LOW);
}
else{
move1=0;
analogWrite(motor_pwm,move1); // Set the PWM vlue on pin 5 to move the motor
digitalWrite(motor_dir1, LOW); // Set the direction with pins 7 and 8
digitalWrite(motor_dir2, LOW);
}
}