Help on school project, first time user of a Particle Photon

I’m having an issue with my project, which is an automatic door that is controlled by google assistant that is connected to a particle which is then connected to a servo to open and close my door. I have my code working and all even Google is working with my code, my only problem is that my code and google only works after flashing that certain coded onto my particle. For instance, if I flash my opening door code to the particle and ask Google to open the door it will do so with ease but if then ask Google to close my door nothing will happen until I flash my closing door code onto the particle and my door would be able to close but then the particle won’t be able to open my door without having to flash the opening door code back again. Please help this is my final project in school.

The problem may lie in your code, then.

Did you plan on posting what you have?

As @BulldogLowell said, seeing code would be helpful. But it sounds like your code runs once and doesn’t go back into a state where it’s listening for the google commands. Do you set some sort of variable to track that a google command was received and then another variable to track what state the door is in (open vs closed)? If so, do you reset the command received variable? Does your code allow the door state variable to flip flop from one state to the other when appropriate?

I assume you are using a Photon, correct? Does the Photon status LED show anything other than breathing Cyan after it runs the first google command and then becomes unresponsive? If so, then your code might get stuck in a loop or the code blocks and never returns back to the main loop.

Door Opening Code:

Servo door;
Servo door2;
void setup() 
{
door.attach(D2);
door2.attach(D3);
Particle.subscribe("Door_Command", doorFunction);
}
void loop() 
{
}
void doorFunction(const char *event, const char *data)
{
 door.write(0);
 door2.write(145);
}

Door Closing Code:

Servo door;
Servo door2;
void setup() 
{
door.attach(D2);
door2.attach(D3);
Particle.subscribe("Door_Close", doorFunction);
}
void loop() 
{
}
void doorFunction(const char *event, const char *data)
{
 door.write(0);
 door2.write(-145);
}

These are my 2 codes and they are both separate codes

You should really combine both of these into one.

Here is a way:

Servo door;
Servo door2;

void setup()
{
    door.attach(D2);
    door2.attach(D3);
    Particle.subscribe("door-open", doorOpen);
    Particle.subscribe("door-close", doorClose);
}

void loop()
{

}

void doorOpen(const char *event, const char *data)
{
    door.write(0);
    door2.write(145);
}

void doorClose(const char *event, const char *data)
{
    door.write(0);
    door2.write(-145);
}
1 Like

Oh… I see what you are doing. You can handle this all in one code. You can add 2 particle subscribes in your setup; one for open and one for close. Add a door function for each state but probably rename the function from doorFunction to doorOpen and doorClose. When you receive the open command, call the open function. When you receive the close command, call the close function. Is that clear enough?

1 Like

I just made that exact code. :laughing:

1 Like

@nrobinson2000, I was too slow :frowning:

1 Like

another way:

Servo door;
Servo door2;

void setup()
{
  door.attach(D2);
  door2.attach(D3);
  Particle.function("door", manageDoor);
}

void loop()
{

}

int manageDoor(String command) {
  if (command == "open"){
    door.write(0);
    door2.write(145);
    return 1;
  } else if (command == "close") {
    door.write(0);
    door2.write(-145);
    return 0;
  }
  return -99;
}
1 Like

Thank you for the help it means a lot