//Desktop Stargate //by Cameron Coward 2019 //https://www.cameroncoward.com //code snippets from: https://www.instructables.com/id/Multiplexing-with-Arduino-and-the-74HC595/ and http://highlowtech.org/?p=1653 /* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ // pin connections for shift register- the #define tag will replace all instances of "latchPin" in your code with A1 (and so on) #define latchPin 2 #define clockPin 1 #define dataPin 0 int chevron = 0; // how many chevrons are lit int calibration = 0; // for capacitive touch int previous; // for capacitive touch long previousMillis = 0; // for counting delay between touches bool canPress = false; // is capacitive touch ready for another tap? //storage variable byte dataToSend; // the shift register byte for controlling LEDs void setup() { //set pins as output pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); delay(100); for (int i = 0; i < 8; i++) { // initial capacitive touch calibration on startup calibration += chargeTime(4); delay(20); } calibration = (calibration + 4) / 8; } void loop() { unsigned long currentMillis = millis(); // see how many milliseconds have passed if(currentMillis - previousMillis > 500) { int n = chargeTime(4); if (previous <= calibration && n > calibration) { // check to see if pad has been touched chevron = chevron +1; if(chevron > 7) chevron = 0; previousMillis = currentMillis; } } switch (chevron) { case 0: //All chevrons off dataToSend = B00000110; // all LEDs off writeToShift(); break; case 1: //Chevron one engaged! dataToSend = B00010100; // turn on LED 6 (chevron 1) writeToShift(); break; case 2: //Chevron two engaged! dataToSend = B00010100; // turn on LED 6 (chevron 1) writeToShift(); dataToSend = B00100100; // turn on LED 7 (chevron 2) writeToShift(); break; case 3: //Chevron three engaged! dataToSend = B00010100; // turn on LED 6 (chevron 1) writeToShift(); dataToSend = B00100100; // turn on LED 7 (chevron 2) writeToShift(); dataToSend = B01000100; // turn on LED 8 (chevron 3) writeToShift(); break; case 4: //Chevron four engaged! dataToSend = B00010100; // turn on LED 6 (chevron 1) writeToShift(); dataToSend = B00100100; // turn on LED 7 (chevron 2) writeToShift(); dataToSend = B01000100; // turn on LED 8 (chevron 3) writeToShift(); dataToSend = B00001010; // turn on LED 1 (chevron 4) writeToShift(); break; case 5: //Chevron five engaged! dataToSend = B00010100; // turn on LED 6 (chevron 1) writeToShift(); dataToSend = B00100100; // turn on LED 7 (chevron 2) writeToShift(); dataToSend = B01000100; // turn on LED 8 (chevron 3) writeToShift(); dataToSend = B00001010; // turn on LED 1 (chevron 4) writeToShift(); dataToSend = B00010010; // turn on LED 2 (chevron 5) writeToShift(); break; case 6: //Chevron six engaged! dataToSend = B00010100; // turn on LED 6 (chevron 1) writeToShift(); dataToSend = B00100100; // turn on LED 7 (chevron 2) writeToShift(); dataToSend = B01000100; // turn on LED 8 (chevron 3) writeToShift(); dataToSend = B00001010; // turn on LED 1 (chevron 4) writeToShift(); dataToSend = B00010010; // turn on LED 2 (chevron 5) writeToShift(); dataToSend = B00100010; // turn on LED 3 (chevron 6) writeToShift(); break; case 7: //Chevron seven locked! dataToSend = B00010100; // turn on LED 6 (chevron 1) writeToShift(); dataToSend = B00100100; // turn on LED 7 (chevron 2) writeToShift(); dataToSend = B01000100; // turn on LED 8 (chevron 3) writeToShift(); dataToSend = B00001010; // turn on LED 1 (chevron 4) writeToShift(); dataToSend = B00010010; // turn on LED 2 (chevron 5) writeToShift(); dataToSend = B00100010; // turn on LED 3 (chevron 6) writeToShift(); dataToSend = B01000010; // turn on LED 4 (chevron 7) writeToShift(); dataToSend = B00001100; // turn on LED 5 (wormhole) writeToShift(); break; default: dataToSend = B00000110; writeToShift(); break; } } void writeToShift() { // set the bits for the shift register digitalWrite(latchPin, LOW); // shift out the bits of dataToSend to the 74HC595 shiftOut(dataPin, clockPin, LSBFIRST, dataToSend); //set latch pin high- this sends data to outputs so the LEDs will light up digitalWrite(latchPin, HIGH); } byte chargeTime(byte pin) // checks the touch pad { byte mask = (1 << pin); byte i; DDRB &= ~mask; // input PORTB |= mask; // pull-up on for (i = 0; i < 16; i++) { if (PINB & mask) break; } PORTB &= ~mask; // pull-up off DDRB |= mask; // discharge return i; }