Hi everyone,
i am trying to control a 2 dc motors with w Seed motor Shield 2.0 through the 433Mhz. Rf transmitter and Receiver. I have had no problem transmitting data from transmitter to receiverer. When I transmit just a simple value and not have it do anything else but output it, everything prints out fine. However, once I combine the transmitting/receiving code with the motor library it freezes.
for example if I send a value of 1 and receiver reads a 1 it outputs "49=". It doesn't even go to the " motordriver.goForward() " function and it just stays there if I send another value it just freezes there.I am very new to all this and really don know where to go from here. I dont know if its a timing issue between the Transmission and the motor function or what. Can anyone please help? Below is a sample of the code im using for the receiver:
#include <VirtualWire.h>
#include "MotorDriver.h"
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
motordriver.init();
motordriver.setSpeed(200,MOTORB);
motordriver.setSpeed(200,MOTORA);
vw_setup(2000); // Bits per sec
vw_set_rx_pin(2); //Rx Data pin to Digital Pin 2
vw_rx_start(); // Start the receiver PLL running
}
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.
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i]);//print received command
if(buf[i] == '1')//if button 1 is pressed.... i.e.forward buton
{
motordriver.goForward();
Serial.println(" = forward");
delay (100);
}
if(buf[i] == '2')//if button 2 is pressed.... i.e.back buton
{
motordriver.goBackward();//go backward
Serial.println(" = backward");
}
if(buf[i] == '3')//if button 3 is pressed.... i.e.left buton
{
motordriver.goLeft();//go left
Serial.println(" = left");
}
if(buf[i] == '4')//if button 4 is pressed.... i.e.right buton
{
motordriver.goRight();//go right
Serial.println(" = right");
}
if(buf[i] == '5')//if button 5 is pressed.... i.e.stop buton
{
motordriver.stop();//stop/brake
Serial.println(" = stopped");
}
Serial.print(" ");
}
Serial.println("");
digitalWrite(13, false);
}//close if
}//close loop
in case its needed here is the transmitter code:
#include <VirtualWire.h>
//Assigning controller buttons to Digital Pins
int forward = 8;
int backward = 9;
int rightTurn = 10;
int leftTurn = 11;
int stopMotor = 12;
int remotePins[]= {8,9,10,11,12};//array to store pin nos
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
// Initialise the IO and ISR
vw_setup(2000); // Bits per sec
vw_set_tx_pin(3); //Transmitter Data Pin to Digital Pin 3
for(int i = 0; i<6 ; i++)
{
pinMode(remotePins[i], INPUT);
digitalWrite(remotePins[i],HIGH);
}
/*
This is what the loop above does :-
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);
pinMode(12, INPUT);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);*/
}//close setup
void loop()
{
char *msg2;
if(digitalRead(forward) == LOW)//if the forward button is pressed
{
char *msg2 = "1";//send 1 to the receiver
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg2, strlen(msg2));//send the byte to the receiver
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false);
}
if(digitalRead(backward) == LOW)//if the back button is pressed
{
char *msg2 = "2";///send 2 to the receiver
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg2, strlen(msg2));//send the byte to the receiver
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false);
}
if(digitalRead(leftTurn) == LOW)//if the left button is pressed
{
char *msg2 = "3";//send 3 to the receiver
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg2, strlen(msg2));//send the byte to the receiver
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false);
}
if(digitalRead(rightTurn) == LOW)//if the right button is pressed
{
char *msg2 = "4";//send 4 to the receiver
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg2, strlen(msg2));//send the message to the receiver
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false);
}
if(digitalRead(stopMotor)==LOW)//if the stop button is pressed
{
char *msg2 = "5";//send 5 to the receiver
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg2, strlen(msg2));//send the message to the receiver
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false);
}
}//close loop