control de un led fisicamente y por bluetooth

buenas tengo un proyecto para la escuela y he decidido hacer una casa domotica controlada con el móvil por bluetooth la cosa esta en que he averiguado la forma de controlar un led simultáneamente por bluetooth y por un botón, pero la única forma que se me ha ocurrido ha sido utilizando dos botones. Querría saber si a alguien se le ocurre una forma de usar solo un botón. os pongo el programa para que os hagáis una idea muchas gracias! :smiley:

const int boton = 49;
const int boton2 = 47;
const int ledPin = 53;
int var = 0;
int buttonState = 0;
int buttonState1 = 0;
String readString;
void setup() {

  • Serial.begin(9600);*

  • pinMode(ledPin, OUTPUT);*

  • pinMode(boton, INPUT); *
    }
    void loop() {

  • buttonState = digitalRead(boton);*

  • if (buttonState == HIGH) { *

  • var = 1; *

  • }*

  • buttonState1 = digitalRead(boton2);*

  • if (buttonState1 == HIGH) { *

  • var = 2; *

  • }*

  • while (Serial.available()) {*

  • delay(3); *

  • char c = Serial.read();*

  • readString += c;*

  • }*

  • if (readString.length() >0) {*

  • Serial.println(readString);*

  • if (readString == "q") *

  • {*

  • var = 1;*

  • }*

  • if (readString == "w")*

  • {*

  • var = 2;*

  • }*

  • readString="";*
    }
    switch ( var )
    {
    case 1:
    digitalWrite(ledPin, HIGH);
    break;

case 2:
digitalWrite(ledPin, LOW);
break;
}
}

si lo que quieres es prender y apagar algo usando una sola variable que cambie de estado cuando es recibida lo único que debes hacer es esto.

defines una variable que almacene el estado anterior.
acá un ejemplo distinto pero que hace lo que necesitas a nivel botón o pulsador.

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      Serial.println("on");
    }
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;

 
  // Prende/Apaga con cada pulsada.

  if (buttonState) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
 
}

muchas gracias por la respuesta pero por lo que entiendo de lo que me has enviado no se podría encender el led por bluetooth. Explico mas detalladamente, por ejemplo enciendo el led por botón y lo apago por bluetooth o viceversa o lo enciendo por el bluetooth y lo apago por el botón o lo enciendo y apago por bluetooth etc. Eso con el programa que he puesto arriba lo he conseguido pero con un botón para apagar y otro para encender y lo que quiero es usar un solo botón.
Muchas gracias!!

Claro que se puede.
Pero me acabo de dar cuenta que esta mal lo que te di.
dejame verlo y te respondo.

ya lo he conseguido muchas gracias acabe el programa que me enviastes y me ha funcionado. cuelgo el programa por si a alguien en el futuro le puede servir.

const int boton = 49;
const int led = 53;
int estado = 0;
int estado_anterior = 0;
int LED = 0;
String readString;

void setup() {
pinMode(led, OUTPUT);
pinMode(boton, INPUT);
Serial.begin(9600);
}

void loop(){

estado = digitalRead(boton);

if((estado == HIGH) && (estado_anterior == LOW)){
LED = 1 - LED;
delay(250);
}
estado = estado_anterior;
if (LED==1){
digitalWrite(led, HIGH);
}else {
digitalWrite(led, LOW);
}

while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
if (readString == "q")
{
LED = 1;
}
if (readString == "w")
{
LED = 0;
}

readString="";
}}