Advertisement
Guest User

Echy's IR sensor switch.

a guest
Nov 22nd, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. /*
  2.  *  This sketch is using an 8 channel multiplexer[4051] (because learning shit) to read digital sensory input from two IR sensor modules[cheap chinese crap] and turn on / off a LED array[DIY BS array]
  3.  *  Sweeping a hand over the sensors will turn on or turn off the lights depending on in which direction sweep is performed
  4.  *  
  5.  *  Note:
  6.  *  Is the light from the IR diodes enough to be harmful? I should probably burst the IREDs at timely interval, and only keep the other sensor on once the first one is triggered
  7.  *  This would require pulsing the entire modules since they have no enable pin... or it would be enough to keep them away from direct eye contact.
  8.  */
  9.  
  10.  
  11.  
  12. // pins
  13. int s0Pin = 2; // mux control
  14. int s1Pin = 4; // mux control
  15. int s2Pin = 7; // mux control
  16. int sensorPin = 8;  // sensory input via mux
  17. int LEDEnablePin = 12;
  18.  
  19. // select values for mux control
  20. bool s0 = 0;  
  21. bool s1 = 0;
  22. bool s2 = 0;
  23.  
  24. // stuff used in sketch
  25. int count = 0;  // used for mux control
  26. bool sensorValues[8]; // storing 8 digital sensor values
  27. unsigned long milliTimer = 0; // probably not needed but might be quicker than calling 'millis()' all the time and it ensures that there are no rollover issues
  28. unsigned long milliOld = 0; // timer control for sweep
  29. bool sensor0Pushed = 0; // sweep control
  30. bool sensor1Pushed = 0; // sweep control
  31. int testTime = 300;  // sweep timeout in milliseconds
  32.  
  33. void setup() {
  34.  
  35. // only for testing
  36. Serial.begin(9600);
  37.  
  38. // sort pin modes
  39. pinMode (s0Pin, OUTPUT);
  40. pinMode (s1Pin, OUTPUT);
  41. pinMode (s2Pin, OUTPUT);
  42. pinMode (sensorPin, INPUT);
  43. pinMode (LEDEnablePin, OUTPUT);
  44.  
  45. }
  46.  
  47. void loop() {
  48.  
  49. // not sure if this timer at all is needed as long as I stick to unsigned long for timing, but it's probably faster to read millis() only once per loop anyway:
  50. milliTimer = millis();
  51.  
  52.  
  53. // git da infurmaichun from sensors via 4051 multiplexer
  54. // smart AF way to decode/code bits i found on the interwebs
  55. for (count=0; count<=7; count++){
  56.   // select the bit, reads bit from bitRead(numeric, bit# from right to left)
  57.   s0 = bitRead(count, 0);
  58.   s1 = bitRead(count, 1);
  59.   s2 = bitRead(count, 2);
  60.  
  61.   //writes the bits into selector pins
  62.   digitalWrite (s0Pin, s0);
  63.   digitalWrite (s1Pin, s1);
  64.   digitalWrite (s2Pin, s2);
  65.  
  66.   //  'Serial' used for testing sensor values
  67.   /*
  68.   Serial.print ("Mux #");
  69.   Serial.print (count);
  70.   Serial.print (": ");
  71.   Serial.println (digitalRead(sensorPin));
  72.   */
  73.  
  74.   // store the sensory data
  75.   sensorValues[count] = digitalRead(sensorPin);
  76. }
  77.  
  78. // sweep left to turn off
  79. if (sensorValues[0] == 0 && milliTimer - milliOld >= testTime){
  80.   milliOld = milliTimer;
  81.   sensor0Pushed = 1;
  82.   Serial.println("sensor0 touched");
  83. }
  84. if (sensorValues[1] == 0 && milliTimer - milliOld < testTime && sensor0Pushed == 1){
  85.   digitalWrite (LEDEnablePin, LOW);
  86.   sensor0Pushed = 0;
  87.   Serial.println("lamp turned OFF");
  88. }
  89.  
  90. // sweep right to turn on
  91. if (sensorValues[1] == 0 && milliTimer - milliOld >= testTime){
  92.   milliOld = milliTimer;
  93.   sensor1Pushed = 1;
  94.   Serial.println("sensor1 touched");
  95. }
  96. if (sensorValues[0] == 0 && milliTimer - milliOld < testTime && sensor1Pushed == 1){
  97.   digitalWrite (LEDEnablePin, HIGH);
  98.   sensor1Pushed = 0;
  99.   Serial.println("lamp turned ON");
  100. }
  101.  
  102. // reset shit after failed operation
  103. if (milliTimer - milliOld >= testTime){
  104.   sensor0Pushed = 0;
  105.   sensor1Pushed = 0;
  106. }
  107.  
  108. // 'millis()' rollover safety(?)
  109. if (milliTimer < milliOld){
  110.   milliOld = milliTimer;
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement