Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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]
- * Sweeping a hand over the sensors will turn on or turn off the lights depending on in which direction sweep is performed
- *
- * Note:
- * 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
- * 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.
- */
- // pins
- int s0Pin = 2; // mux control
- int s1Pin = 4; // mux control
- int s2Pin = 7; // mux control
- int sensorPin = 8; // sensory input via mux
- int LEDEnablePin = 12;
- // select values for mux control
- bool s0 = 0;
- bool s1 = 0;
- bool s2 = 0;
- // stuff used in sketch
- int count = 0; // used for mux control
- bool sensorValues[8]; // storing 8 digital sensor values
- 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
- unsigned long milliOld = 0; // timer control for sweep
- bool sensor0Pushed = 0; // sweep control
- bool sensor1Pushed = 0; // sweep control
- int testTime = 300; // sweep timeout in milliseconds
- void setup() {
- // only for testing
- Serial.begin(9600);
- // sort pin modes
- pinMode (s0Pin, OUTPUT);
- pinMode (s1Pin, OUTPUT);
- pinMode (s2Pin, OUTPUT);
- pinMode (sensorPin, INPUT);
- pinMode (LEDEnablePin, OUTPUT);
- }
- void loop() {
- // 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:
- milliTimer = millis();
- // git da infurmaichun from sensors via 4051 multiplexer
- // smart AF way to decode/code bits i found on the interwebs
- for (count=0; count<=7; count++){
- // select the bit, reads bit from bitRead(numeric, bit# from right to left)
- s0 = bitRead(count, 0);
- s1 = bitRead(count, 1);
- s2 = bitRead(count, 2);
- //writes the bits into selector pins
- digitalWrite (s0Pin, s0);
- digitalWrite (s1Pin, s1);
- digitalWrite (s2Pin, s2);
- // 'Serial' used for testing sensor values
- /*
- Serial.print ("Mux #");
- Serial.print (count);
- Serial.print (": ");
- Serial.println (digitalRead(sensorPin));
- */
- // store the sensory data
- sensorValues[count] = digitalRead(sensorPin);
- }
- // sweep left to turn off
- if (sensorValues[0] == 0 && milliTimer - milliOld >= testTime){
- milliOld = milliTimer;
- sensor0Pushed = 1;
- Serial.println("sensor0 touched");
- }
- if (sensorValues[1] == 0 && milliTimer - milliOld < testTime && sensor0Pushed == 1){
- digitalWrite (LEDEnablePin, LOW);
- sensor0Pushed = 0;
- Serial.println("lamp turned OFF");
- }
- // sweep right to turn on
- if (sensorValues[1] == 0 && milliTimer - milliOld >= testTime){
- milliOld = milliTimer;
- sensor1Pushed = 1;
- Serial.println("sensor1 touched");
- }
- if (sensorValues[0] == 0 && milliTimer - milliOld < testTime && sensor1Pushed == 1){
- digitalWrite (LEDEnablePin, HIGH);
- sensor1Pushed = 0;
- Serial.println("lamp turned ON");
- }
- // reset shit after failed operation
- if (milliTimer - milliOld >= testTime){
- sensor0Pushed = 0;
- sensor1Pushed = 0;
- }
- // 'millis()' rollover safety(?)
- if (milliTimer < milliOld){
- milliOld = milliTimer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement