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++;
}
}