0% encontró este documento útil (0 votos)
97 vistas24 páginas

Control de Motores DC con Arduino PWM

Este documento describe cómo controlar el movimiento de un automóvil robot mediante la transmisión inalámbrica de señales de un joystick a través de módulos NRF24L01. Se explica el código para el transmisor (joystick) y el receptor (automóvil robot), incluida la lectura de los ejes X e Y del joystick, el mapeo de los valores a la velocidad de los motores y el control de la dirección mediante la variación independiente de la velocidad de cada motor.

Cargado por

Isabella Sanchez
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como TXT, PDF, TXT o lee en línea desde Scribd
0% encontró este documento útil (0 votos)
97 vistas24 páginas

Control de Motores DC con Arduino PWM

Este documento describe cómo controlar el movimiento de un automóvil robot mediante la transmisión inalámbrica de señales de un joystick a través de módulos NRF24L01. Se explica el código para el transmisor (joystick) y el receptor (automóvil robot), incluida la lectura de los ejes X e Y del joystick, el mapeo de los valores a la velocidad de los motores y el control de la dirección mediante la variación independiente de la velocidad de cada motor.

Cargado por

Isabella Sanchez
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como TXT, PDF, TXT o lee en línea desde Scribd

/************************************************probar pwm para motr dc circuito

puente h con rele y velocidad irfz44n


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

/*
CREADO POR : ELECTROALL
FACEBOOK : https://www.facebook.com/ChecharlsElEctroall/
P�GINA : http://che-charls-electroall.webnode.es/
TWITTER : https://twitter.com/ChCharlsELECTRO
________________________________________________________
{==[=======> (CONTROL MOTOR) <=======]==}
________________________________________________________
*/
int Motor_IZQ = 6; // declaramos las variables de tipo entero
int Motor_DER = 5;
int entrada_analogica= A0;

void setup() {
pinMode (Motor_IZQ, OUTPUT); // designamos los pines como pines de
salida y entrada
pinMode (Motor_DER, OUTPUT);
pinMode (entrada_analogica, INPUT); // entrada
}

void loop() { //entramos a un siclo repetitivo

int valor_entrada_analogica =0; // declaramos las variables de tipo entero


int valor_salida_pwm=0; // declaramos la variable de tipo
entero para la ulizacion de la modulacion por ancho de pulso
// ojo ( estos pines tienes que ser pines de
(PWM)

valor_entrada_analogica = analogRead(entrada_analogica); // Guardamos la lectura


analogica en la variable (valor_entrada_anal�gica)
valor_salida_pwm =map(valor_entrada_analogica,0, 1023,0,255); // guardamos el
mapeo en la variable (valor salida)
analogWrite(Motor_IZQ, valor_salida_pwm); //
manifestamos en el led la lectura analogica que hemos reducido de( 0 - 255)
analogWrite(Motor_DER, valor_salida_pwm);
// donde 0 es 0 voltios
y 255 viene ser 5 voltios

/************************************************Arduino PS2 Joystick


Tutorial*************************************************/

// Henry's Bench
// Module KY023

int Xin= A0; // X Input Pin


int Yin = A1; // Y Input Pin
int KEYin = 3; // Push Button

void setup ()
{
pinMode (KEYin, INPUT);
Serial.begin (9600);
}
void loop ()
{
int xVal, yVal, buttonVal;

xVal = analogRead (Xin);


yVal = analogRead (Yin);
buttonVal = digitalRead (KEYin);

Serial.print("X = ");
Serial.println (xVal, DEC);

Serial.print ("Y = ");


Serial.println (yVal, DEC);

Serial.print("Button is ");
if (buttonVal == HIGH){
Serial.println ("not pressed");
}
else{
Serial.println ("PRESSED");
}

delay (500);
}

/*************************************** 1. Arduino Robot Car Wireless Control


using the NRF24L01 Transceiver module

-------transmisor
/*
Arduino Robot Car Wireless Control using the NRF24L01 Transceiver module
== Transmitter - Joystick ==
by Dejan Nedelkovski, www.HowToMechatronics.com
Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(8, 9); // CE, CSN
const byte address[6] = "00001";
char xyData[32] = "";
String xAxis, yAxis;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop() {

xAxis = String(analogRead(A0)); // Read Joysticks X-axis


yAxis = String(analogRead(A1)); // Read Joysticks Y-axis
// X value
xAxis.toCharArray(xyData, 5); // Put the String (X Value) into a character
array
radio.write(&xyData, sizeof(xyData)); // Send the array data (X value) to the
other NRF24L01 modile
// Y value
yAxis.toCharArray(xyData, 5);
radio.write(&xyData, sizeof(xyData));
delay(20);
}

-------------receptor

/*
Arduino Robot Car Wireless Control using the NRF24L01 Transceiver module
== Receiver - Arduino robot car ==
by Dejan Nedelkovski, www.HowToMechatronics.com
Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define enA 2 // Note: Pin 9 in previous video ( pin 10 is used for the SPI
communication of the NRF24L01)
#define in1 4
#define in2 5
#define enB 3 // Note: Pin 10 in previous video
#define in3 6
#define in4 7
RF24 radio(8, 9); // CE, CSN
const byte address[6] = "00001";
char receivedData[32] = "";
int xAxis, yAxis;
int motorSpeedA = 0;
int motorSpeedB = 0;
void setup() {
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if (radio.available()) { // If the NRF240L01 module received data
radio.read(&receivedData, sizeof(receivedData)); // Read the data and put
it into character array
xAxis = atoi(&receivedData[0]); // Convert the data from the character
array (received X value) into integer
delay(10);
radio.read(&receivedData, sizeof(receivedData));
yAxis = atoi(&receivedData[0]);
delay(10);
}

// Y-axis used for forward and backward control


if (yAxis < 470) {
// Set Motor A backward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B backward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Convert the declining Y-axis readings for going backward from 470 to 0
into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 470, 0, 0, 255);
motorSpeedB = map(yAxis, 470, 0, 0, 255);
}
else if (yAxis > 550) {
// Set Motor A forward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// Convert the increasing Y-axis readings for going forward from 550 to
1023 into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 550, 1023, 0, 255);
motorSpeedB = map(yAxis, 550, 1023, 0, 255);
}
// If joystick stays in middle the motors are not moving
else {
motorSpeedA = 0;
motorSpeedB = 0;
}
// X-axis used for left and right control
if (xAxis < 470) {
// Convert the declining X-axis readings from 470 to 0 into increasing 0 to
255 value
int xMapped = map(xAxis, 470, 0, 0, 255);
// Move to left - decrease left motor speed, increase right motor speed
motorSpeedA = motorSpeedA - xMapped;
motorSpeedB = motorSpeedB + xMapped;
// Confine the range from 0 to 255
if (motorSpeedA < 0) {
motorSpeedA = 0;
}
if (motorSpeedB > 255) {
motorSpeedB = 255;
}
}
if (xAxis > 550) {
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255
value
int xMapped = map(xAxis, 550, 1023, 0, 255);
// Move right - decrease right motor speed, increase left motor speed
motorSpeedA = motorSpeedA + xMapped;
motorSpeedB = motorSpeedB - xMapped;
// Confine the range from 0 to 255
if (motorSpeedA > 255) {
motorSpeedA = 255;
}
if (motorSpeedB < 0) {
motorSpeedB = 0;
}
}
// Prevent buzzing at low speeds (Adjust according to your motors. My motors
couldn't start moving if PWM value was below value of 70)
if (motorSpeedA < 70) {
motorSpeedA = 0;
}
if (motorSpeedB < 70) {
motorSpeedB = 0;
}
analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}

/*******************dos motores 1
joysti***************************************************/
https://howtomechatronics.com/tutorials/arduino/arduino-dc-motor-control-tutorial-
l298n-pwm-h-bridge/

/* Arduino DC Motor Control - PWM | H-Bridge | L298N


Example 02 - Arduino Robot Car Control
by Dejan Nedelkovski, www.HowToMechatronics.com
*/

#define enA 9
#define in1 4
#define in2 5
#define enB 10
#define in3 6
#define in4 7

int motorSpeedA = 0;
int motorSpeedB = 0;

void setup() {
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}

void loop() {
int xAxis = analogRead(A0); // Read Joysticks X-axis
int yAxis = analogRead(A1); // Read Joysticks Y-axis

// Y-axis used for forward and backward control


if (yAxis < 470) {
// Set Motor A backward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B backward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Convert the declining Y-axis readings for going backward from 470 to 0 into
0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 470, 0, 0, 255);
motorSpeedB = map(yAxis, 470, 0, 0, 255);
}
else if (yAxis > 550) {
// Set Motor A forward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// Convert the increasing Y-axis readings for going forward from 550 to 1023
into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 550, 1023, 0, 255);
motorSpeedB = map(yAxis, 550, 1023, 0, 255);
}
// If joystick stays in middle the motors are not moving
else {
motorSpeedA = 0;
motorSpeedB = 0;
}

// X-axis used for left and right control


if (xAxis < 470) {
// Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255
value
int xMapped = map(xAxis, 470, 0, 0, 255);
// Move to left - decrease left motor speed, increase right motor speed
motorSpeedA = motorSpeedA - xMapped;
motorSpeedB = motorSpeedB + xMapped;
// Confine the range from 0 to 255
if (motorSpeedA < 0) {
motorSpeedA = 0;
}
if (motorSpeedB > 255) {
motorSpeedB = 255;
}
}
if (xAxis > 550) {
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
int xMapped = map(xAxis, 550, 1023, 0, 255);
// Move right - decrease right motor speed, increase left motor speed
motorSpeedA = motorSpeedA + xMapped;
motorSpeedB = motorSpeedB - xMapped;
// Confine the range from 0 to 255
if (motorSpeedA > 255) {
motorSpeedA = 255;
}
if (motorSpeedB < 0) {
motorSpeedB = 0;
}
}
// Prevent buzzing at low speeds (Adjust according to your motors. My motors
couldn't start moving if PWM value was below value of 70)
if (motorSpeedA < 70) {
motorSpeedA = 0;
}
if (motorSpeedB < 70) {
motorSpeedB = 0;
}
analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}

/**************control 1 motor y puetao con


potenciometro**********************************************/

/* Arduino DC Motor Control - PWM | H-Bridge | L298N - Example 01


by Dejan Nedelkovski, www.HowToMechatronics.com
*/
#define enA 9
#define in1 6
#define in2 7
#define button 4
int rotDirection = 0;
int pressed = false;
void setup() {
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(button, INPUT);
// Set initial rotation direction
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}
void loop() {
int potValue = analogRead(A0); // Read potentiometer value
int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Map the potentiometer
value from 0 to 255
analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin
// Read button - Debounce
if (digitalRead(button) == true) {
pressed = !pressed;
}
while (digitalRead(button) == true);
delay(20);
// If button is pressed - change rotation direction
if (pressed == true & rotDirection == 0) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
rotDirection = 1;
delay(20);
}
// If button is pressed - change rotation direction
if (pressed == false & rotDirection == 1) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
rotDirection = 0;
delay(20);
}
}

/**************************Arduino Motor Control and PWM Signal with L298N H-bridge


Motor Driver ******************************************/
const int IN1 = 7;
const int IN2 = 6;

const int IN3 = 5;


const int IN4 = 4;

//const int ENA = 9;


//const int ENB = 3;

void setup() {

pinMode (IN1, OUTPUT);


pinMode (IN2, OUTPUT);

pinMode (IN3, OUTPUT);


pinMode (IN4, OUTPUT);

//pinMode (ENA, OUTPUT);


//pinMode (ENB, OUTPUT);
// put your setup code here, to run once:

void loop() {

//analogWrite(ENA, 255);
//analogWrite(ENB, 255);

digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);

digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);

// put your main code here, to run repeatedly:

/**************************DC Motor Control Tutorial � L298N | PWM


***********************************************************************//
//https://howtomechatronics.com/tutorials/arduino/arduino-dc-motor-control-
tutorial-l298n-pwm-h-bridge/

/* Arduino DC Motor Control - PWM | H-Bridge | L298N - Example 01

by Dejan Nedelkovski, www.HowToMechatronics.com


*/

#define enA 9
#define in1 6
#define in2 7
#define button 4

int rotDirection = 0;
int pressed = false;
void setup() {
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(button, INPUT);
// Set initial rotation direction
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}

void loop() {
int potValue = analogRead(A0); // Read potentiometer value
int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Map the potentiometer value
from 0 to 255
analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin

// Read button - Debounce


if (digitalRead(button) == true) {
pressed = !pressed;
}
while (digitalRead(button) == true);
delay(20);

// If button is pressed - change rotation direction


if (pressed == true & rotDirection == 0) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
rotDirection = 1;
delay(20);
}
// If button is pressed - change rotation direction
if (pressed == false & rotDirection == 1) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
rotDirection = 0;
delay(20);
}
}

/******************************Arduino DC Motor Speed and Direction Control using


Relays and MOSFET***************************************************

https://circuitdigest.com/microcontroller-projects/arduino-dc-motor-speed-
direction-control

int x;
int y;
void setup()
{
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(6,OUTPUT);
pinMode(A0,INPUT);
}
void loop()
{
x=analogRead(A0);
y=map(x,0,1023,0,255);
analogWrite(6,y);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
}

/*************************dos motores 1
joystic**************************************************************/

/* Arduino DC Motor Control - PWM | H-Bridge | L298N


Example 02 - Arduino Robot Car Control
by Dejan Nedelkovski, www.HowToMechatronics.com
*/
#define enA 9
#define in1 4
#define in2 5
#define enB 10
#define in3 6
#define in4 7
int motorSpeedA = 0;
int motorSpeedB = 0;
void setup() {
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop() {
int xAxis = analogRead(A0); // Read Joysticks X-axis
int yAxis = analogRead(A1); // Read Joysticks Y-axis
// Y-axis used for forward and backward control
if (yAxis < 470) {
// Set Motor A backward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B backward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Convert the declining Y-axis readings for going backward from 470 to 0
into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 470, 0, 0, 255);
motorSpeedB = map(yAxis, 470, 0, 0, 255);
}
else if (yAxis > 550) {
// Set Motor A forward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// Convert the increasing Y-axis readings for going forward from 550 to
1023 into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 550, 1023, 0, 255);
motorSpeedB = map(yAxis, 550, 1023, 0, 255);
}
// If joystick stays in middle the motors are not moving
else {
motorSpeedA = 0;
motorSpeedB = 0;
}
// X-axis used for left and right control
if (xAxis < 470) {
// Convert the declining X-axis readings from 470 to 0 into increasing 0 to
255 value
int xMapped = map(xAxis, 470, 0, 0, 255);
// Move to left - decrease left motor speed, increase right motor speed
motorSpeedA = motorSpeedA - xMapped;
motorSpeedB = motorSpeedB + xMapped;
// Confine the range from 0 to 255
if (motorSpeedA < 0) {
motorSpeedA = 0;
}
if (motorSpeedB > 255) {
motorSpeedB = 255;
}
}
if (xAxis > 550) {
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255
value
int xMapped = map(xAxis, 550, 1023, 0, 255);
// Move right - decrease right motor speed, increase left motor speed
motorSpeedA = motorSpeedA + xMapped;
motorSpeedB = motorSpeedB - xMapped;
// Confine the range from 0 to 255
if (motorSpeedA > 255) {
motorSpeedA = 255;
}
if (motorSpeedB < 0) {
motorSpeedB = 0;
}
}
// Prevent buzzing at low speeds (Adjust according to your motors. My motors
couldn't start moving if PWM value was below value of 70)
if (motorSpeedA < 70) {
motorSpeedA = 0;
}
if (motorSpeedB < 70) {
motorSpeedB = 0;
}
analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}

/*******************************joystick
anal�gico****************************************************

GND - GND
Vcc - 5v
VRx - A0
VRy - A1
SW - D9
*/

const int pinLED = 13;


const int pinJoyX = A0;
const int pinJoyY = A1;
const int pinJoyButton = 9;

void setup() {
pinMode(pinJoyButton , INPUT_PULLUP); //activar resistencia pull up
Serial.begin(9600);
}

void loop() {
int Xvalue = 0;
int Yvalue = 0;
bool buttonValue = false;

//leer valores
Xvalue = analogRead(pinJoyX);
delay(100); //es necesaria una peque�a pausa entre lecturas
anal�gicas
Yvalue = analogRead(pinJoyY);
buttonValue = digitalRead(pinJoyButton);

//mostrar valores por serial


Serial.print("X:" );
Serial.print(Xvalue);
Serial.print(" | Y: ");
Serial.print(Yvalue);
Serial.print(" | Pulsador: ");
Serial.println(buttonValue);
delay(1000);
}

/
***********************************************************************************
*********************/
/*
nRF24L01+ Joystick Transmitter
nrf24l01-joy-xmit-demo.ino
nRF24L01+ Transmitter with Joystick
Use with Joystick Receiver Demo
DroneBot Workshop 2018
https://dronebotworkshop.com
*/

// Include RadioHead ReliableDatagram & NRF24 Libraries


#include <RHReliableDatagram.h>
#include <RH_NRF24.h>

// Include dependant SPI Library


#include <SPI.h>

// Define Joystick Connections


#define JoyStick_X_PIN A0
#define JoyStick_Y_PIN A1
// Define addresses for radio channels
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2

// Create an instance of the radio driver


RH_NRF24 RadioDriver;

// Sets the radio driver to NRF24 and the client address to 1


RHReliableDatagram RadioManager(RadioDriver, CLIENT_ADDRESS);

// Declare unsigned 8-bit joystick array


uint8_t joystick[3];

// Define the Message Buffer


uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

void setup()
{
// Setup Serial Monitor
Serial.begin(9600);

// Initialize RadioManager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!RadioManager.init())
Serial.println("init failed");
}

void loop()
{
// Print to Serial Monitor
Serial.println("Reading joystick values ");

// Read Joystick values and map to values of 0 - 255


joystick[0] = map(analogRead(JoyStick_X_PIN), 0, 1023, 0, 255);
joystick[1] = map(analogRead(JoyStick_Y_PIN), 0, 1023, 0, 255);
joystick[2] = 100;

//Display the joystick values in the serial monitor.


Serial.println("-----------");
Serial.print("x:");
Serial.println(joystick[0]);
Serial.print("y:");
Serial.println(joystick[1]);

Serial.println("Sending Joystick data to nrf24_reliable_datagram_server");

//Send a message containing Joystick data to manager_server


if (RadioManager.sendtoWait(joystick, sizeof(joystick), SERVER_ADDRESS))
{
// Now wait for a reply from the server
uint8_t len = sizeof(buf);
uint8_t from;
if (RadioManager.recvfromAckTimeout(buf, &len, 2000, &from))
{
Serial.print("got reply from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
}
else
{
Serial.println("No reply, is nrf24_reliable_datagram_server running?");
}
}
else
Serial.println("sendtoWait failed");

delay(100); // Wait a bit before next transmission


}

/
***********************************************************************************
*************************
//https://dronebotworkshop.com/dc-motors-l298n-h-bridge/

/*
L298N Motor Control Demonstration with Joystick
L298N-Motor-Control-Demo-Joystick.ino
Demonstrates use of Joystick control with Arduino and L298N Motor Controller

DroneBot Workshop 2017


http://dronebotworkshop.com
*/

// Motor A

int enA = 9;
int in1 = 8;
int in2 = 7;

// Motor B

int enB = 3;
int in3 = 5;
int in4 = 4;

// Joystick Input

int joyVert = A0; // Vertical


int joyHorz = A1; // Horizontal

// Motor Speed Values - Start at zero

int MotorSpeed1 = 0;
int MotorSpeed2 = 0;

// Joystick Values - Start at 512 (middle position)

int joyposVert = 512;


int joyposHorz = 512;

void setup()

{
// Set all the motor control pins to outputs

pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

// Start with motors disabled and direction forward

// Motor A

digitalWrite(enA, LOW);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

// Motor B

digitalWrite(enB, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

void loop() {

// Read the Joystick X and Y positions

joyposVert = analogRead(joyVert);
joyposHorz = analogRead(joyHorz);

// Determine if this is a forward or backward motion


// Do this by reading the Verticle Value
// Apply results to MotorSpeed and to Direction

if (joyposVert < 460)


{
// This is Backward

// Set Motor A backward

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);

// Set Motor B backward

digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

//Determine Motor Speeds

// As we are going backwards we need to reverse readings

joyposVert = joyposVert - 460; // This produces a negative number


joyposVert = joyposVert * -1; // Make the number positive

MotorSpeed1 = map(joyposVert, 0, 460, 0, 255);


MotorSpeed2 = map(joyposVert, 0, 460, 0, 255);
}
else if (joyposVert > 564)
{
// This is Forward

// Set Motor A forward

digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

// Set Motor B forward

digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

//Determine Motor Speeds

MotorSpeed1 = map(joyposVert, 564, 1023, 0, 255);


MotorSpeed2 = map(joyposVert, 564, 1023, 0, 255);

}
else
{
// This is Stopped

MotorSpeed1 = 0;
MotorSpeed2 = 0;

// Now do the steering


// The Horizontal position will "weigh" the motor speed
// Values for each motor

if (joyposHorz < 460)


{
// Move Left

// As we are going left we need to reverse readings

joyposHorz = joyposHorz - 460; // This produces a negative number


joyposHorz = joyposHorz * -1; // Make the number positive

// Map the number to a value of 255 maximum

joyposHorz = map(joyposHorz, 0, 460, 0, 255);

MotorSpeed1 = MotorSpeed1 - joyposHorz;


MotorSpeed2 = MotorSpeed2 + joyposHorz;

// Don't exceed range of 0-255 for motor speeds

if (MotorSpeed1 < 0)MotorSpeed1 = 0;


if (MotorSpeed2 > 255)MotorSpeed2 = 255;

}
else if (joyposHorz > 564)
{
// Move Right

// Map the number to a value of 255 maximum

joyposHorz = map(joyposHorz, 564, 1023, 0, 255);

MotorSpeed1 = MotorSpeed1 + joyposHorz;


MotorSpeed2 = MotorSpeed2 - joyposHorz;

// Don't exceed range of 0-255 for motor speeds

if (MotorSpeed1 > 255)MotorSpeed1 = 255;


if (MotorSpeed2 < 0)MotorSpeed2 = 0;

// Adjust to prevent "buzzing" at very low speed

if (MotorSpeed1 < 8)MotorSpeed1 = 0;


if (MotorSpeed2 < 8)MotorSpeed2 = 0;

// Set the motor speeds

analogWrite(enA, MotorSpeed1);
analogWrite(enB, MotorSpeed2);

/*****************************************nrf24l01-wireless-
joystick************************************************************/
//https://dronebotworkshop.com/nrf24l01-wireless-joystick/

/*
nRF24L01+ Joystick Transmitter
nrf24l01-joy-xmit-car.ino
nRF24L01+ Transmitter with Joystick for Robot Car
Use with Joystick Receiver for Robot Car
DroneBot Workshop 2018
https://dronebotworkshop.com
*/

// Include RadioHead ReliableDatagram & NRF24 Libraries


#include <RHReliableDatagram.h>
#include <RH_NRF24.h>

// Include dependant SPI Library


#include <SPI.h>

// Define Joystick Connections


#define joyVert A0
#define joyHorz A1

// Define Joystick Values - Start at 512 (middle position)


int joyposVert = 512;
int joyposHorz = 512;
// Define addresses for radio channels
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2

// Create an instance of the radio driver


RH_NRF24 RadioDriver;

// Sets the radio driver to NRF24 and the client address to 1


RHReliableDatagram RadioManager(RadioDriver, CLIENT_ADDRESS);

// Declare unsigned 8-bit motorcontrol array


// 2 Bytes for motor speeds plus 1 byte for direction control
uint8_t motorcontrol[3];

// Define the Message Buffer


uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

void setup()
{
// Setup Serial Monitor
Serial.begin(9600);

// Initialize RadioManager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!RadioManager.init())
Serial.println("init failed");

// Set initial motor direction as forward


motorcontrol[2] = 0;

void loop()
{
// Print to Serial Monitor
Serial.println("Reading motorcontrol values ");

// Read the Joystick X and Y positions


joyposVert = analogRead(joyVert);
joyposHorz = analogRead(joyHorz);

// Determine if this is a forward or backward motion


// Do this by reading the Verticle Value
// Apply results to MotorSpeed and to Direction

if (joyposVert < 460)


{
// This is Backward
// Set Motors backward
motorcontrol[2] = 1;

//Determine Motor Speeds


// As we are going backwards we need to reverse readings
motorcontrol[0] = map(joyposVert, 460, 0, 0, 255);
motorcontrol[1] = map(joyposVert, 460, 0, 0, 255);

}
else if (joyposVert > 564)
{
// This is Forward
// Set Motors forward
motorcontrol[2] = 0;

//Determine Motor Speeds


motorcontrol[0] = map(joyposVert, 564, 1023, 0, 255);
motorcontrol[1] = map(joyposVert, 564, 1023, 0, 255);

}
else
{
// This is Stopped
motorcontrol[0] = 0;
motorcontrol[1] = 0;
motorcontrol[2] = 0;

// Now do the steering


// The Horizontal position will "weigh" the motor speed
// Values for each motor

if (joyposHorz < 460)


{
// Move Left
// As we are going left we need to reverse readings
// Map the number to a value of 255 maximum
joyposHorz = map(joyposHorz, 460, 0, 0, 255);

motorcontrol[0] = motorcontrol[0] - joyposHorz;


motorcontrol[1] = motorcontrol[1] + joyposHorz;

// Don't exceed range of 0-255 for motor speeds


if (motorcontrol[0] < 0)motorcontrol[0] = 0;
if (motorcontrol[1] > 255)motorcontrol[1] = 255;

}
else if (joyposHorz > 564)
{
// Move Right
// Map the number to a value of 255 maximum
joyposHorz = map(joyposHorz, 564, 1023, 0, 255);

motorcontrol[0] = motorcontrol[0] + joyposHorz;


motorcontrol[1] = motorcontrol[1] - joyposHorz;

// Don't exceed range of 0-255 for motor speeds


if (motorcontrol[0] > 255)motorcontrol[0] = 255;
if (motorcontrol[1] < 0)motorcontrol[1] = 0;

// Adjust to prevent "buzzing" at very low speed


if (motorcontrol[0] < 8)motorcontrol[0] = 0;
if (motorcontrol[1] < 8)motorcontrol[1] = 0;

//Display the Motor Control values in the serial monitor.


Serial.print("Motor A: ");
Serial.print(motorcontrol[0]);
Serial.print(" - Motor B: ");
Serial.print(motorcontrol[1]);
Serial.print(" - Direction: ");
Serial.println(motorcontrol[2]);

//Send a message containing Motor Control data to manager_server


if (RadioManager.sendtoWait(motorcontrol, sizeof(motorcontrol), SERVER_ADDRESS))
{
// Now wait for a reply from the server
uint8_t len = sizeof(buf);
uint8_t from;
if (RadioManager.recvfromAckTimeout(buf, &len, 2000, &from))
{
Serial.print("got reply from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
}
else
{
Serial.println("No reply, is nrf24_reliable_datagram_server running?");
}
}
else
Serial.println("sendtoWait failed");

delay(100); // Wait a bit before next transmission


}

------------------------------rceptor--------------

/*
nRF24L01+ Joystick Receiver for Robot Car
nrf24l01-joy-rcv-car.ino
nRF24L01+ Receiver and L298N driver for Robot Car
Use with Joystick Transmitter for Robot Car
DroneBot Workshop 2018
https://dronebotworkshop.com
*/

// Include RadioHead ReliableDatagram & NRF24 Libraries


#include <RHReliableDatagram.h>
#include <RH_NRF24.h>

// Include dependant SPI Library


#include <SPI.h>

// Define addresses for radio channels


#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2

// Motor A Connections
int enA = 9;
int in1 = 14;
int in2 = 4;

// Motor B Connections
int enB = 5;
int in3 = 7;
int in4 = 6;

// Create an instance of the radio driver


RH_NRF24 RadioDriver;

// Sets the radio driver to NRF24 and the server address to 2


RHReliableDatagram RadioManager(RadioDriver, SERVER_ADDRESS);

// Define a message to return if values received


uint8_t ReturnMessage[] = "JoyStick Data Received";

// Define the Message Buffer


uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

void setup()
{
// Setup Serial Monitor
Serial.begin(9600);

// Set all the motor control pins to outputs


pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

// Initialize RadioManager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!RadioManager.init())
Serial.println("init failed");
}

void loop()
{
if (RadioManager.available())
{
// Wait for a message addressed to us from the client
uint8_t len = sizeof(buf);
uint8_t from;
if (RadioManager.recvfromAck(buf, &len, &from))

//Serial Print the values of joystick


//Serial.print("got request from : 0x");
//Serial.print(from, HEX);
//Serial.print(": MotorA = ");
//Serial.print(buf[0]);
//Serial.print(" MotorB = ");
//Serial.print(buf[1]);
//Serial.print(" Dir = ");
//Serial.println(buf[2]);

// Set Motor Direction


if (buf[2] == 1)
{
// Motors are backwards
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}else{
// Motors are forwards
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}

// Drive Motors
analogWrite(enA, buf[1]);
analogWrite(enB, buf[0]);

// Send a reply back to the originator client, check for error


if (!RadioManager.sendtoWait(ReturnMessage, sizeof(ReturnMessage), from))
Serial.println("sendtoWait failed");
}
}
}

/*******************************L298N Motor
Demonstration****************************************************

/*
L298N Motor Demonstration
L298N-Motor-Demo.ino
Demonstrates functions of L298N Motor Controller

DroneBot Workshop 2017


http://dronebotworkshop.com
*/

// Motor A

int enA = 9;
int in1 = 8;
int in2 = 7;

// Motor B

int enB = 3;
int in3 = 5;
int in4 = 4;

void setup()

// Set all the motor control pins to outputs


pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}

void demoOne()

// This function will run the motors in both directions at a fixed speed
// Turn on motor A

digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

// Set speed to 200 out of possible range 0~255

analogWrite(enA, 200);

// Turn on motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

// Set speed to 200 out of possible range 0~255

analogWrite(enB, 200);

delay(2000);

// Now change motor directions

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

delay(2000);

// Now turn off motors

digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);

void demoTwo()

// This function will run the motors across the range of possible speeds
// Note that maximum speed is determined by the motor itself and the operating
voltage

// Turn on motors

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// Accelerate from zero to maximum speed

for (int i = 0; i < 256; i++)


{

analogWrite(enA, i);
analogWrite(enB, i);

delay(20);

// Decelerate from maximum speed to zero

for (int i = 255; i >= 0; --i)

analogWrite(enA, i);
analogWrite(enB, i);

delay(20);

// Now turn off motors

digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);

void loop()

demoOne();

delay(1000);

demoTwo();

delay(1000);

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

También podría gustarte