Hello,
I want to regulate the speed of a motor connected to a L297D, using the signal received from a potentiometer wired on another board. I want to transmit the voltage received from the potentiometer to the first board, the one with the motor, through a 433 FM transmission device.
I am able to transmit the analog signal from a board to another, and to display it on the serial monitor. But when I use the command analogWrite to tell the motor to run at the speed set by the potentiometer, it only reads it once, runs it at that speed, and doesn't receive any other signal from the second board.
Can anyone help me?
Thank you
This is the code from the receiver
#include <VirtualWire.h>
int enablePin = 11; //Enable pin in the L297D
int rPin = 10; //Pin to turn to the right in the L297D
int mSpeed = 0;
void setup() {
Serial.begin(9600);
Serial.println("Reciever: setup");
vw_set_rx_pin(13);
vw_setup(4000);
vw_rx_start();
for (int i = 0; i < 12; i++) {
digitalWrite(i, LOW);
}
}
void loop() {
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
mSpeed = 0;
if (vw_get_message((uint8_t *)buf, &buflen)) {
int i;
String chain = "";
if ((char)buf[0] == 'i') {
for (i = 1; i < buflen; i++) {
chain.concat((char)buf[i]);
}
mSpeed = chain.toInt();
Serial.print("Motor Speed: ");
Serial.println(mSpeed);
}
analogWrite(enablePin, mSpeed);
digitalWrite(10, HIGH);
digitalWrite(9, LOW);
delay(1000);
}
}
This is the code from the transmitter
#include <VirtualWire.h>
const int enablePin = A0;
int motorSpeed = 0;
void setup() {
Serial.begin(9600);
vw_set_ptt_inverted(true);
vw_set_tx_pin(13);
vw_setup(4000);
}
void loop() {
char buf[VW_MAX_MESSAGE_LEN];
motorSpeed = analogRead(enablePin) / 4;
String str="";
str= "i" + String(motorSpeed);
str.toCharArray(buf, sizeof(buf));
vw_send((uint8_t *)buf, strlen(buf));
vw_wait_tx();
delay(1000);
}