Using a simple cloud function to turn on relays but they don't remain on

Hi there,
i’m using a simple cloud function to turn on and off 4 different relays. I tested the hardware using the iOS Particle app Tinker firmware first and I’m able to run all the relays on and off. Now i used this code below and when i send a msg to enable0 or the other ports of the relays they turn on for a few seconds and turn back off. I’m not sure what is wrong.

void setup() {
    Spark.function("parse", parse);
    
    // 0-7 maps to D0-D7
    for (int i = 0; i <= 7; i++) {
        pinMode(i, OUTPUT);
    }
}

void loop() {

}

int parse(String cmd) {
    cmd.trim();
    cmd.toUpperCase();
    
    if (cmd.startsWith("ENABLE")) {
        int pin = cmd.substring(6).toInt();
        digitalWrite(pin, HIGH);
    }
    if (cmd.startsWith("DISABLE")) {
        int pin = cmd.substring(7).toInt();
        digitalWrite(pin, LOW);
    }
}

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