Open faebulous17 opened 6 years ago
` // Code to control a Rick and Morty Portal Gun // Written by Brandon Pomeroy, 2015
/* **** Required Libraries *****
/* ** Required Hardware *****
// Set up our LED display Adafruit_7segment alpha4 = Adafruit_7segment(); int displayBuffer[4]; uint8_t dimensionLetter='C';
// Set up the click encoder ClickEncoder *encoder; int16_t last; int value;
// Steps per notch can be 1, 4, or 8. If your encoder is counting // to fast or too slow, change this!
// Comment this line to make the encoder increment in the opposite direction
// FX Board output delay (ms) const int msDelay = 500;
// Set up the Green LEDs
int topBulbBrightness = 255;
// set Display letter values
double currentLetter = capitalLetterC;
// Set up what we need to sleep/wake the Trinket // Define the pins you'll use for interrupts - CHANGE THESE to match the input pins // you are using in your project
//Let us know if our Trinket woke up from sleep volatile bool justWokeUp;
void timerIsr() { encoder->service(); }
void setup() { enablePinInterupt(NAV0_PIN);
//Set up pin modes pinMode(topBulbPin, OUTPUT); pinMode(frontRightPin, OUTPUT); pinMode(frontLeftPin, OUTPUT); pinMode(frontCenterPin, OUTPUT);
digitalWrite(frontRightPin, HIGH); digitalWrite(frontLeftPin, HIGH); digitalWrite(frontCenterPin, HIGH); digitalWrite(topBulbPin, HIGH);
encoderSetup(); alpha4.begin(0x70); // pass in the address for the LED display
justWokeUp = false;
//uncomment this to make the display run through a test at startup //displayTest(); }
void loop() { if (justWokeUp) { digitalWrite(frontRightPin, HIGH); digitalWrite(frontLeftPin, HIGH); digitalWrite(frontCenterPin, HIGH); digitalWrite(topBulbPin, HIGH); justWokeUp = false; }
ClickEncoder::Button b = encoder->getButton(); switch (b) { case ClickEncoder::Held: // Holding the button will put your trinket to sleep. // The trinket will wake on the next button press alpha4.clear(); alpha4.writeDigitRaw(0, capitalLetterC); alpha4.writeDigitNum(1, 1); alpha4.writeDigitNum(2, 2); alpha4.writeDigitNum(3, 3); digitalWrite(frontRightPin, LOW); digitalWrite(frontLeftPin, LOW); digitalWrite(frontCenterPin, LOW); digitalWrite(topBulbPin, LOW); alpha4.writeDisplay(); delay(5000); alpha4.clear(); alpha4.writeDisplay(); delay(5000); justWokeUp = true; goToSleep(); break; case ClickEncoder::Clicked: // When the encoder wheel is single clicked
break;
case ClickEncoder::DoubleClicked:
//If you double click the button, it sets the dimension to C137
dimensionLetter = 'C';
value = 137;
break;
case ClickEncoder::Open:
// The dimension will increment from 0-999, then roll over to the next
// letter. (A999 -> B000)
updateDimension();
break;
} }
void encoderSetup(){ // set up encoder encoder = new ClickEncoder(encoderPinA, encoderPinB, encoderButtonPin, stepsPerNotch); encoder->setAccelerationEnabled(true);
Timer1.initialize(1000);
Timer1.attachInterrupt(timerIsr);
last = -1;
value = 137;
}
void updateDimension(){
value -= encoder->getValue();
value += encoder->getValue();
if (value != last) { if (value > 999){ value = 0;
switch (dimensionLetter) { case 'C': dimensionLetter = 'A'; currentLetter = capitalLetterA; break;
case 'A':
dimensionLetter = 'E';
currentLetter = capitalLetterE;
break;
case 'E':
dimensionLetter = 'F';
currentLetter = capitalLetterF;
break;
case 'F':
dimensionLetter = 'J';
currentLetter = capitalLetterJ;
break;
case 'J':
dimensionLetter = 'P';
currentLetter = capitalLetterP;
break;
case 'P':
dimensionLetter = 'U';
currentLetter = capitalLetterU;
break;
case 'U':
dimensionLetter = 'C';
currentLetter = capitalLetterC;
break;
}
} else if ( value < 0 ) {
value = 999;
switch (dimensionLetter) { case 'C': dimensionLetter = 'U'; currentLetter = capitalLetterU; break;
case 'A':
dimensionLetter = 'C';
currentLetter = capitalLetterC;
break;
case 'E':
dimensionLetter = 'A';
currentLetter = capitalLetterA;
break;
case 'F':
dimensionLetter = 'E';
currentLetter = capitalLetterE;
break;
case 'J':
dimensionLetter = 'F';
currentLetter = capitalLetterF;
break;
case 'P':
dimensionLetter = 'J';
currentLetter = capitalLetterJ;
break;
case 'U':
dimensionLetter = 'P';
currentLetter = capitalLetterP;
break;
}
}
last = value;
}
// sprintf(displayBuffer, "%03i", value);
//if (value < 100) {
// displayBuffer[0] = 0;
//} else {
displayBuffer[0] = value / 100 % 10;
//}
//if (value < 100) {
// displayBuffer[1] = 0;
//} else {
displayBuffer[1] = value / 10 % 10;
//}
displayBuffer[2] = value%10;
alpha4.clear(); alpha4.writeDigitRaw(0, currentLetter); alpha4.writeDigitNum(1, displayBuffer[0], false); alpha4.writeDigitNum(3, displayBuffer[1], false); alpha4.writeDigitNum(4, displayBuffer[2], false); alpha4.writeDisplay(); }
*/
// Most of this code comes from seanahrens on the adafruit forums // http://forums.adafruit.com/viewtopic.php?f=25&t=59392#p329418
void enablePinInterupt(byte pin) { *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group }
void goToSleep()
{
// The ATmega328 has five different sleep states.
// See the ATmega 328 datasheet for more information.
// SLEEP_MODE_IDLE -the least power savings
// SLEEP_MODE_ADC
// SLEEP_MODE_PWR_SAVE
// SLEEP_MODE_STANDBY
// SLEEP_MODE_PWR_DOWN -the most power savings
// I am using the deepest sleep mode from which a
// watchdog timer interrupt can wake the ATMega328
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Set sleep mode. sleep_enable(); // Enable sleep mode. sleep_mode(); // Enter sleep mode. // After waking the code continues // to execute from this point.
sleep_disable(); // Disable sleep mode after waking.
}
ISR (PCINT0_vect) // handle pin change interrupt for D8 to D13 here
{
// if I wired up D8-D13 then I'd need some code here
}
ISR (PCINT1_vect) // handle pin change interrupt for A0 to A5 here // NAV0 { / This will bring us back from sleep. /
/* We detach the interrupt to stop it from
is low. */
detachInterrupt(0);
}
ISR (PCINT2_vect) // handle pin change interrupt for D0 to D7 here // NAV1, NAV2 { // Check it was NAV1 or NAV2 and nothing else }
*/
void displayTest() {
alpha4.writeDigitRaw(3, 0x0); alpha4.writeDigitRaw(0, 0xFFFF); alpha4.writeDisplay(); delay(200); alpha4.writeDigitRaw(0, 0x0); alpha4.writeDigitRaw(1, 0xFFFF); alpha4.writeDisplay(); delay(200); alpha4.writeDigitRaw(1, 0x0); alpha4.writeDigitRaw(2, 0xFFFF); alpha4.writeDisplay(); delay(200); alpha4.writeDigitRaw(2, 0x0); alpha4.writeDigitRaw(3, 0xFFFF); alpha4.writeDisplay(); delay(200);
alpha4.clear(); alpha4.writeDisplay();
// display every character, for (uint8_t i='!'; i<='z'; i++) { alpha4.writeDigitRaw(0, i); alpha4.writeDigitRaw(1, i+1); alpha4.writeDigitRaw(3, i+2); alpha4.writeDigitRaw(4, i+3); alpha4.writeDisplay(); delay(300); } }`
Hi yall
I just started building this, thanks first of all for all the work you've put in. I accidentally ordered a 7 segment display instead of a 14 seg, the 14 was sold out anyway. I've rewritten the code so it works with a 7 segment as well (only with limited letters tho). I've never coded C++ before and have only tested the display with the rotary, but not the button clicks. I'll upload the code soon after so you can check if what I did is useful. ☺️