pomeroyb / PortalGun

Code to control a Portal Gun (From Rick and Morty)
32 stars 19 forks source link

7 segment display #10

Open faebulous17 opened 6 years ago

faebulous17 commented 6 years ago

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. ☺️

faebulous17 commented 6 years ago

` // Code to control a Rick and Morty Portal Gun // Written by Brandon Pomeroy, 2015

/* **** Required Libraries *****

/* ** Required Hardware *****

include

include "Adafruit_LEDBackpack.h"

include "Adafruit_GFX.h"

include

include

include <avr/sleep.h>

include <avr/power.h>

// 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;

define encoderPinA A1

define encoderPinB A0

define encoderButtonPin A2

// Steps per notch can be 1, 4, or 8. If your encoder is counting // to fast or too slow, change this!

define stepsPerNotch 4

// Comment this line to make the encoder increment in the opposite direction

define reverseEncoderWheel

// FX Board output delay (ms) const int msDelay = 500;

// Set up the Green LEDs

define topBulbPin 9

define frontRightPin 3

define frontCenterPin 5

define frontLeftPin 6

define maximumBright 255

define mediumBright 127

int topBulbBrightness = 255;

// set Display letter values

define capitalLetterC B00111001

define capitalLetterA B01110111

define capitalLetterE B01111001

define capitalLetterF B01110001

define capitalLetterJ B00011110

define capitalLetterP B01100011

define capitalLetterU B00111110

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

define NAV0_PIN A2

//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(){

ifdef reverseEncoderWheel

value -= encoder->getValue();

endif

ifndef reverseEncoderWheel

value += encoder->getValue();

endif

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(); }

/* ============== Sleep/Wake Methods ==================

*/

// 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

}

ISR (PCINT2_vect) // handle pin change interrupt for D0 to D7 here // NAV1, NAV2 { // Check it was NAV1 or NAV2 and nothing else }

/* ============== Testing Methods ==================

*/

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); } }`