First off, I'm using Servo2, it was necessary to use the older version because VirtualWire doesn't play nice with Servo.
I couldn't find the original version, so I found the one that had a few extra lines to make it Mega compatible, and commented out those lines. (If anyone has the ORIGINAL servo 2, let me know, for testing purposes).
Here's my code:
#include <VirtualWire.h>
#include <Servo2.h>
Servo myservo;
int pos = 90;
int RXPin = 4;
int servoPin = 9;
void setup()
{
myservo.attach(servoPin);
Serial.begin(9600);
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for RX Link Module
vw_setup(2000); // Bits per sec
vw_set_rx_pin(RXPin); // We will be receiving on pin 4, ie the RX pin from the module connects to this pin.
vw_rx_start(); // Start the RF receiver
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // check to see if anything has been received
{
if (buf[0] == 'L'){
pos = 60;
}
if (buf[0] == 'R'){
pos = 120;
}
if (buf[0] == 'S'){
pos = 90;
}
myservo.write(pos);
delay(200);
Serial.println(buf[0]);
}
}
Now when I comment out "myservo.write(pos);" the if statement evaluates just as it should, every time it's run there is data read from the receiver, it properly determines if it read L, R, or S, and adjusts variable "pos" accordingly.
When I uncomment "myservo.write(pos);" the first if statement evaluates false, and I have no idea why.
It is worth mentioning that when I press the reset button, when the program first loads it'll run all the way through correctly JUST ONCE.
The Serial.println(buf[0]) is just there for debugging.
Can anyone shed some light on why myservo.write is causing this problem? (And if anyone has the original Servo2 library, can they link it for me please?)