I am working on a project where I am sending numbers via serial port to an RF transmitter. On the other side, the receiver is attached to an Arduino. The Visual Basic program is sending numbers and the Arduino program is meant to receive these numbers and use them to control a motor. (We need a PWM signal for the motor) However, for some reason, the Arduino program is not receiving the number right and hence the motor is not functioning as required. I have tried to send a number to the PC from the Arduino but I only receive garbage. These are the transmitter and receiver I am using:
http://www.alldatasheet.com/datasheet-pdf/pdf/344352/KYL/KYL-210.html
Do I have a problem with the type of values the transmitter and receiver are sending? Or would it be a problem from the Arduino program?
Here is the program:
int i = 0;
int MLmosfet1_4 = 10;
int MLmosfet2_3 = 12;
int MRmosfet1_4 = 2;
int MRmosfet2_3 = 5;
void setup()
{
pinMode(MLmosfet1_4, OUTPUT);
pinMode(MLmosfet2_3, OUTPUT);
pinMode(MRmosfet1_4, OUTPUT);
pinMode(MRmosfet2_3, OUTPUT);
Serial.begin(9600); // This opens serial port
// and sets data rate at 9600 bps
}
void loop()
{
byte record[2] = {0};
byte state = 0;
byte currentState = 0;
if (Serial.available()) // check if data has been sent from the computer
{
state = Serial.read(); // reads the most recent byte (from 0 - 255)
if(i % 2 == 0) // THIS PART CHECKS WERE TO FILL IN ARRAY SO AS TO HAVE MEMORY EFFECT
{
record[0] = state;
}
else
{
record[1] = state;
}
if (state>=0 && state <=63) //forward
{
currentState = state * 4; //0 - 252
analogWrite(MLmosfet1_4, currentState); //steady 5V
analogWrite(MLmosfet2_3, 0); // 0V
analogWrite(MRmosfet1_4, currentState); //steady 5V
analogWrite(MRmosfet2_3, 0); // 0V
}
else if (state>=64 && state<=127) //reverse
{
currentState = (state-64)*4; //0 - 252
analogWrite(MLmosfet1_4, 0); // 0V
analogWrite(MLmosfet2_3, currentState); // steady 5V
analogWrite(MRmosfet1_4, 0); // 0V
analogWrite(MRmosfet2_3, currentState); // steady 5V
}
else if(state>=128 && state <=191) //left
{
currentState = (state - 118); //input will be 10 - 73
if(i % 2 == 0)
{
if (record[1] >=0 && record[1] <=63) //previous action was move forward
{
analogWrite(MLmosfet1_4, currentState); //approx 25% duty cycle
analogWrite(MLmosfet2_3, 0); // 0V
analogWrite(MRmosfet1_4, record[1]); //keeps previous forward speed
analogWrite(MRmosfet2_3, 0); // 0V
}
else //means that previous action was reverse
{
analogWrite(MLmosfet1_4, 0); // 0V
analogWrite(MLmosfet2_3, currentState); // approx 25% duty cycle
analogWrite(MRmosfet1_4, 0); // 0V
analogWrite(MRmosfet2_3, record[1]); // keeps previous reverse speed
}
}
else // i is odd, therefore we have to read from reord[0]
{
if (record[0] >=0 && record[0] <=63) //previous action was move forward
{
analogWrite(MLmosfet1_4, currentState); //approx 25% duty cycle
analogWrite(MLmosfet2_3, 0); // 0V
analogWrite(MRmosfet1_4, record[0]); //keeps previous forward speed
analogWrite(MRmosfet2_3, 0); // 0V
}
else //means that previous action was reverse
{
analogWrite(MLmosfet1_4, 0); // 0V
analogWrite(MLmosfet2_3, currentState); // approx 25% duty cycle
analogWrite(MRmosfet1_4, 0); // 0V
analogWrite(MRmosfet2_3, record[0]); // keeps previous reverse speed
}
}
}
else if(state>=192 && state <=255) //right
{
currentState = (state - 118); //input will be 10 - 73
if(i % 2 == 0)
{
if (record[1] >=0 && record[1] <=63) //previous action was move forward
{
analogWrite(MLmosfet1_4, record[1]); //keeps previous forward speed
analogWrite(MLmosfet2_3, 0); // 0V
analogWrite(MRmosfet1_4, currentState); //approx 25% duty cycle
analogWrite(MRmosfet2_3, 0); // 0V
}
else //means that previous action was reverse
{
analogWrite(MLmosfet1_4, 0); // 0V
analogWrite(MLmosfet2_3, record[1]); // keeps previous reverse speed
analogWrite(MRmosfet1_4, 0); // 0V
analogWrite(MRmosfet2_3, currentState); // approx 25% duty cycle
}
}
else // i is odd, therefore we have to read from reord[0]
{
if (record[0] >=0 && record[0] <=63) //previous action was move forward
{
analogWrite(MLmosfet1_4, record[0]); //keeps previous forward speed
analogWrite(MLmosfet2_3, 0); // 0V
analogWrite(MRmosfet1_4, currentState); //approx 25% duty cycle
analogWrite(MRmosfet2_3, 0); // 0V
}
else //means that previous action was reverse
{
analogWrite(MLmosfet1_4, 0); // 0V
analogWrite(MLmosfet2_3, record[0]); // keeps previous reverse speed
analogWrite(MRmosfet1_4, 0); // 0V
analogWrite(MRmosfet2_3, currentState); // approx 25% duty cycle
}
}
}
i++;
}
}
You can test the transmitter and the receiver by connection one of them to the PC (using the RS232 interface) and the other one to the arduino pin 0 Rx and Tx (using the TTL interface). once you hooked it up, connect the arduino to a power supply, open the arduino IDE, and set that the arduino connected to COM port 1(the serial interface on the mother board). just try to run one of the examples and see if it work.
NOTE that the interfacing method is very impotent, the arduino using TTL and the PC works with RS232, according to the datasheet it should be set by somesort of jumper, Am i right?
I would first debug the connection without any code
Connect both the RF RX and RX to a pc use putty or any other terminal to connect to both come ports, type some text on the TX terminal and look what comes out in the RX terminal.
If this works connect the RX to the arduino and use a terminal to send data to it. If now your arduino is working then its the VB that has an error, if it is not working then the arduino code has a problem.
Most often these RS232 problems are with the baud rate settings of the COM port in the software. make sure both sides are set to the same Baudrate be sure to check the VB code that the data rate is set to 9600 just as on the arduino.
I would use a simple program to light up LED's to see if the issue is not with the motor code.
Also shouldn't you be checking if (Serial.available() > 0)