Go Down

Topic: Control devices with both app bluetooth and wall push buttons! (Read 896 times) previous topic - next topic

odi

Hello everybody!I have a problem and i would like your help!I am not very good with codes but i learned some things,as a result i made an app with arduino and bluetooth in order to control 2 devices or lights!Now i want a way to control this devices with both app and wall push buttons!For instance,when i turn on the led2 via app, i want to turn it off via app or wall push button.Also when i turn off the the led2 via app or wall push button i want to turn it on via app or push button!I spent a lot of hours and days but i cannot find the way.Above is the code of arduino.There is also a zip archive!

Code: [Select]

char incomingByte;
int LED2 = 2;
int LED3 = 3;
const int buttonPin1 = 7;  // the number of the pushbutton pin
const int buttonPin2 = 8;

int  ledState = HIGH;        // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers


void setup() {
Serial.begin(9600);

 
 pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
 
 pinMode(LED2, OUTPUT);
 pinMode(LED3, OUTPUT);

 digitalWrite(LED2, ledState);
 digitalWrite(LED3, ledState);

}

void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.read();

if(incomingByte == '2') {digitalWrite(LED2, HIGH); }
if(incomingByte == '6')      {digitalWrite(LED2, LOW); }

if(incomingByte == '4') {digitalWrite(LED3, HIGH); }
if(incomingByte == '8')      {digitalWrite(LED3, LOW); }








}
 int reading = digitalRead(buttonPin1);
 
 
if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
     
      }
    }

// set the LED:
  digitalWrite(LED2, ledState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;


   
  }


 int now= digitalRead(buttonPin2);
 
 
if (now != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (now != buttonState) {
      buttonState = now;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
     
      }
    }

// set the LED:
  digitalWrite(LED3, ledState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = now;


   
  }



 

 

}

odi

Also i post the arduino code without buttons!As i said, i want to put button in order to control lights with both app and wall push buttons!How can i do this?Has anyone done it?

Thanks!!

char incomingByte;
int LED2 = 2;
int LED3 = 3;

void setup() {
Serial.begin(9600);

digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);

pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);

}
void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.read();

if(incomingByte == '2') {digitalWrite(LED2, HIGH); }
if(incomingByte == '6') {digitalWrite(LED2, LOW); }

if(incomingByte == '4') {digitalWrite(LED3, HIGH); }
if(incomingByte == '8') {digitalWrite(LED3, LOW); }

}
}

odi

Here is what i did!
https://www.youtube.com/watch?v=PBD0lG8jAbc

https://www.youtube.com/watch?v=7p6CapfJykE      

odi


ieee488

With a wall push button you would need to wire it directly to the Arduino and monitor it.

odi

Hello ieee488, i connected the push button with arduino but i am doing something wrong with the code and it does not work correctly!Arduino drives relay!I want to turn on-off the Led2 for example with one wall push button or app
I would like the help of someone who is good with code or did it!
above is a photo of connection:

ieee488

Perhaps you should consider learning how to make the wall push button part to work and then put the two parts together.  Sounds like your code doesn't work for that.


odi

I found this and i tested!It works good!When i put other push buttons and led,it does not work! I make something wrong! how can i put 5  push buttons and 5 led inside in this sketch?

Code: [Select]



=========== Comm Legend ===========
 10 - LED Off
 20 - LED On
255 - Request from app for local variable status
===================================


Created by Matt Kachur
Dec 27 2015

This example code is in the public domain.
*/



#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // Sets Arduino (RX, TX) pins
#define defLEDpin 13
#define defButtonpin 8

byte lastRead;
byte currentRead;
byte ledStatus = 10;  // initialize LED in OFF state

int buttonState = LOW;
int lastButtonState = LOW;
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 20;    // the debounce time; increase if the output flickers



void setup() {
    pinMode(defLEDpin, OUTPUT);  // LED pin
    pinMode(defButtonpin, INPUT);  // Button pin
    digitalWrite(defLEDpin, LOW); //initialize with LED Off

    mySerial.begin(9600);
}



void loop() {

    // if there's any serial bytes available, read them:
    while (mySerial.available() > 0) {

        currentRead = mySerial.read();

        // Read all bytes until a zero is received, store the bytes received
        // before the zero.
        if (currentRead != NULL) {
            lastRead = currentRead;
        }

        // once a zero is received, evaluate all the bytes read before the zero
        if (currentRead == NULL) {

            if (lastRead == 255) {  // 255 is request for current status
                // send back LED status
                mySerial.write(ledStatus);
            }

            if (lastRead == 10) {
                ledStatus = 10;
                digitalWrite(defLEDpin, LOW);
                mySerial.write(ledStatus);
            }

            if (lastRead == 20) {
                ledStatus = 20;
                digitalWrite(defLEDpin, HIGH);
                mySerial.write(ledStatus);
            }

        }
    }



    // LOCAL BUTTON LED CONTROL

    // read the state of the button into a local variable
    int buttonReading = digitalRead(defButtonpin);

    // If the button state has changed, due to noise OR pressing
    if (buttonReading != lastButtonState) {
        // reset the debouncing timer
        lastDebounceTime = millis();
    }

    // check to see if the current state has been the same for longer than
    // the debounce delay, if true make, it the current state
    if ((millis() - lastDebounceTime) > debounceDelay) {
        // if the "new" button state is different from the current,
        // take actions for a button press
        if (buttonReading != buttonState) {
            buttonState = buttonReading;
            if (buttonState == HIGH) {
                if (ledStatus == 10) {
                    ledStatus = 20;
                    digitalWrite(defLEDpin, HIGH);
                }
                else {
                    ledStatus = 10;
                    digitalWrite(defLEDpin, LOW);
                }
            }
        }
    }

    // Save the current button state so it is updated for the next loop iteration
    lastButtonState = buttonReading;

}









odi

It seems that no one cares!  [I do! Please use code tags, the </> button. And delete the extra blank lines.  Thanks, Moderator]
I added an extra button and led to the previous sketch.I don't know if it is correct but it seems to work well with each push button!The modification that i did to app inventor does not work and the app stucks!
i appreciate if someone can help.I include a zip of arduino code and aia app inventor.
Code: [Select]

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // Sets Arduino (RX, TX) pins
#define defLEDpin 13
#define defButtonpin 8

byte lastRead;
byte currentRead;
byte ledStatus = 10;  // initialize LED in OFF state

int buttonState = LOW;
int lastButtonState = LOW;
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 20;    // the debounce time; increase if the output flickers

//above are the data that i add for the second button and led.

#define defLEDpin2  4
#define defButtonpin2  7

byte currentRead2;
byte ledStatus2 = 30;//// initialize LED in OFF state

int buttonState2 = LOW;
int lastButtonState2 = LOW;

void setup() {
  pinMode(defLEDpin, OUTPUT);  // LED pin
  pinMode(defButtonpin, INPUT);  // Button pin
  digitalWrite(defLEDpin, LOW); //initialize with LED Off

//data for second button and led

 pinMode(defLEDpin2, OUTPUT);  // LED pin
  pinMode(defButtonpin2, INPUT);  // Button pin
  digitalWrite(defLEDpin2, LOW); //initialize with LED Off

  mySerial.begin(9600);
}

void loop() {
 
  // if there's any serial bytes available, read them:
  while (mySerial.available() > 0) {
   
    currentRead = mySerial.read();
   
    // Read all bytes until a zero is received, store the bytes received
    // before the zero.
    if (currentRead != NULL) {
      lastRead = currentRead;
    }

    // once a zero is received, evaluate the bytes read before the zero
    if (currentRead == NULL) {
 
      if (lastRead == 255) {  // 255 is request for current status
        // send back LED status
        mySerial.write(ledStatus);
      }
     
      if (lastRead == 10) {
        ledStatus = 10;
        digitalWrite(defLEDpin, LOW);
        mySerial.write(ledStatus);
      }
     
      if (lastRead == 20) {
        ledStatus = 20;
        digitalWrite(defLEDpin, HIGH);
        mySerial.write(ledStatus);
      }
     
    }
  }

  // for the second button and led
// if there's any serial bytes available, read them:
  while (mySerial.available() > 0) {
   
currentRead2 = mySerial.read();
   
    // Read all bytes until a zero is received, store the bytes received
    // before the zero.
    if (currentRead2 != NULL) {
      lastRead = currentRead2;
    }
   
    // once a zero is received, evaluate the bytes read before the zero
    if (currentRead2 == NULL) {
 
      if (lastRead == 255) {  // 255 is request for current status
        // send back LED status
        mySerial.write(ledStatus2);
      }
     
      if (lastRead == 30) {
        ledStatus2 = 30;
        digitalWrite(defLEDpin2, LOW);
        mySerial.write(ledStatus2);
      }
     
      if (lastRead == 35) {
        ledStatus2 = 35;
        digitalWrite(defLEDpin2, HIGH);
        mySerial.write(ledStatus2);
      }
     
    }

  }
 
  // LOCAL BUTTON LED CONTROL
 
  // read the state of the button into a local variable
  int buttonReading = digitalRead(defButtonpin);
 
  // If the button state has changed, due to noise OR pressing
  if (buttonReading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
 
  // check to see if the current state has been the same for longer than
  // the debounce delay, if true make, it the current state
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // if the "new" button state is different from the current,
    // take actions for a button press
    if (buttonReading != buttonState) {
      buttonState = buttonReading;
      if (buttonState == HIGH) {
        if (ledStatus == 10) {
          ledStatus = 20;
          digitalWrite(defLEDpin, HIGH);
        }
        else {
          ledStatus = 10;
          digitalWrite(defLEDpin, LOW);
        }
      }
    }
  }

     // LOCAL BUTTON LED CONTROL for the second led and button
 
  // read the state of the button into a local variable
  int buttonReading2 = digitalRead(defButtonpin2);
 
  // If the button state has changed, due to noise OR pressing
  if (buttonReading2 != lastButtonState2) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
 
  // check to see if the current state has been the same for longer than
  // the debounce delay, if true make, it the current state
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // if the "new" button state is different from the current,
    // take actions for a button press
    if (buttonReading2 != buttonState2) {
      buttonState2 = buttonReading2;
      if (buttonState2 == HIGH) {
        if (ledStatus2 == 30) {
          ledStatus2 = 35;
          digitalWrite(defLEDpin2, HIGH);
        }
        else {
          ledStatus2 = 30;
          digitalWrite(defLEDpin2, LOW);
        }
      }
    }
  }

 
   // Save the current button state so it is updated for the next loop iteration
  lastButtonState = buttonReading;

   // Save the current button state so it is updated for the next loop iteration
  lastButtonState2 = buttonReading2;
 
}

ieee488

It's a matter of remembering the state of the light.
Then continuously checking for a command from the app or a press of the wall button.
If one of them happens, turn off or turn on the light depending on its previous state.
Remember the new state.

I really don't want to spend anytime debugging your code. 

devmandhan

i am having the same problem. if Bluetooth begins then wall button not working and if wall button works Bluetooth commands fails. how to control 1 relay switch with to different inputs

craigcurtin

OK as both of your states are controlled by the Arduino (i assume the Bluetooth app is talking to the arduino through some shield ?) then you need to think of the concept of a Virtual Button (State)

Firstly - what sort of button is it ? Is it a momentary push or is it a latching button ?

You need to poll that that input and see if it changes state from on (latching button) or momentary high (momentary button

When it does - use this to set a variable (say) Button_Press = Yes - have a 2nd variable LED_State = High

Do a test (pseudo code)

If Button_Press = Yes

    If LED_State = High, LED_State=Low
      Else If LED_State = Low, LED_State = High
    End If
End If

Do the same for the Bluetooth action when it is received by the Sketch

Craig


Go Up