VirtualWire and ServoTimer2 - Servo not responding

Hello, i posted recently and a user helped fix the code about the tx-rx part, but now the servo won't move, it receives the value from the transmitter but it does not feed the servo. The code is below, i'm a newbie. Anyone know why ?

Transmitter

#include <VirtualWire.h>

int potanalogvalue;
String potstringvalue;
char potcharvalue[27];
int potpin = 0;  // analog pin used to connect the potentiometer

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  vw_setup(2000);
  vw_set_tx_pin(7);
}

void loop() {
  // put your main code here, to run repeatedly:
  potanalogvalue = analogRead(potpin);
  Serial.print(potanalogvalue);
  vw_send((uint8_t *) &potanalogvalue, 2);
  vw_wait_tx();
}

Receiver

#include <VirtualWire.h>
#include <ServoTimer2.h>  // the servo library

#define servo1Pin 3   // the pin the servo is attached to

ServoTimer2 servo1;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  servo1.attach(servo1Pin);

  vw_setup(2000);                   
  vw_set_rx_pin(7);           
  vw_rx_start();   
}

void loop() {
  // put your main code here, to run repeatedly:
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;

 int potval = 0;
 buflen=2; //max two bytes expected

 if (vw_get_message((uint8_t *) &potval, &buflen)) {
     Serial.println(potval);
    }
 potval = map(potval, 0, 1023, 500, 2400);
 servo1.write(potval);
 delay(25);
}

Why is the code to move the servo not inside the block where the data is read?

On most passes through loop(), there will be no data for vw_get_message() to read, so the servo will be sent to 0. On the passes where there is data, the servo will be sent to a new position, but before it gets there, it will be told to go back to 0.

Oh it's now responding, i saw something like that in the serial monitor, but in my mind it was something normal... thanks sensei, but it's not responding very well, but its turning accordly to the potentiometer, any idea to improve response time ?

any idea to improve response time ?

Stop poking your head in the sand:

 delay(25);

You'll need to be more specific with me, because i tried many things with delay, removing, increasing, decreasing it...

You'll need to be more specific with me, because i tried many things with delay, removing, increasing, decreasing it...

The only one of those that makes sense is removing the delay() call completely.

Reading data slower than the data is sent is a recipe for disaster.

You need to keep in mind that those radios are slow. You have the baud rate set to 2000. Of course, raising the baud rate results in shorter transmission distances and more corrupt data.

So i should accept that the servo won't follow the potentiometer perfectly ?

Imitheo:
So i should accept that the servo won't follow the potentiometer perfectly ?

Yes, or describe in more detail, exactly what is not perfect about it. If the response time is a problem, you need to analyze to find the reason.

Also, did you correct the problem mentioned in reply #1?

Did you remove the Serial debug print's in the transmit sketch?

So i should accept that the servo won't follow the potentiometer perfectly ?

You should minimize the amount of data sent. On the sender, read the potentiometer, but only send data if the current reading is not the same as the last reading. No changes are needed on the receiver, since it only moves the servo if there is data.

Hmm about not sending the same data it's a real deal. But unfortunately, i don't know how to check that.

Imitheo:
Hmm about not sending the same data it's a real deal. But unfortunately, i don't know how to check that.

Pseudocode:
if (data != data_sent)
{
send
data_sent = data
}

Hmm about not sending the same data it's a real deal. But unfortunately, i don't know how to check that.

int prevPotVal;

void loop()
{
   int currPotVal = analogRead(potPin);
   if(currPotVal != prevPotVal)
   {
      // Send currPotVal
   }
   prevPotVal = currPotVal;
}

Whoah, i thought it was complicated, but i also wrote the same code.

Imitheo:
Whoah, i thought it was complicated, but i also wrote the same code.

Then why did you say you didn't know how to do it? :slight_smile:

I thought it was complicated, because i learnt c++ from a pseudocode called "Portugol", so it's difficult for me to transform the pseudocode into c++, but i managed to do it looking at comparison table between the two languages, most of time i think it's hard to do it, but then i manage to achieve. But thanks, you have been really helpful, it improved the "following process" of the servo relative to the potentiometer position a bit. But i'll look to improve that, sometimes it stucks in a position regardless of the transmitting position value, like a lag in the receiving operation.