Digital pin to default to HIGH state when not receiving

Hey Guys, new to Arduino programming. Got a 433mhz wireless transmit and receive program working.

See my RECEIVE code below

I need the 2 pins (Digital2 and 3) to be always on HIGH and then when it receives either a 2 or 3, will set the respective pins to LOW. There is a problem if the signal might drop out for a split second, the pins will be placed in the last received state (either HIGH or LOW), so therefore to be safe it needs to be on HIGH even if signal does drop for a second.

I tried placing the 2 lines ( digitalWrite(2, HIGH); and digitalWrite(3, HIGH) outside the "if" block but the pins always stay on HIGH. It doesn't change.

Is someone able to point me in the right direction?

#include <RH_ASK.h>
#include <SPI.h>
RH_ASK driver;

void setup() {
     Serial.begin(9600);	  // Debugging only
    if (!driver.init())
         Serial.println("init failed");
}

void loop() {
  int Mode = 0;

//buf is a pointer to the received bytes--------------------------
 uint8_t buflen = 1;
    uint8_t buf[buflen];

    if (driver.recv(buf, &buflen)) // Non-blocking
    {
      int i;
      // DEBUG Message with a good checksum received, dump it.
        //Enable 5v pull up on Digital Pin 2 and 3
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);

        Serial.print("Mode: ");
        Serial.println(*buf);  
    Mode = buf[0];    

      if (Mode == 2) { //If Mode 2, set Pin 2 to Low 0v
        digitalWrite(2, LOW);
      }

      if (Mode == 3) { //If Mode 3, set Pin 3 to Low 0v
        digitalWrite(3, LOW);
      }
    }
}

I don't see pinMode(2, OUTPUT); or pinMode(3, OUTPUT); in your setup() function (or anywhere else) to change the pins to be outputs.

Ok. I put them in the setup, what should i do now?

#include <RH_ASK.h>
#include <SPI.h>
RH_ASK driver;

void setup() {
     Serial.begin(9600);	  // Debugging only
    if (!driver.init())
         Serial.println("init failed");
     pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);

}

Show us your transmit code. This:

  Mode = buf[0];    
      if (Mode == 2) ...

is looking for an integer value of 2, but I suspect your code may be sending an ASCII '2'.
Or, change
if (Mode == 2)
to
if (Mode == '2')
to test for the character 2.

Where is this library from? Not familiar with it.

RadioHead-1.59

Yes it does set itself back to HIGH when going back through the loop. But i mentioned that this only works when it is Receiving data. If i loose RF signal, it will stay on the last known state.

Anyways i've sorted it out. I worked around it.

Now when i ground either Digitalpin 2 or 3 and purposely disconnect/block RF signal, the Pins will revert back to HIGH state

      if (Mode== 2) {
        digitalWrite(2, LOW);
        delay(200);
        digitalWrite(2, HIGH);
      }

      if (Mode== 3) { 
        digitalWrite(3, LOW);
        delay(200);
        digitalWrite(3, HIGH);
      }

wait a minute - you're grounding an output pin?

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.