Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. /*
  2. Home Automation with Arduino,Android,Bluetooth
  3.  
  4. Created 5 Dec 2016
  5. by Sayan Seth
  6. */
  7. #include <Servo.h>
  8.  
  9. Servo myservo;
  10.  
  11. #define ledPin1 13
  12. #define ledPin2 12
  13. #define servoPin 9
  14. #define thresh 500
  15. int state = 0;
  16. int flag=0; //check if led 1 is changing state.If it is in auto mode, check the lighting condition in every loop. If it is not, don't.
  17. void auto_light();
  18. void setup()
  19. {
  20. pinMode(ledPin1, OUTPUT);
  21. digitalWrite(ledPin1, LOW);
  22. pinMode(ledPin2, OUTPUT);
  23. digitalWrite(ledPin2, LOW);
  24. myservo.attach(servoPin);
  25. Serial.begin(9600); // Default communication rate of the Bluetooth module
  26. }
  27. void loop() {
  28. if(Serial.available() > 0) // Checks whether data is comming from the serial port
  29. {
  30. state = Serial.read(); // Reads the data from the serial port
  31. }
  32. if (state == '2')
  33. {
  34. digitalWrite(ledPin1, LOW); // Turn LED OFF
  35. state = 0;
  36. flag=1; //We are chnging state of LED 1, no need to run the auto function
  37. }
  38. if (state == '1')
  39. {
  40. digitalWrite(ledPin1, HIGH); // Turn LED ON
  41. state = 0;
  42. flag=1; // We are changing state of LED 1, no need to run the auto function
  43. }
  44. if (state == '4')
  45. {
  46. digitalWrite(ledPin2, LOW);
  47. state = 0;
  48.  
  49. }
  50. if (state == '3')
  51. {
  52. digitalWrite(ledPin2, HIGH);
  53. state = 0;
  54. }
  55. if (state == '5')
  56. {
  57. myservo.write(10);
  58. delay(1000);
  59. state = 0;
  60. }
  61. if (state == '6')
  62. {
  63. myservo.write(90);
  64. delay(1000);
  65. state = 0;
  66. }
  67. if (state == '7')
  68. {
  69. myservo.write(170);
  70. delay(1000);
  71. state = 0;
  72. }
  73. if (state == '8' || flag==2) //Auto mode is activated, run the auto function
  74. {
  75. auto_light();
  76. state = 0;
  77. }
  78. }
  79. void auto_light()
  80. {
  81.  
  82. int val;
  83. flag=2; // signifies that auto mode is on
  84. val = analogRead(A0);
  85. if(val>=thresh)
  86. {
  87. digitalWrite(ledPin1,LOW);
  88. }
  89. else
  90. digitalWrite(ledPin1, HIGH);
  91.  
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement