//nerf upgrade - build 110812t2.0 - nimrod316@gmx.de //#include "pins_arduino.h" // Arduino pre-1.0 needs this #define PowerIREm A0 //IR LED power pin #define DataIRRec A1 //IR data pin #define PowerIRRec A3 //IR Receiver power pin #define IRthreshold 90 //value to measure if visual connection between IR Emitter & Receiver is interrupted (~90) #define NoFingerOnTrigger 0 //sensivity; 0 = no finger on trigger; 1 and greater = finger on trigger #define DataTrigger A2 //trigger data pin #define basetimer 25 //25ms, general timer #define scanspeed 25 //25ms, every time check of visual connection between IR Emitter & Receiver byte pinArray[] = {A9, A8, A7, A6, A5, A4}; //LED array for ammunition byte count = 0; //general counter byte shots = 6; //maximum shots on barrel word sensorValue = 0; byte tmp_trigger = 0; // thx to Paul from pjrc.com uint8_t readCapacitivePin(int pinToMeasure) { // Variables used to translate from Arduino to AVR pin naming volatile uint8_t* port; volatile uint8_t* ddr; volatile uint8_t* pin; // Here we translate the input pin number from // Arduino pin number to the AVR PORT, PIN, DDR, // and which bit of those registers we care about. byte bitmask; port = portOutputRegister(digitalPinToPort(pinToMeasure)); ddr = portModeRegister(digitalPinToPort(pinToMeasure)); bitmask = digitalPinToBitMask(pinToMeasure); pin = portInputRegister(digitalPinToPort(pinToMeasure)); // Discharge the pin first by setting it low and output *port &= ~(bitmask); *ddr |= bitmask; delay(1); // Make the pin an input with the internal pull-up on *ddr &= ~(bitmask); *port |= bitmask; // Now see how long the pin to get pulled up int cycles = 17; for(int i = 0; i < 16; i++){ if (*pin & bitmask){ cycles = i; break; } } // Discharge the pin again by setting it low and output // It's important to leave the pins low if you want to // be able to touch more than 1 sensor at a time - if // the sensor is left pulled high, when you touch // two sensors, your body will transfer the charge between // sensors. *port &= ~(bitmask); *ddr |= bitmask; return cycles; } void setup(){ //IR LED reset pinMode(PowerIREm, OUTPUT); digitalWrite(PowerIREm, HIGH); //IR Receiver reset pinMode(PowerIRRec,OUTPUT); digitalWrite(PowerIRRec, HIGH); //reset ammo LEDs for (count = 0; count < shots; count++) { pinMode(pinArray[count], OUTPUT); digitalWrite(pinArray[count], LOW); } //begin knight rider effect for (count = 0; count < (shots); count++) { digitalWrite(pinArray[count], HIGH); delay(basetimer); digitalWrite(pinArray[count + 1], HIGH); delay(basetimer); digitalWrite(pinArray[count], LOW); delay(basetimer * 2); } for (count = (shots); count > 0; count--) { digitalWrite(pinArray[count], HIGH); delay(basetimer); digitalWrite(pinArray[count - 1], HIGH); delay(basetimer); digitalWrite(pinArray[count], LOW); delay(basetimer * 2); } //end knight rider effect //set all ammo LEDs HIGH, barrel is presumably fully loaded for (count = 0; count < (shots); count++) { digitalWrite(pinArray[count], HIGH); delay(basetimer); digitalWrite(pinArray[count + 1], HIGH); delay(basetimer * 4); //using the basetimer here, gives a cool recharge effect } } void loop() { //as long as we are not out of ammo while (shots > 0) { //check if finger is on trigger tmp_trigger = readCapacitivePin(DataTrigger); switch (tmp_trigger) { case NoFingerOnTrigger: //LOW ammo LEDs for (count = 0; count < shots; count++) { digitalWrite(pinArray[count], LOW); } //LOW IR LED digitalWrite(PowerIREm, LOW); //LOW IR Receiver Pin digitalWrite(PowerIRRec, LOW); break; default: //HIGH ammo LEDs for (count = 0; count < shots; count++) { digitalWrite(pinArray[count], HIGH); } //HIGH IR LED digitalWrite(PowerIREm, HIGH); //HIGH IR Receiver Pin digitalWrite(PowerIRRec, HIGH); //visual connection interrupted? sensorValue = analogRead(DataIRRec); if (sensorValue >= IRthreshold) { shots -= 1; //LOW ammo LED digitalWrite(pinArray[shots], LOW); } delay(scanspeed); } } delay(scanspeed); }