77Views0Replies

Author Options:

Virtualwire RF 433mhz Voltage problem mk2 Answered

Hi,
I have this code and have modded it a bit, i was wondering if there was a way of making the pincode more than 1 digit long?
Also i have got the code sending command and pin code when buton to pressed and stopping when released and the receiver end reading it and seeing it stop, sending a pin high when it reads it but i cant get i to go low when it stops receiving it. wen ever it looks like it will work It seems to make the pin go to 0.16 when low to 0.24 when high. can you help?
tom
Transmitter code
<div class="inpostbox">
#include <VirtualWire.h>
int button = 2;
  void setup() {
    Serial.begin(9600);
    vw_set_ptt_inverted(true);
    vw_setup(2000);
    vw_set_tx_pin(4);
    pinMode(button,INPUT);
    pinMode(button,LOW);
  }

  void loop() {
    while
    (digitalRead(button) == LOW);
     sendMessage("1", "5");
     //delay(10000);
  }

  void sendMessage(char* pinCode, char *data) {
     if (strlen(data) > 0) {
      double startTime = millis();
      Serial.println("Sending...");
      int msgSize = (strlen(data) + strlen(pinCode) + 1);
      char packetData[msgSize];
      strcat(packetData, pinCode);
      strcat(packetData, ".");
      strcat(packetData, data);

      Serial.println("Debugging ");
      Serial.print("Sending Command: ");
      Serial.print(data);
      Serial.print(" with Pin Code: ");
      Serial.print(pinCode);
      Serial.print("\n");

      vw_send((uint8_t *)packetData, msgSize);
      vw_wait_tx();

      double endTime = millis();
      double timeTaken = (endTime - startTime);
      Serial.print("Time Taken: ");
      Serial.print(timeTaken);
      Serial.print(" ms\n");
    } 
  }
</div>
Receiver Code
<div class="inpostbox">

#include <VirtualWire.h>
  #include <string.h>
  int relay = 8;
  byte message[VW_MAX_MESSAGE_LEN];
  byte messageLength = VW_MAX_MESSAGE_LEN;

  void setup() {
    Serial.begin(9600);
    vw_set_ptt_inverted(true);
    vw_setup(2000);
    vw_set_rx_pin(2);
    vw_rx_start();
    pinMode(relay,INPUT);
    pinMode(relay,LOW);
  }

  void loop() {
    while
    //(vw_have_message())
   (vw_get_message(message, &messageLength))
    {
       int command = processResponse((char*)message, 1); //Byte Array Response and Pin Code.
       if
      // (vw_have_message());
       (command) {
         Serial.print("Received Command: ");
         Serial.print(command);     
         Serial.print("\n");
         digitalWrite (relay,HIGH);
       }
        
         else //REMOVE
         delay(100); //REMOVE
         digitalWrite (relay,LOW); //REMOVE
        
       }
   }
int processResponse(char* message, int pinCode) {
      char *p = message;
      char *buf;
      int o = 0;
      int pin;
      int command;

      while ((buf = strtok_r(p, ".", &p)) != NULL)  {
         if (o == 0) {
           pin = atoi(buf);
         
         } else {
           command = atoi(buf);
           ;
         }
         o++;
      }

      if (pinCode == pin && command > 0) {
          return command;
         
      } else {
         return 0;
        
        
      }
  }
</div>
This code only sends pin 8 from 0.14 when low to 0.17 when high. unless you remove the three lines marked REMOVE then it goes from 0.14 when low to 4.47 when high but stays there and will not go low when buttton it released.
Please help

Discussions