Hi all hope someone can help me.
i have recently got these cheap RF set to make my arduino projects wireless.
My first project with these is a remote controlled boat that I have pulled the electronics out and replaced with arduino.
I have already created a sketch using a wii nunchuck and got it working. I now want to make it wireless.
I have successfully transmitted the values from the transmitter to the receiver and printed them using serial.print.
But when I then send the values to the two motors in the boat the I start having problems.
It will still print when the values are zero but as soon as they go higher it stopes printing and stopes receiving.
so I have narrowed it down to the analogWrite.
I have used the virtualwire library and have changed one of the examples to fit my project.
This is my transmit sketch with the nunchuck clk atatched to A5 and data A4.
eg without analogWrite,it works, with analogWrite, it freezes.
// 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 $
//Adjusted by Toby Woodward boat project
#include <VirtualWire.h>
#include <Wire.h>
#include <WiiChuck.h> //#include "nunchuck_funcs.h"
WiiChuck chuck = WiiChuck();
const int led_pin = 13;
const int transmit_pin = 2;
int maxPWM = 60; // max pwm to motors
void setup()
{
delay(1000);
Serial.begin(115200); // Debugging only
Serial.println("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);
//nunchuck
chuck.begin();
chuck.update();
chuck.calibrateJoy();
}
void loop()
{
chuck.update();
int Mspeed,steerL,steerR,speedL,speedR;
int y, x;
//reads x, y nunchuck joystick values
y = (chuck.readJoyY());
x = (chuck.readJoyX());
//forwared- if joy pushed up
if (y>0){
Mspeed = map(y, 0, 125, 0, maxPWM);
}else{
Mspeed = 0;
}
//right -if joy pushed right
if (x>1){
steerR = map(x, 0, 123, 0, maxPWM);
steerL = 0;
}else
//left-if joy pushed left
if(x<0){
steerL = map(x, -130, 0, maxPWM, 0);
steerR = 0;
}else{// else go straight
steerL = 0;
steerR = 0;
}
// set speed for left motor and right motor with steer adjust
speedL=Mspeed-steerL;
speedR=Mspeed-steerR;
//stopes values going negative
if (speedL<0){
speedL=0;
}
if (speedR<0){
speedR=0;
}
// //prevents bad joystick calibration from suppling high volts to the motors
// if (speedL > maxPWM){
// speedL=maxPWM;
// }
// if (speedR > maxPWM){
// speedR=maxPWM;
// }
// create char array to send motor speeds as ascii goes from 0 to 255
//and my values are limited to 60 not a problem.
char msg[2] = {speedL,speedR};
if (chuck.buttonZ == true) {
digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
//debuging only
Serial.print("Left ");
Serial.print(speedL);
Serial.print(" Right ");
Serial.println(speedR);
//send char array
vw_send((uint8_t *)msg, 2);
vw_wait_tx(); // Wait until the whole message is gone
}
digitalWrite(led_pin, LOW);
delay(20);
}
And my receive sketch
// receiver.pde
// 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 $
//Adjusted by Toby Woodward boat project
#include <VirtualWire.h>
const int led_pin = 13;
const int receive_pin = 3;
//motor pins go to mosfets
const int ML = 9;//left motor
const int MR = 10;//right motor
void setup()
{
delay(1000);
Serial.begin(115200); // Debugging only
Serial.println("setup");
// Initialise the IO and ISR
vw_set_rx_pin(receive_pin);
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
pinMode(led_pin, OUTPUT);
//motor init
pinMode(ML, OUTPUT);
pinMode(MR, OUTPUT);
}
void loop()
{
char tbs[30]; //holds serialprint sentance/line allows formating
int speedL,speedR;
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
digitalWrite(led_pin, HIGH); // Flash a light to show received good message
// Message with a good checksum received, dump it.
//convert char into int values and split
speedL = int(buf[0]);
speedR = int(buf[1]);
//print values
sprintf(tbs, "Left: %4d Right: %4d",speedL,speedR);
Serial.println(tbs);
//this is what im having problems with
//send motors their speeds
//analogWrite(ML,speedL);
//analogWrite(MR,speedR);
digitalWrite(led_pin, LOW);
}
else{// stop motors if no transmition
analogWrite(ML, 0);
analogWrite(MR, 0);
}
}
Would love some help if you see somthing else that could be more efficient or better please reply.
Thanks Toby