Devices & Components
Arduino Nano
HC-05 Bluetooth Module
Tactile Switch, Top Actuated
Hook Up Wire Kit, 22 AWG
Relay Module (Generic)
Hardware & Tools
Cutter
Tape, Masking
Software & Tools
Arduino IDE
Project description
Code
The code
cpp
1#include <SoftwareSerial.h> 2SoftwareSerial MyBlue(8,10); 3 4String 5 message; 6char character; 7int lastButtonState = 0; 8 9int buttonPin = 3; 10int 11 relayPin = 4; 12 13void setup() { 14 Serial.begin(9600); 15 MyBlue.begin(9660); 16 17 18 pinMode(buttonPin, INPUT); 19 pinMode(relayPin, OUTPUT); 20} 21 22void 23 loop() { 24 while(MyBlue.available()){ 25 character = MyBlue.read(); 26 27 if(character == '#'){ 28 Serial.println(message); 29 switch(message.toInt()){ 30 31 case 1: 32 Serial.println("on bluetooth"); 33 digitalWrite(relayPin,HIGH); 34 35 break; 36 case 0: 37 Serial.println("off bluetooth"); 38 39 digitalWrite(relayPin,LOW); 40 break; 41 } 42 message 43 = ""; 44 Serial.println(); 45 }else{ 46 Serial.println(character); 47 48 message.concat(character); 49 } 50 } 51 if (digitalRead(buttonPin) 52 == HIGH) { 53 if(lastButtonState == 0){ 54 lastButtonState = 1; 55 Serial.println("on"); 56 57 digitalWrite(relayPin,HIGH); 58 }else if (lastButtonState == 1){ 59 Serial.println("off"); 60 61 lastButtonState = 0; 62 digitalWrite(relayPin,LOW); 63 } 64 delay(300); 65 66 } 67}
Downloadable files
The App
https://github.com/dzh121/Bluetooth-Light-Switch/blob/main/app.apk
Comments
Only logged in users can leave comments