|
Post by Deynox on Aug 26, 2015 12:13:52 GMT
Hi, I build my first RC tractor, but I want to use 433 MHz RF module. I have 2 Arduino Uno, 2 PS2 joystick and motor shield. So can I use your code to my project, and what I need to change in this code ? Please help
|
|
|
Post by Deynox on Aug 26, 2015 12:18:40 GMT
Sorry, I forgot to add one information. I use 1 DC motor and 2 servo.
|
|
|
Post by RC Tractor Guy on Aug 26, 2015 12:37:39 GMT
The code should be the same except for the radio control code. I'm not sure how you need to do that so I recommend you get code that sends a byte from one Arduino to another working separately first and then we can try to combine it into the final code which shouldn't be too difficult.
|
|
|
Post by Deynox on Aug 26, 2015 12:51:36 GMT
I find code for RF module, but I want to do some change like: https://gist.github.com/conoro/223035df942b72d2d2f6 https://gist.github.com/conoro/8430ec8753317883c2b7 - 1 joystick and 4 buttons change to 2 joystick - add 2 servo and 1 DC motor instead 4 DC motor Can you help me ? Please.
|
|
|
Post by RC Tractor Guy on Aug 26, 2015 13:08:58 GMT
You've tested that code to make sure it works with your setup? If that is the case then I think if you use the John Deere code here you just need to include the library and this code // VirtualWire Initialise the IO and ISR vw_set_ptt_inverted(true); // Required for DR3100 vw_setup(2000); // Bits per sec then change // Function to send the data void Send_Data(){ uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN]; uint8_t len = sizeof(buf); nrf24.send(command, sizeof(command)); nrf24.waitPacketSent(); Data_Check_Timer = 0; } in the controller code to // Function to send the data void Send_Data(){ // send string to receiver (actually just broadcast. No concept of 1-1 comms with these TX/RX modules vw_send((uint8_t *)command, strlen(command)); vw_wait_tx(); // Wait until the whole message is gone Data_Check_Timer = 0; } And for the tractor code you need to include the library and this code // Due to lack of access to Digital Pins with Motor Shield in place, using Analogue A0 (Arduino Pin 14) as Digital vw_set_rx_pin(14); // Initialise the IO and ISR vw_set_ptt_inverted(true); // Required for DR3100 vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running and change this code if (radio.available())radio.read(buf, 16); { to if (vw_get_message(buf, 16)) // Non-blocking { basically just copy the radio code for the new module into the tractor code and forget the rest. That is what I would try anyway.
|
|
|
Post by Deynox on Aug 26, 2015 13:14:22 GMT
Thank you very much, I try to do this.
|
|
|
Post by Deynox on Aug 26, 2015 16:13:01 GMT
Transmitter with joystick works fine but receiver does not react.
I set transmitter pin to 8, and receiver to 7 - it can be like that ?
// transmitter.pde
//
// Simple example of how to use VirtualWire to transmit messages
// Implements a simplex (one-way) transmitter with an TX-C1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@airspayce.com)
// Copyright (C) 2008 Mike McCauley
// $Id: transmitter.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $
#include <VirtualWire.h>
const int led_pin = 11;
const int transmit_pin = 8;
const int receive_pin = 7;
const int transmit_en_pin = 3;
// Store the Arduino pin associated with each input
// Select button is triggered when joystick is pressed
const byte PIN_BUTTON_SELECT = 2;
const byte PIN_BUTTON_RIGHT = 3;
const byte PIN_BUTTON_UP = 4;
const byte PIN_BUTTON_DOWN = 5;
const byte PIN_BUTTON_LEFT = 6;
const byte PIN_ANALOG_X = 0;
const byte PIN_ANALOG_Y = 1;
int x_position;
int y_position;
char msg[19]="X0000Y0000A0B0C0D0";
int led = 13;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
Serial.println("setup");
pinMode(PIN_BUTTON_RIGHT, INPUT);
digitalWrite(PIN_BUTTON_RIGHT, HIGH);
pinMode(PIN_BUTTON_LEFT, INPUT);
digitalWrite(PIN_BUTTON_LEFT, HIGH);
pinMode(PIN_BUTTON_UP, INPUT);
digitalWrite(PIN_BUTTON_UP, HIGH);
pinMode(PIN_BUTTON_DOWN, INPUT);
digitalWrite(PIN_BUTTON_DOWN, HIGH);
pinMode(PIN_BUTTON_SELECT, INPUT);
digitalWrite(PIN_BUTTON_SELECT, HIGH);
// VirtualWire Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
}
void loop() {
int thous;
int huns;
int tens;
int ones;
char digitsascii[5] = "0000";
x_position = analogRead(PIN_ANALOG_X);
y_position = analogRead(PIN_ANALOG_Y);
// Format is XnnnnYnnnnAnBnCnDn
// X is X position of joystick as 4 digits
// Y is Y position of joystick as 4 digits
// A is A button on joystick. 0= Not pressed. 1 = pressed
// B is B button on joystick. 0= Not pressed. 1 = pressed
// C is C button on joystick. 0= Not pressed. 1 = pressed
// D is D button on joystick. 0= Not pressed. 1 = pressed
//First convert X pos to 4 ascii characters (48 is decimal ASCII code for '0')
thous = x_position/1000;
msg[1]=thous+48;
huns = (x_position - (thous*1000))/100;
msg[2]=huns+48;
tens = (x_position - (thous*1000) - (huns*100))/10;
msg[3]=tens+48;
ones = x_position - (thous*1000) - (huns*100) - (tens*10);
msg[4]=ones+48;
//Then convert Y pos to 4 ascii characters
thous = y_position/1000;
msg[6]=thous+48;
huns = (y_position - (thous*1000))/100;
msg[7]=huns+48;
tens = (y_position - (thous*1000) - (huns*100))/10;
msg[8]=tens+48;
ones = y_position - (thous*1000) - (huns*100) - (tens*10);
msg[9]=ones+48;
// Will implement buttons laters
digitalWrite(led, HIGH);
Serial.println(msg);
// send string to receiver (actually just broadcast. No concept of 1-1 comms with these TX/RX modules
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone
delay(100);
digitalWrite(led, LOW);
delay(100);
}
// receiver.pde
//
// Simple example of how to use VirtualWire to receive messages
// Implements a simplex (one-way) receiver with an Rx-B1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@airspayce.com)
// Copyright (C) 2008 Mike McCauley
// $Id: receiver.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $
#include <VirtualWire.h>
#include <AFMotor.h>
const int led_pin = 13;
const int transmit_pin = 8;
const int receive_pin = 7;
const int transmit_en_pin = 3;
AF_DCMotor motor1(1, MOTOR12_1KHZ); // create motor #1, 1KHz pwm
AF_DCMotor motor2(2, MOTOR12_1KHZ); // create motor #2, 1KHz pwm
AF_DCMotor motor3(3, MOTOR34_1KHZ); // create motor #3, 1KHz pwm
AF_DCMotor motor4(4, MOTOR34_1KHZ); // create motor #4, 1KHz pwm
const int X_THRESHOLD_LOW = 505;
const int X_THRESHOLD_HIGH = 530;
const int Y_THRESHOLD_LOW = 500;
const int Y_THRESHOLD_HIGH = 510;
int x_position;
int y_position;
int x_direction;
int y_direction;
int current_x_direction;
int current_y_direction;
uint8_t motor1_speed;
uint8_t motor2_speed;
uint8_t motor3_speed;
uint8_t motor4_speed;
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
motor1_speed = 255;
motor2_speed = 255;
motor3_speed = 255;
motor4_speed = 255;
motor1.setSpeed(0); // set the speed to 200/255
motor2.setSpeed(0); // set the speed to 200/255
motor3.setSpeed(0); // set the speed to 200/255
motor4.setSpeed(0); // set the speed to 200/255
motor1.run(RELEASE); // turn it on going backwards
motor2.run(RELEASE); // turn it on going backwards
motor3.run(RELEASE); // turn it on going backwards
motor4.run(RELEASE); // turn it on going backwards
// Due to lack of access to Digital Pins with Motor Shield in place, using Analogue A0 (Arduino Pin 14) as Digital
vw_set_rx_pin(14);
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
current_x_direction = 0;
current_y_direction = 0;
x_direction = 0;
y_direction = 0;
Serial.println("setup done");
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
int j;
digitalWrite(13, true); // Flash a light to show received good message
// Message with a good checksum received, dump it.
Serial.print("Got: ");
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i], HEX);
Serial.print(" ");
}
Serial.println("");
digitalWrite(13, false);
// Format is XnnnnYnnnnAnBnCnDn
// X is X position of joystick as 4 digits
// Y is Y position of joystick as 4 digits
// A is A button on joystick. 0= Not pressed. 1 = pressed
// B is B button on joystick. 0= Not pressed. 1 = pressed
// C is C button on joystick. 0= Not pressed. 1 = pressed
// D is D button on joystick. 0= Not pressed. 1 = pressed
//Serial.println("");
x_position = (1000 * (buf[1] - 48)) + (100*(buf[2] - 48)) + (10*(buf[3] - 48)) + (buf[4] - 48);
y_position = (1000 * (buf[6] - 48)) + (100*(buf[7] - 48)) + (10*(buf[8] - 48)) + (buf[9] - 48);
Serial.println(x_position);
Serial.println(y_position);
// Forwards
if (y_position > 600){
y_direction = 1;
}
// Backwards
else if (y_position < 400){
y_direction = -1;
}
// Stop
else {
y_direction = 0;
}
// Now sort out turning
// Right Forwards
if ((x_position > 600) && (y_direction == 1)){
x_direction = 1;
motor1_speed = 125;
motor2_speed = 255;
motor3_speed = 255;
motor4_speed = 125;
Serial.println("Right Forwards");
}
// Left Forwards
else if ((x_position < 400) && (y_direction == 1)){
x_direction = -1;
motor1_speed = 255;
motor2_speed = 125;
motor3_speed = 125;
motor4_speed = 255;
Serial.println("Left Forwards");
}
// Straight Forwards
else if ((x_position > 400) && (x_position < 600) && (y_direction == 1)){
x_direction = 0;
motor1_speed = 255;
motor2_speed = 255;
motor3_speed = 255;
motor4_speed = 255;
Serial.println("Straight Forwards");
}
// Right Backwards
else if ((x_position > 600) && (y_direction == -1)){
x_direction = 1;
motor1_speed = 255;
motor2_speed = 125;
motor3_speed = 125;
motor4_speed = 255;
Serial.println("Right Backwards");
}
// Left Backwards
else if ((x_position < 400) && (y_direction == -1)){
x_direction = -1;
motor1_speed = 125;
motor2_speed = 255;
motor3_speed = 255;
motor4_speed = 125;
Serial.println("Left Backwards");
}
// Straight Backwards
else if ((x_position > 400) && (x_position < 600) && (y_direction == -1)){
x_direction = 0;
motor1_speed = 255;
motor2_speed = 255;
motor3_speed = 255;
motor4_speed = 255;
Serial.println("Straight Backwards");
}
// Stop
else {
x_direction = 0;
motor1_speed = 0;
motor2_speed = 0;
motor3_speed = 0;
motor4_speed = 0;
Serial.println("Stop");
}
if ((y_direction == 1) && (current_y_direction == 1)){
Serial.println("Still Forwards");
motor1.setSpeed(motor1_speed);
motor2.setSpeed(motor2_speed);
motor3.setSpeed(motor3_speed);
motor4.setSpeed(motor4_speed);
}
if ((y_direction == -1) && (current_y_direction == -1)){
Serial.println("Still Backwards");
motor1.setSpeed(motor1_speed);
motor2.setSpeed(motor2_speed);
motor3.setSpeed(motor3_speed);
motor4.setSpeed(motor4_speed);
}
else if ((y_direction == -1) && ((current_y_direction == 1) || (current_y_direction == 0))){
current_y_direction = -1;
motor1.setSpeed(motor1_speed);
motor2.setSpeed(motor2_speed);
motor3.setSpeed(motor3_speed);
motor4.setSpeed(motor4_speed);
motor1.run(BACKWARD); // turn it on going backwards
motor2.run(BACKWARD); // turn it on going backwards
motor3.run(BACKWARD); // turn it on going backwards
motor4.run(BACKWARD); // turn it on going backwards
Serial.println("Newly Backwards");
}
else if ((y_direction == 1) && ((current_y_direction == -1) || (current_y_direction == 0))){
current_y_direction = 1;
motor1.setSpeed(motor1_speed);
motor2.setSpeed(motor2_speed);
motor3.setSpeed(motor3_speed);
motor4.setSpeed(motor4_speed);
motor1.run(FORWARD); // turn it on going forwards
motor2.run(FORWARD); // turn it on going forwards
motor3.run(FORWARD); // turn it on going forwards
motor4.run(FORWARD); // turn it on going forwards
Serial.println("Newly Forwards");
}
else if (y_direction == 0){
current_y_direction = 0;
motor1.run(RELEASE); // turn it on going backwards
motor2.run(RELEASE); // turn it on going backwards
motor3.run(RELEASE); // turn it on going backwards
motor4.run(RELEASE); // turn it on going backwards
Serial.println("Stop");
}
}
}
|
|
|
Post by Deynox on Aug 26, 2015 16:39:58 GMT
|
|
|
Post by Deynox on Aug 26, 2015 18:20:28 GMT
I know why it doesn't work, I try with another simple program to send message and I must use only 12 and 11 pin otherwise it not work.
Sorry, for this spam but I can't edit post.
|
|
|
Post by RC Tractor Guy on Aug 26, 2015 18:29:20 GMT
Don't worry about it, I'm glad you got it to work. Hopefully the rest of your build works well.
|
|
|
Post by Deynox on Aug 26, 2015 18:36:29 GMT
I'm afraid about this information:
What pins are not used on the motor shield? All 6 analog input pins are available. They can also be used as digital pins (pins #14 thru 19) Digital pin 2, and 13 are not used. The following pins are in use only if the DC/Stepper noted is in use: Digital pin 11: DC Motor #1 / Stepper #1 (activation/speed control) Digital pin 3: DC Motor #2 / Stepper #1 (activation/speed control) Digital pin 5: DC Motor #3 / Stepper #2 (activation/speed control) Digital pin 6: DC Motor #4 / Stepper #2 (activation/speed control) The following pins are in use if any DC/steppers are used Digital pin 4, 7, 8 and 12 are used to drive the DC/Stepper motors via the 74HC595 serial-to-parallel latch The following pins are used only if that particular servo is in use: Digitals pin 9: Servo #1 control Digital pin 10: Servo #2 control
|
|
|
Post by Deynox on Aug 26, 2015 19:52:12 GMT
Without your help I can't do this, I try to make it about 6h and nothing. My knowledge about C/C++ is too small.
|
|
|
Post by RC Tractor Guy on Aug 26, 2015 21:11:10 GMT
I'll try help if I can, what about the information are you struggling with? It just appears to tell you what each pin does on the motor shield.
|
|
|
Post by Deynox on Aug 27, 2015 6:31:51 GMT
Connection: All about Motor Shield: learn.adafruit.com/adafruit-motor-shield/overviewAbout DC Motor from this site: 1. Make sure you #include <AFMotor.h> 2. Create the AF_DCMotor object with AF_DCMotor(motor#, frequency), to setup the motor H-bridge and latches. The constructor takes two arguments. The first is which port the motor is connected to, 1, 2, 3 or 4. frequency is how fast the speed controlling signal is. For motors 1 and 2 you can choose MOTOR12_64KHZ, MOTOR12_8KHZ, MOTOR12_2KHZ, orMOTOR12_1KHZ. A high speed like 64KHz wont be audible but a low speed like 1KHz will use less power. Motors 3 & 4 are only possible to run at 1KHz and will ignore any setting given 3. Then you can set the speed of the motor using setSpeed(speed) where the speed ranges from 0 (stopped) to 255 (full speed). You can set the speed whenever you want. 4. To run the motor, call run(direction) where direction is FORWARD, BACKWARD or RELEASE. Of course, the Arduino doesn't actually know if the motor is 'forward' or 'backward', so if you want to change which way it thinks is forward, simply swap the two wires from the motor to the shield. #include <AFMotor.h>
AF_DCMotor motor(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Motor test!");
motor.setSpeed(200); // set the speed to 200/255
}
void loop() {
Serial.print("tick");
motor.run(FORWARD); // turn it on going forward
delay(1000);
Serial.print("tock");
motor.run(BACKWARD); // the other way
delay(1000);
Serial.print("tack");
motor.run(RELEASE); // stopped
delay(1000);
} About pins: All 6 analog input pins are available. They can also be used as digital pins (pins #14 thru 19) Digital pin 2, and 13 are not used. The following pins are in use only if the DC/Stepper noted is in use: Digital pin 11: DC Motor #1 / Stepper #1 (activation/speed control) Digital pin 3: DC Motor #2 / Stepper #1 (activation/speed control) Digital pin 5: DC Motor #3 / Stepper #2 (activation/speed control) Digital pin 6: DC Motor #4 / Stepper #2 (activation/speed control) The following pins are in use if any DC/steppers are used Digital pin 4, 7, 8 and 12 are used to drive the DC/Stepper motors via the 74HC595 serial-to-parallel latch The following pins are used only if that particular servo is in use: Digitals pin 9: Servo #1 control Digital pin 10: Servo #2 control
|
|
|
Post by RC Tractor Guy on Aug 27, 2015 12:42:39 GMT
Sorry I don't understand, what is the problem exactly?
|
|