Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2.  
  3. const int ledPin = 12;
  4. //A0 - LDR pin
  5. //A1 - potentiometer pin
  6.  
  7. //enumerator states is so we can give states a name
  8. typedef enum states {
  9. On = 1;
  10. Off = 0;
  11. roomLight = 2;
  12. }
  13.  
  14. //initiate state
  15. int currentState = roomLight;
  16.  
  17. void setup() {
  18. //setup pin behaiviour
  19. pinMode(ledPin, OUTPUT);
  20. pinMode(A0, INPUT);
  21. pinMode(A1, INPUT);
  22.  
  23. //start a serial for debugging
  24. Serial.begin(9600);
  25.  
  26. //roomLight state is somewhat the init state
  27. if(currentState == roomLight) {
  28. //proceed to the Off state which is expected at room light
  29. digitalWrite(ledPin, Off);
  30. }
  31. }
  32.  
  33. void loop() {
  34. //read the analogInput
  35. int potValue = analogRead(A1);
  36. int ldrValue = analogRead(A0);
  37.  
  38. //if reading shows its dark enough
  39. if(ldrValue <= potValue) {
  40.  
  41. //if the current state is On
  42. //there is no need to add delay nor do anything
  43. //otherwise check again for the read was valid
  44. if(currentState != On) {
  45.  
  46. //add a delay to reduce undesired interference
  47. delay(2000);
  48.  
  49. //if the value still satisfies the condition for switching
  50. if(ldrValue <= potValue) {
  51.  
  52. //turn it on
  53. digitalWrite(ledPin, On);
  54.  
  55. //set the new state
  56. currentState = On;
  57. }
  58. }
  59. }
  60. //if we are passed the point of light where we turn off
  61. else if(ldrValue >= potValue + 200) {
  62.  
  63. //if the current state is Off
  64. //there is no need to add delay nor do anything
  65. //otherwise check again for the read was valid
  66. if(currentState != Off) {
  67.  
  68. //add a delay to reduce undesired interference
  69. delay(2000);
  70.  
  71. //if the value still satisfies the condition for switching
  72. if(ldrValue >= potValue + 200) {
  73.  
  74. //turn it off
  75. digitalWrite(ledPin, Off);
  76.  
  77. //set the new state
  78. currentState = Off;
  79. }
  80. }
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement