How to toggle relay for 1s?

hello, I need some help please
I need to add to my firm - see below, a momentary instance to a relay ( 1 sec)
can you please be kind and add it to my firm?
thanks in advance


// This #include statement was automatically added by the Particle IDE.
#include "RelayShield.h"

// Create an instance of the RelayShield library, so we have something to talk to
RelayShield myRelays;
int relayOn(String command);
int relayOff(String command);


void setup() {
    //.begin() sets up a couple of things and is necessary to use the rest of the functions
    myRelays.begin();
    Particle.function("relayOn", relayOn);
    Particle.function("relayOff", relayOff);
}

void loop() {
    // Nothing needed here, functions will just run when called
}

int relayOn(String command){
    // Ritual incantation to convert String into Int
    char inputStr[64];
    command.toCharArray(inputStr,64);
    int i = atoi(inputStr);

    // Turn the desired relay on
    myRelays.on(i);

    // Respond
    return 1;
}

int relayOff(String command){
    // Ritual incantation to convert String into Int
    char inputStr[64];
    command.toCharArray(inputStr,64);
    int i = atoi(inputStr);

    // Turn the desired relay off
    myRelays.off(i);

    // Respond
    return 1;
}

I’ve cleaned up your code a bit. What exactly do you mean by “a momentary instance to a relay”? Do you want to delay for a second before turning a relay on or off?

// This #include statement was automatically added by the Particle IDE.
#include "RelayShield.h"

// Create an instance of the RelayShield library, so we have something to talk to
RelayShield myRelays;
int relayOn(const char* args);
int relayOff(const char* args);

void setup() {
        //.begin() sets up a couple of things and is necessary to use the rest of the functions
        myRelays.begin();
        Particle.function("relayOn", relayOn);
        Particle.function("relayOff", relayOff);
}

void loop() {
        // Nothing needed here, functions will just run when called
}

int relayOn(const char* args) {
        // Char array to int
        int i = atoi(args);

        // Turn the desired relay on
        myRelays.on(i);

        // Respond
        return 1;
}

int relayOff(const char* args) {
        // Char array to int
        int i = atoi(args);

        // Turn the desired relay off
        myRelays.off(i);

        // Respond
        return 1;
}

thanks @nrobinson2000

Yes, " Do you want to delay for a second before turning a relay on or off?"

You can put delay(1000); into relayOn() and relayOff()

yes but that case the relay will only have a delay? or i can keep on off option?

were is the line will be located in my firm?

// This #include statement was automatically added by the Particle IDE.
#include "RelayShield.h"

// Create an instance of the RelayShield library, so we have something to talk to
RelayShield myRelays;
int relayOn(const char* args);
int relayOff(const char* args);

void setup() {
        //.begin() sets up a couple of things and is necessary to use the rest of the functions
        myRelays.begin();
        Particle.function("relayOn", relayOn);
        Particle.function("relayOff", relayOff);
}

void loop() {
        // Nothing needed here, functions will just run when called
}

int relayOn(const char* args) {
        // Char array to int
        int i = atoi(args);

        // Turn the desired relay on
        myRelays.on(i);

        delay(1000); // HERE IS THE DELAY

        // Respond
        return 1;
}

int relayOff(const char* args) {
        // Char array to int
        int i = atoi(args);

        // Turn the desired relay off
        myRelays.off(i);

        delay(1000); // HERE IS THE DELAY

        // Respond
        return 1;
}

thanks

the on / off works fine
what is the on with delay command?

I think you want to clarify a bit better what you are after.

Do you want a function which

  • toggles the current state for one second and then return to the original state, or
  • switches ON for one second and back to OFF, or
  • switches OFF for one second and than back ON, or
  • all of that

However, this is not the most complex problem, so when you want to get into this kind of stuff, you should try to understand what and how your existing code does already and from that plus the info you got given by @nrobinson2000 you should be able invest some of your own brain power into the project to come up with a solution.

3 Likes

what I need is

“switch ON for one second and back to OFF”

thanks

You know how to switch on.
You know how to delay exection for 1 second.
You know how to switch off.
So you only need to put all three “tasks” together into one function.

5 Likes

thanks, i will look into it