Coding Servo Motor Problem

I’m new to this so its taking me a little bit to get things working. I’m attempting to write a code that will move from position 135 at a pin Write being HIGH and 45 at the reading being LOW. The idea is to be able to turn the light to my room on or off from my phone. I also plan on using a light sensor that will be able to tell me if the light is actually on or off, however that’s further down the line. My current code that I tried looks like this;

Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 

int pos = 135;    // variable to store the servo position 

void setup() 
{ 
  myservo.attach(A5);  // attaches the servo on pin 0 to the servo object 
} 


void loop() 
{ 
  if (digitalRead(A0) == HIGH)
  {
    pos = 45;
    myservo.write(pos);
    delay(15);
  }

  if (digitalRead(A0) == LOW)
  {
    pos = 135;
    myservo.write(pos);
    delay(15);
  }
}

My servo is connected to pin A5. I placed this in a new app on the Particle build platform and flashed it and there were no issues, however when I go to the app from my phone I’m not getting the result I’m looking for. I realize that i need to place the code in the Tinker app already installed with the device, however when I try to place this code (or something similar into the tinker app I get an error. Does anyone have advice or direction for me? It would be greatly appreciated.

-PB

You need to set A0 to be an INPUT and I’d guess a 15ms delay is a bit short for your servo to move all the way to the on position.

BTW: If you use switch, the usually have an open state which does not provide either HIGH or LOW but let the pin float. For that reason you need a pull-resistor.
The easiest way to add one is be use of pinMode(A0, INPUT_PULLDOWN) when your switch closes to 3V3 (don’t close to Vin! otherwise you might damage your pin) - saver is closing to GND with INPUT_PULLUP and reverting the logic HIGH <--> LOW.

Also you only need one digitalRead() and an if() ... else

  if (digitalRead(A0))
  {
    pos = 45;
    myservo.write(pos);
    delay(5000); // five sec should do
  }
  else
  { // default state
    pos = 135;
    myservo.write(pos);
  }