0

I'm trying to make a contraption where you press a button on one board and it moves the servo either to 90 degrees or 180 degrees on the other board. If it's at 90 then it moves to 180 and vice versa.

I'm not very knowledgeable when it comes to this stuff as this is my first major project so bear with me. I already have the wireless system working (thanks to hours of Googling) and a toggle system for an LED (for testing to see if the wireless is working).

I'm using one of the tiny RF transmitters, two Nanos, and a servo from Radio Shack. The problem is the servo doesn't turn but my test LED turns on and off. Here is the code for the receiver end of things:

#include <VirtualWire.h>
#include <ServoTimer2.h>

const int releu_pin = 9;
const int servoPin = 6;
const int transmit_pin = 12;
const int receive_pin = 3;//pin connected between RX module and Arduino
const int transmit_en_pin = 5;

ServoTimer2 myservo;  

void setup() {
    myservo.attach(servoPin);
    myservo.write(45);
    vw_set_tx_pin(transmit_pin);
    vw_set_rx_pin(receive_pin);
    vw_set_ptt_pin(transmit_en_pin);
    vw_set_ptt_inverted(true);
    vw_setup(2000);//speed communication bps

    vw_rx_start(); // activate receiving mode
    pinMode(releu_pin, OUTPUT);
    pinMode(LED_BUILTIN, OUTPUT); //Debug
}

void loop() {
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
    if (vw_get_message(buf, &buflen)) {
      //verify if any data is received
      if(buf[0]=='1') {
        //if received 1 turn ON releu_pin 
        myservo.write(90);
        digitalWrite(releu_pin , HIGH);
        digitalWrite(LED_BUILTIN, HIGH); //Debug
        delay(100); 
      }  
     if(buf[0]=='0') {
        myservo.write(180);
        digitalWrite(releu_pin , LOW);
        digitalWrite(LED_BUILTIN, LOW); //Debug
        delay(100); 
    }
  }
}
dda
  • 6,030
  • 2
  • 25
  • 34
ZenixNet
  • 33
  • 3
  • The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should [edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ May 24 '17 at 20:58
  • Did you test the servo without using the wireless? In other words, are you sure it's not the circuit/servo itself? – DigitalNinja May 24 '17 at 21:10
  • @DigitalNinja Yes, I hooked the servo up to the servo example that Arduino gives you. I also modified that example to require a button push. Both work great. – ZenixNet May 24 '17 at 21:15
  • Good. What is the relay controlling? Do you need to activate that before controlling the servo perhaps? – DigitalNinja May 24 '17 at 21:17
  • @DigitalNinja Are you talking about releu_pin? That's the testing LED. One of the many tutorials I built this from used it as their name for the LED so I stuck with it. – ZenixNet May 24 '17 at 21:29
  • Okay, I thought you were using `LED_BUILTIN` for toggling the LED. Is that all of your code? It doesn't look like `loop()` is actually looping... – DigitalNinja May 24 '17 at 21:34
  • @DigitalNinja I'm actually using both. Another help forum asked if my onboard LED would work so I left that in too. But yes, that's all my code. – ZenixNet May 24 '17 at 21:36
  • I'm not sure what is wrong. I would retest the servo using this exact code except remove looking for the message. Instead just try turning the servo and toggling the led with a delay in between movements. – DigitalNinja May 24 '17 at 22:16
  • @DigitalNinja Got it figured out. ServoTimer2 is in milliseconds, not degrees like the servo library that comes with the board. – ZenixNet May 24 '17 at 22:20

0 Answers0