Switching between two codes

I have a code which reads a light sensor and spins a servo corresponding to the value of the light, and a code that connects the servo to an app that I made and you can manually control it. I’m having trouble getting it to switch between the two. What I originally wanted was for it to see if the Bluetooth was connected to a phone and switch if it wasn’t, but I changed it to change codes depending on if a button was pressed. The two codes work individually, but not when combined. I don not know what’s wrong with the code, it only reads the light sensor when combined

int programState = 0;
#include <Servo.h>

#include <SoftwareSerial.h>

Servo myservo;
 int photocellPin=0;
 int servoPin=9;
 int pos = 0; 
int bluetoothTx = 10;

int bluetoothRx = 11;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
  // put your setup code here, to run once:


//Setup USB serial connection to computer


//Setup Bluetooth serial connection to android

void loop()
  if(digitalRead(0)) //Button is pressed
     programState = 1 - programState; //Flip-Flop state between 0 and 1

  if(programState == 0) //Check to see which state to run
     //Read from Bluetooth and write to USB serial

if(bluetooth.available()> 0 )


int servopos = bluetooth.read();




 pos=constrain(pos, 220, 740);

 int servoPos = map(pos, 220, 740, 225, 0);
 int servoDegree = map(servoPos, 225,0, 0, 179);
 Serial.print("Servo Degree=");

the if statement sets up the Bluetooth module so that the servo can be controlled by an Android app
the else statement makes if so that the servo is controlled by the value getting sent from the photocell that’s attached
Each work individually but when placed into an if-else statement it only reads the else

The Arduino should switch between the two codes by pressing a button

The code you posted incorrectly (code tags, not quote tags) does something. I don't intend to guess what it does.

You want it to do something. Presumably, what you want it to do is not the same as what it actually does, but I couldn't guess, if I even wanted to, what you want it to do.

Have you read this:-

My question to OP: I wasn't aware that the software serial port works at 115200. When you are doing bluetooth only, were you using hardware of software serial port?

The bluetooth sensor is a Bluesmirf which communicates at 115200 buad rate

The bluetooth sensor is a Bluesmirf which communicates at 115200 buad rate

I was asking about the arduino that has to receive that baud rate. Have you tested with simple code that software serial can function with that baud rate? FYI, servos also use interrupts so the chances of communicating at high baud rate with interrupt-driven software serial are lowered with the servo.