Hey guys,
I want to control four DC motors using a L239D, a potentiometer and the 433MHz RF module, but I'm having problems in the reception. When I open the Serial to see the datas that the receiver is receiving, I can see that is receiving, continuously, the data of the starting posistion of the potentiometer. But, when I change this position, the repector revieve the first data and only this. The Serial don't show more datas, and the program execute the function referring to the last data received.
I want a continuous transmission.
The program that I'm using is:
#include <VirtualWire.h> //Inclui a biblioteca VirtualWire.h
#define motorCCEnt1 7
#define motorCCEnt2 4
#define controleL293D 9
#define motor2CCEnt1 12
#define motor2CCEnt2 13
int potVelPotencia = 0;
int i;
int flag = 0;
int pwm;
char valor[4];
void setup()
{
vw_set_ptt_inverted(true);
vw_setup(2000);
vw_set_rx_pin(2);
vw_rx_start();
pinMode(2, INPUT);
pinMode(motorCCEnt1,OUTPUT);
pinMode(motorCCEnt2,OUTPUT);
pinMode(controleL293D,OUTPUT);
pinMode(motor2CCEnt1,OUTPUT);
pinMode(motor2CCEnt2,OUTPUT);
Serial.begin(9600);
}
void loop()
{
char cod;
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen))
{
flag = 1;
}
if (flag == 1) {
for (i = 0; i < buflen; i++) {
valor[i] = buf[i];
}
pwm = atoi(valor);
if (pwm == 124) {
digitalWrite(motorCCEnt1,LOW);
digitalWrite(motorCCEnt2,LOW);
digitalWrite(motor2CCEnt1,LOW);
digitalWrite(motor2CCEnt2,LOW);
}
if (pwm >= 125) {
potVelPotencia = pwm;
analogWrite(controleL293D,potVelPotencia);
digitalWrite(motorCCEnt1,HIGH);
digitalWrite(motorCCEnt2,LOW);
digitalWrite(motor2CCEnt1,HIGH);
digitalWrite(motor2CCEnt2,LOW);
}
else if (pwm <= 123) {
potVelPotencia = 255 - pwm;
analogWrite(controleL293D,potVelPotencia);
digitalWrite(motorCCEnt1,LOW);
digitalWrite(motorCCEnt2,HIGH);
digitalWrite(motor2CCEnt1,LOW);
digitalWrite(motor2CCEnt2,HIGH);
}
Serial.println(pwm);
}
flag = 0;
for (i = 0; i <= 4; i++) {
valor[i] = 0;
}
}
Thank you
Please post the transmitter code, and examples of the data that you are receiving.
The transmitter code:
#include <stdlib.h>
#include<string.h>
#include <VirtualWire.h> //Inclui a biblioteca VirtualWire.h
const int potPin = A0;
long val = 0;
int i;
int pwm;
char pwmEnvia[10];
char *dado ; //Cria a variável que ira armazenar a informação pré definida enviadas pelos botões
void setup()
{
//Configura o Arduino para trabalhar como transmissor
vw_set_ptt_inverted(true);
vw_setup(5000);
vw_set_tx_pin(2);//Configura o pino 2 como tx
//===================================================
Serial.begin(9600);
//Configura o estado das portas digitais
pinMode(2, OUTPUT);
pinMode(A0, INPUT);
}
void loop()
{
val = analogRead(potPin); // Leitura do potênciomentro
pwm = val / 4;// Recebe o valor de 0-1023 e converte para 0-255
itoa(pwm, pwmEnvia, 10);// Coverte inteiro em String
dado = pwmEnvia; //Armazena o dado pré determinado
vw_send((uint8_t *)dado, strlen(dado)); //Envia a variável dado
delay(25);
vw_wait_tx(); //Aguarda o fim de transmissão
delay(200); //Aguarda um tempo para a próxima leitura
Serial.println(dado);
}
The received data:
It stops when I change the potentiometer
But, the transmitter continue sending datas:
You are not sending zero-terminated character strings, and the receiver is not properly zero terminating the strings received. So, functions like atoi() won't work properly.
There are many other things wrong with that code, and there is really no need for most of it. Much of it is unnecessary copying of bytes and pointers.
Just send an integer (the analogRead result) and receive an integer, as follows:
Transmitter:
int val = analogRead(potPin); // Leitura do potênciomentro
vw_send((uint8_t *)&val, 2); //Envia a variável
vw_wait_tx(); //Aguarda o fim de transmissão
Receiver:
int pwm;
uint8_t buflen = 2;
if (vw_get_message((uint8_t *)&pwm, &buflen)) {
pwm = pwm/4; // if needed to keep same logic
Serial.println(pwm);
...
}
I guess that I didn't undestend... I tried to do this changes and the same problem is happening.
Check the loop code for me, please:
void loop()
{
uint8_t buflen = 2;
if (vw_get_message((uint8_t *)&pwm, &buflen))
{
pwm = pwm/4;
if (pwm == 124) {
potVelPotencia = 0;
digitalWrite(motorCCEnt1,LOW);
digitalWrite(motorCCEnt2,LOW);
digitalWrite(motor2CCEnt1,LOW);
digitalWrite(motor2CCEnt2,LOW);
}
if (pwm >= 125) {
potVelPotencia = pwm;
analogWrite(controleL293D,potVelPotencia);
digitalWrite(motorCCEnt1,HIGH);
digitalWrite(motorCCEnt2,LOW);
digitalWrite(motor2CCEnt1,HIGH);
digitalWrite(motor2CCEnt2,LOW);
}
if (pwm <= 123){
potVelPotencia = 255 - pwm;
analogWrite(controleL293D,potVelPotencia);
digitalWrite(motorCCEnt1,LOW);
digitalWrite(motorCCEnt2,HIGH);
digitalWrite(motor2CCEnt1,LOW);
digitalWrite(motor2CCEnt2,HIGH);
}
Serial.print(pwm);
}
Sorry, I'm very newbie and I'm just trying to do a project for my graduation.
I think thak I solved the problem in the transmitter, but I can't find a solutuion for the receptor.
You forgot to explain what the problem is with the receiver. Always post ALL THE CODE, not pieces.
Put Serial.println() statements in your code to see what is being received, and if that matches what is being transmitted.
Man, I think that I find the problem. I changed the function to open the L239D when the treceptor receives "124", the value of the starting position. Now, it just receives one data and stops. Before, it was receiving 124 many times and when I changed the position, it received only one data and stopped. What do you think about this? When I use the L239D, the datas stop.
Wiring problem.
Never use the 5V output of the Arduino to power motors, always use a separate power supply and connect the grounds.