Hi,
I am trying to control a remote control car with a relay module (4 relays), two arduino units, and a 433 Mhz transmitter using the VirtualWire library. It almost works however one of the relay modules jitters, for a reason I cannot figure out. Forward and reverse work fine, though they are a bit delayed, however left and right are not working well.
The relay is connected to two 5v regulators for power (one of them gets hot). When the transmitter is not on there is no jitter. When I turn the transmitter on, one of the two relays that controls left and right jitters on and off. I am using a paralax joystick to send the signals from the arduino to the transmitter.
Here is my code
/*James Stenquist
08/07/2014
arduino remote control for RC car etc*/
/*using paralax joystick
433 Mhz transmitter receiver modules*/
//VirtualWire library and example code used written by
//Mike McCauley (mikem@open.com.au)
//Using code edited by Kyle Fieldus
//https://www.youtube.com/watch?v=U839NZ78EOo
//libraries
#include <VirtualWire.h>
//pins
int leftRightIn = 0; //analog pin
int upDownIn = 1; //analog pin
int transmit = 12;
//global vars
int leftRight = 0;
int upDown = 0;
int count = 0;
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
// Initialise the IO and ISR
vw_set_tx_pin(transmit); // Sets pin D12 as the TX pin
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
}
void loop()
{
leftRight = analogRead(leftRightIn);
upDown = analogRead(upDownIn);
char * msg = "hello";
if(leftRight > 600)
{msg[0] = '1';
msg[1] = '1';}
else if (leftRight < 470)
{msg[0] = '1';
msg[1] = '3';}
else {
msg[0] = '1';
msg[1] = '2';}
//digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone
//digitalWrite(13, false);
delay(50);
Serial.print("leftRight ");
Serial.print(msg[0]);
Serial.println(msg[1]);
if(upDown > 570)
{msg[0] = '2';
msg[1] = '1';
}
else if(upDown < 450)
{msg[0] = '2';
msg[1] = '3';
}
else{msg[0] = '2';
msg[1] = '2';
}
//digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone
//digitalWrite(13, false);
delay(100);
/*Serial.print("upDown ");
Serial.print(msg[0]);
Serial.println(msg[1]);
count ++;
Serial.println(count);
delay(10);*/
}
// 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 $
//edits by James Stenquist and previous edits by Kyle Feildus
#include <VirtualWire.h>
//pins
const int receiving = 12;
const int thf = 3;
const int thr = 5;
const int wr = 13;
const int wl = 6;
//global vars
int count;
char firstChar;
char secondChar;
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup"); //Prints "Setup" to the serial monitor
vw_set_rx_pin(12); //Sets pin D12 as the RX Pin
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
pinMode(thr, OUTPUT);
pinMode(thf, OUTPUT);
pinMode(wr, OUTPUT);
pinMode(wl, OUTPUT);
}
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;
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++)
{
char c = (buf[i]);
Serial.print(c);
Serial.print(" ");
}
count++;
Serial.print(count);
Serial.println("");
digitalWrite(13, false);
/*James S main additions*/
firstChar = buf[0];
secondChar = buf[1];
}
Serial.print("second char is ");
Serial.println(secondChar);
//left and right
if(firstChar == '1')
{
Serial.println("using first char");
if(secondChar == '1')
{
digitalWrite(wr , HIGH);
digitalWrite(wl, LOW);
Serial.println("char 11 is working");
}
else if(secondChar == '2')
{
digitalWrite(wr, LOW);
digitalWrite(wl, LOW);
Serial.println("what is going on here?");
}
else if (secondChar == '3')
{
digitalWrite(wr, LOW);
digitalWrite(wl, HIGH);
Serial.println("char 13 is working");
}
}
//forward and reverse
else if(firstChar == '2')
{
Serial.println("using second char");
if(secondChar == '1')
{
digitalWrite(thf , HIGH);
digitalWrite(thr, LOW);
Serial.println("char 21 is working");
}
else if(secondChar == '2')
{
digitalWrite(thf, LOW);
digitalWrite(thr, LOW);
}
else if (secondChar == '3')
{
digitalWrite(thf, LOW);
digitalWrite(thr, HIGH);
Serial.println("char 23 is working");
}
}
delay (1);
}