Hi!
So i'm trying to make a simple rf controlled car.
I'm using 2 dc motors, an l298, a receiver and transmitter pair Large Power Long Range 433MHz Wireless Transceiver Kit for Arduino - Free shipping - DealExtreme (this one), a 2 axis joystick and 2 arduino's (obviously).
This is my code for the transmitter:
#include <VirtualWire.h>
const int led_pin = 13;
const int transmit_pin = 12;
struct package {
float y ;
float x ;
};
typedef struct package Package;
Package data;
void setup() {
// Initialise the IO and ISR
vw_set_tx_pin(transmit_pin);
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
pinMode(led_pin, OUTPUT);
}
void loop() {
digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
readSensor();
vw_send((uint8_t *)&data, sizeof(data));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(led_pin, LOW);
delay(100);
}
void readSensor() {
data.y = analogRead(A0);
data.x = analogRead(A1);
}
And this is my code for the receiver:
#include <VirtualWire.h>
// Motor 1
int dir1PinA = 2;
int dir2PinA = 3;
int speedPinA = 9; // Needs to be a PWM pin to be able to control motor speed
// Motor 2
int dir1PinB = 4;
int dir2PinB = 5;
int speedPinB = 10; // Needs to be a PWM pin to be able to control motor speed
int y;
int x;
int pwm;
const int receive_pin = 11;
char yChar[10];
char xChar[10];
struct package {
float y = 0.0;
float x = 0.0;
};
typedef struct package Package;
Package data;
void setup() {
Serial.begin(9600);
pinMode(dir1PinA, OUTPUT);
pinMode(dir2PinA, OUTPUT);
pinMode(speedPinA, OUTPUT);
pinMode(dir1PinB, OUTPUT);
pinMode(dir2PinB, OUTPUT);
pinMode(speedPinB, OUTPUT);
vw_set_rx_pin(receive_pin);
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
}
void loop() {
checkradio();
if (data.y > 520 && data.x > 500 && data.x < 520) {
pwm = map(data.y, 514, 1023, 0, 255);
motor1forward();
motor2forward();
}
else if (data.y < 500 && data.x > 500 && data.x < 520) {
pwm = map(data.y, 514, 0, 0, 255);
motor1back();
motor2back();
}
else if (data.x > 500 && data.x < 520 && data.y < 520 && data.y > 500) {
motor2stop();
motor1stop();
}
else if (data.x < 500 && data.y > 500 && data.y < 520) {
pwm = map(data.x, 513, 0, 0, 255);
motor1back();
motor2forward();
}
else if (data.x > 520 && data.y > 500 && data.y < 520) {
pwm = map(data.x, 513, 1023, 0, 255);
motor1forward();
motor2back();
}
delay(100);
}
void checkradio() {
uint8_t buf[sizeof(data)];
uint8_t buflen = sizeof(data);
if (vw_have_message() == HIGH) {
vw_get_message(buf, &buflen);
memcpy(&data, &buf, buflen);
String xString = String(data.x, 1);
xString.toCharArray(xChar, 10);
Serial.print("Package: ");
Serial.print(data.x);
String yString = String(data.y, 1);
yString.toCharArray(yChar, 10);
Serial.print(" ");
Serial.println(data.y);
}
else if (vw_have_message() == LOW) {
Serial.println("no transmission");
}
}
void motor1forward() {
analogWrite(speedPinA, pwm);
digitalWrite(dir1PinA, LOW);
digitalWrite(dir2PinA, HIGH);
}
void motor1stop() {
analogWrite(speedPinA, 0);
digitalWrite(dir1PinA, LOW);
digitalWrite(dir2PinA, HIGH);
}
void motor1back() {
analogWrite(speedPinA, pwm);
digitalWrite(dir1PinA, HIGH);
digitalWrite(dir2PinA, LOW);
}
void motor2forward() {
// Motor 2 Forward
analogWrite(speedPinB, pwm);
digitalWrite(dir1PinB, LOW);
digitalWrite(dir2PinB, HIGH);
}
void motor2stop() {
// Motor 1 Stop (Freespin)
analogWrite(speedPinB, 0);
digitalWrite(dir1PinB, LOW);
digitalWrite(dir2PinB, HIGH);
}
void motor2back() {
// Motor 2 Reverse
analogWrite(speedPinB, pwm);
digitalWrite(dir1PinB, HIGH);
digitalWrite(dir2PinB, LOW);
}
When i play around with the joystick wirelessly it works for a few seconds, then it stops working, once i start using the joystick and turning it.
The rf receiver doesn't receive any signals anymore, i added a serial print to check.
But, when i use 1 arduino and skip the rf modules, it works just like i intended it to do.
I can post that code too if it would help, but i doubt it.
Also, when i use just the receiver (like a dumpinfo) sketch, it also stops working after a few seconds once i start using it.
I'm relatively new to arduino, so probably i'm not seeing something some that of you will...
Thanks!
-Razo