// This sketch use a DMX shield with DmxSimple librairie // A photoelectric sensor used to count people : ... // A digital display to graph people count : Serial-7-Seg from Sparkfun // maurin.box@gmail.com // DmxSimple is available from: http://code.google.com/p/tinkerit // Help and support: http://groups.google.com/group/dmxsimple // TIMER2 id used by DMXSimple #include #include #define sensPin 9 // people sensor input pin int sensVal = 0; // sensor value #define sensSwitchPin 4 // switch sensor input pin int sensSwitchVal = 0; // sensor value long Time = 0; // time int debounceTime = 10; // debounce time int peopleCount = 0; // people count int maxPeopleCount = 5000; // set maximum count boolean peopleCountState = true; boolean DEBUG = false; void setup(){ pinMode(sensPin, INPUT); // set pin as INPUT digitalWrite(sensPin, HIGH); // turn on pullup resistor pinMode(sensSwitchPin, INPUT); // set pin as INPUT digitalWrite(sensSwitchPin, HIGH); // turn on pullup resistor /* The most common pin for DMX output is pin 3, which DmxSimple ** uses by default. If you need to change that, do it here. */ DmxSimple.usePin(11); // TIMER2 is pin 3 & 11 /* DMX devices typically need to receive a complete set of channels ** even if you only need to adjust the first channel. You can ** easily change the number of channels sent here. If you don't ** do this, DmxSimple will set the maximum channel number to the ** highest channel you DmxSimple.write() to. */ // DmxSimple.maxChannel(4); Serial.begin(9600); if(!DEBUG)displayInit(); DmxSimple.write(1, 0); // DMX cannon OFF DmxSimple.write(2, 0); // DMX light OFF } void loop(){ sensSwitchVal = digitalRead(sensSwitchPin); // get switch value sensVal = digitalRead(sensPin); // get sensor value if( (change(&sensVal) == true) && (sensVal==LOW) && (Time>=debounceTime) && (peopleCountState==true) ){ Time = 0; // init debounceTime peopleCount++; if(!DEBUG) digit_print(peopleCount); // print people count on DIGIT screen if(DEBUG) Serial.println(peopleCount); // print people count on serial delay(10); } if( (change(&sensSwitchVal) == true) && (sensSwitchVal==LOW) && (peopleCountState==true) ){ peopleCount = 4980; // Set people count to 4980 if(!DEBUG) digit_print(peopleCount); // print people count on DIGIT screen if(DEBUG) Serial.println(peopleCount); // print people count on serial delay(10); } if( (peopleCount >= maxPeopleCount) && (peopleCountState == true) ){ Time = 0; // init debounceTime DmxSimple.write(1, 255); // DMX cannon ON DmxSimple.write(2, 255); // DMX light ON delay(10); peopleCountState = false; } Time++; // inc debounce Time delay(5); } /////////////////////////////////////////////////////////////////// ///////////// if input values change output is true int change(int *val){ // *pointeur static int lastVal = 0; if(*val != lastVal){ lastVal = *val; delay(10); return true; } else{ return false; } } //////////// SetupDisplay void displayInit(){ Serial.print(0x76, BYTE); // reset display Serial.print(0x7A, BYTE); // brightness register Serial.print(0xC8, BYTE); // set brightness to 200 (0 - 255) Serial.print(0x77, BYTE); // adress o decimal points register Serial.print(0x00, BYTE); // turn all decimal points OFF // make start-up signal for(int i=0; i<20; i++){ Serial.print(random(200), BYTE); delay(20); Serial.print(random(200), BYTE); delay(20); Serial.print(random(200), BYTE); delay(20); } Serial.print(0x76, BYTE); // reset display delay(20); } void digit_print(int myValue){ int digPos = numdigits(myValue); for(int i=4; i>digPos; --i){ if(!DEBUG)Serial.print(0x78, BYTE); // display a blank digit 120 delay(10); } if(!DEBUG)Serial.print(myValue, DEC); delay(10); } int numdigits(int i){ char str[20]; sprintf(str, "%d", abs(i)); return(strlen(str)); }