Taking time inputs from the user and setting a light to go on during the set duration

Hey there!
I’ve been trying to implement a new function in my smart lamp project where the code would take 4 inputs from the user: start hour, start minute, end hour, and end minute in order to turn my led on during a desired duration. Whenever I’m inputting commands from the Particle app, the led refrains from turning on during the time i’ve set. I’ve attached my code below.
I think the problem has to do with the action of taking a string from the user and converting it to an int, and that being unable to be passed to the alarm function.
Any feedback would be a great help! Thanks!

// This #include statement was automatically added by the Particle IDE.
#include <TimeAlarms.h>

int flag=0;
int led=D7;
int A,B,C,D;
void powswtch()
    if(flag==0) {
    else if(flag==1) {

int setA(String command) {
    A = command.toInt();
    return A;

int setB(String command) {
    B = command.toInt();
    return B;

int setC(String command) {
    C = command.toInt();
    return C;

int setD(String command) {
    D = command.toInt();
    return D;

void setup()
    Alarm.alarmRepeat(A,B,0, powswtch); 
    Alarm.alarmRepeat(C,D,0, powswtch); 

void loop() {

You set the values correctly in the function calls, but then never seem to actually pass them on to the Alarm, since that’s configured in setup. Have another look at that :slight_smile:


In addition to the main point addressed already, global variables should best be named in some readable manner - using anonymous one-letter variable names may be OK for a very limited scope, but globals should be self-explanatory.

I’d also suggest you use some formatted input string to provide the data at once (with sanity checks) - e.g. "1=hh:mm" and "0=hh:mm" and parse the data like this

int setSchedule(const char* arg) {
  int state = 0;
  int hh = 0;
  int mm = 0;

  if (sscanf(arg, "%d=%d:%d", &state, &hh, &mm) != 3) return -1; // bad format
  if (0 > state || state > 1)                         return -2; // dissallowed state
  if (0 > hh || hh > 59 || 0 > mm || mm > 59)         return -3; // invalid time
  // set your timers accordingly 

  return 1000*state + 100*hh + 1*mm;                             // return a combination of all values to report success

I’d also use two timers one dedicated for on, the other for off.
If you only toggle the current state, your on times may eventually switch the off and vice versa.