Created
July 4, 2016 14:09
-
-
Save samr28/318ad06573b1b5229041c8f0c6ed8807 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wh#include <Wire.h> | |
#include "Adafruit_Trellis.h" | |
#include <LiquidCrystal.h> | |
LiquidCrystal lcd(12,11,5,4,3,2); | |
const int LCD_WIDTH = 16, LCD_ROWS = 2; | |
const int DISPLAY_CONTRAST = 130; | |
const int DISPLAY_PWM = 6; | |
#define MOMENTARY 0 | |
#define LATCHING 1 | |
#define MODE MOMENTARY | |
Adafruit_Trellis matrix0 = Adafruit_Trellis(); | |
Adafruit_TrellisSet trellis = Adafruit_TrellisSet(&matrix0); | |
#define NUMTRELLIS 1 | |
#define numKeys (NUMTRELLIS * 16) | |
#define INTPIN A2 | |
void setup() { | |
Serial.begin(9600); | |
analogWrite(DISPLAY_PWM, DISPLAY_CONTRAST); | |
lcd.begin(LCD_WIDTH, LCD_ROWS); | |
lcd.clear(); | |
lcd.print(" Calculator"); | |
// INT pin requires a pullup | |
pinMode(INTPIN, INPUT); | |
digitalWrite(INTPIN, HIGH); | |
trellis.begin(0x70); | |
for (uint8_t i=0; i<numKeys; i++) { | |
trellis.setLED(i); | |
trellis.writeDisplay(); | |
delay(50); | |
} | |
for (uint8_t i=0; i<numKeys; i++) { | |
trellis.clrLED(i); | |
trellis.writeDisplay(); | |
delay(50); | |
} | |
eaqLED(); | |
} | |
void loop() { | |
writeLCDvalues(); | |
delay(30); // 30ms delay is required, dont remove me! | |
if (trellis.readSwitches()) { | |
for (uint8_t i=0; i<numKeys; i++) { | |
if (trellis.justPressed(i)) { | |
//Serial.print("v"); Serial.println(i); | |
trellis.setLED(i); | |
calculate(i); | |
} | |
if (trellis.justReleased(i)) { | |
//Serial.print("^"); Serial.println(i); | |
trellis.clrLED(i); | |
} | |
} | |
trellis.writeDisplay(); | |
} | |
/*if (MODE == MOMENTARY) { | |
// If a button was just pressed or released... | |
if (trellis.readSwitches()) { | |
// go through every button | |
for (uint8_t i=0; i<numKeys; i++) { | |
// if it was pressed, turn it on | |
if (trellis.justPressed(i)) { | |
Serial.print("v"); Serial.println(i); | |
trellis.setLED(i); | |
} | |
// if it was released, turn it off | |
if (trellis.justReleased(i)) { | |
Serial.print("^"); Serial.println(i); | |
trellis.clrLED(i); | |
} | |
} | |
// tell the trellis to set the LEDs we requested | |
trellis.writeDisplay(); | |
} | |
} | |
if (MODE == LATCHING) { | |
// If a button was just pressed or released... | |
if (trellis.readSwitches()) { | |
// go through every button | |
for (uint8_t i=0; i<numKeys; i++) { | |
// if it was pressed... | |
if (trellis.justPressed(i)) { | |
Serial.print("v"); Serial.println(i); | |
// Alternate the LED | |
if (trellis.isLED(i)) | |
trellis.clrLED(i); | |
else | |
trellis.setLED(i); | |
} | |
} | |
// tell the trellis to set the LEDs we requested | |
trellis.writeDisplay(); | |
} | |
}*/ | |
} | |
const int eaqDELAY = 100; | |
void eaqLED() { | |
trellis.setLED(15); | |
delay(eaqDELAY); | |
trellis.writeDisplay(); | |
trellis.clrLED(15); | |
trellis.setLED(14); | |
trellis.setLED(11); | |
delay(eaqDELAY); | |
trellis.writeDisplay(); | |
trellis.clrLED(11); | |
trellis.clrLED(14); | |
trellis.clrLED(15); | |
trellis.setLED(7); | |
trellis.setLED(10); | |
trellis.setLED(13); | |
delay(eaqDELAY); | |
trellis.writeDisplay(); | |
trellis.clrLED(7); | |
trellis.clrLED(10); | |
trellis.clrLED(13); | |
trellis.setLED(3); | |
trellis.setLED(6); | |
trellis.setLED(9); | |
trellis.setLED(12); | |
delay(eaqDELAY); | |
trellis.writeDisplay(); | |
trellis.clrLED(3); | |
trellis.clrLED(6); | |
trellis.clrLED(9); | |
trellis.clrLED(12); | |
trellis.setLED(2); | |
trellis.setLED(5); | |
trellis.setLED(8); | |
delay(eaqDELAY); | |
trellis.writeDisplay(); | |
trellis.clrLED(2); | |
trellis.clrLED(5); | |
trellis.clrLED(8); | |
trellis.setLED(1); | |
trellis.setLED(4); | |
delay(eaqDELAY); | |
trellis.writeDisplay(); | |
trellis.clrLED(1); | |
trellis.clrLED(4); | |
trellis.setLED(0); | |
delay(eaqDELAY); | |
trellis.writeDisplay(); | |
trellis.clrLED(0); | |
delay(eaqDELAY); | |
trellis.writeDisplay(); | |
} | |
const int clrDELAY = 100; | |
void clrLED() { | |
for (int i = 0; i < numKeys; i++) { | |
trellis.setLED(i); | |
} | |
trellis.writeDisplay(); | |
delay(clrDELAY); | |
for (int i = 0; i < numKeys; i++) { | |
trellis.clrLED(i); | |
} | |
trellis.writeDisplay(); | |
} | |
void divLED(bool on) { | |
if (on) { | |
trellis.setLED(3); | |
trellis.setLED(6); | |
trellis.setLED(9); | |
trellis.setLED(12); | |
} | |
if (!on) { | |
trellis.clrLED(3); | |
trellis.clrLED(6); | |
trellis.clrLED(9); | |
trellis.clrLED(12); | |
} | |
} | |
void mltplyLED(bool on) { | |
if (on) { | |
trellis.setLED(5); | |
trellis.setLED(6); | |
trellis.setLED(9); | |
trellis.setLED(10); | |
} | |
if (!on) { | |
trellis.clrLED(5); | |
trellis.clrLED(6); | |
trellis.clrLED(9); | |
trellis.clrLED(10); | |
} | |
} | |
void addLED(bool on) { | |
if (on) { | |
trellis.setLED(1); | |
trellis.setLED(4); | |
trellis.setLED(5); | |
trellis.setLED(6); | |
trellis.setLED(9); | |
} | |
if (!on) { | |
trellis.clrLED(1); | |
trellis.clrLED(4); | |
trellis.clrLED(5); | |
trellis.clrLED(6); | |
trellis.clrLED(9); | |
} | |
} | |
void subLED(bool on) { | |
if (on) { | |
trellis.setLED(4); | |
trellis.setLED(5); | |
trellis.setLED(6); | |
} | |
if (!on) { | |
trellis.clrLED(4); | |
trellis.clrLED(5); | |
trellis.clrLED(6); | |
} | |
} | |
int opr; | |
bool wasAns = false; | |
float ans; | |
String num1, num2, oprStr; | |
bool isNum2 = false; | |
const int DIV = 14, MLTPLY = 13, SUB = 12, ADD = 11, EAQ = 10, CLEAR = 15; | |
const int KEYPAD[16] = {0, DIV, MLTPLY, CLEAR, | |
7, 8, 9, SUB, | |
4, 5, 6, ADD, | |
1, 2, 3, EAQ}; | |
void calculate(int button) { | |
if (KEYPAD[button] >= 10) { | |
if (KEYPAD[button] == EAQ) { | |
eaqLED(); | |
} | |
if (KEYPAD[button] == CLEAR) { | |
clrLED(); | |
resetValues(); | |
} | |
else if (isNum2 == true) { | |
if (opr == DIV) { | |
divLED(false); | |
ans = (float)num1.toInt()/(float)num2.toInt(); | |
Serial.print("ANS: "); | |
Serial.println(ans); | |
} | |
if (opr == MLTPLY) { | |
mltplyLED(false); | |
ans = (float)num1.toInt()*(float)num2.toInt(); | |
Serial.print("ANS: "); | |
Serial.println(ans); | |
} | |
if (opr == ADD) { | |
addLED(false); | |
ans = (float)num1.toInt()+(float)num2.toInt(); | |
Serial.print("ANS: "); | |
Serial.println(ans); | |
} | |
if (opr == SUB) { | |
subLED(false); | |
ans = (float)num1.toInt()-(float)num2.toInt(); | |
Serial.print("ANS: "); | |
Serial.println(ans); | |
} | |
reset(); | |
} | |
else { | |
opr = KEYPAD[button]; | |
if (KEYPAD[button] == DIV) { | |
divLED(true); | |
oprStr = "/"; | |
} | |
else if (KEYPAD[button] == MLTPLY) { | |
mltplyLED(true); | |
oprStr = "*"; | |
} | |
else if (KEYPAD[button] == ADD) { | |
addLED(true); | |
oprStr = "+"; | |
} | |
else if (KEYPAD[button] == SUB) { | |
subLED(true); | |
oprStr = "-"; | |
} | |
isNum2 = true; | |
} | |
} | |
else if (isNum2 == false) { | |
if (wasAns) { | |
num1 = ""; | |
wasAns = false; | |
} | |
num1 = num1 + KEYPAD[button]; | |
} | |
else { | |
num2 = num2 + KEYPAD[button]; | |
} | |
Serial.println("--------------------------------------"); | |
Serial.println(isNum2); | |
Serial.println(num1); | |
Serial.println(oprStr); | |
Serial.println(num2); | |
Serial.println("--------------------------------------"); | |
} | |
void resetValues() { | |
Serial.println("CLEARING VALUES"); | |
num1 = ""; | |
num2 = ""; | |
ans = 0; | |
opr = 0; | |
oprStr = ""; | |
isNum2 = false; | |
} | |
void reset() { | |
num1 = ans; | |
num2 = ""; | |
ans = 0; | |
opr = 0; | |
oprStr = ""; | |
wasAns = true; | |
isNum2 = false; | |
} | |
void writeLCDvalues() { | |
lcd.clear(); | |
if (num1 == "" && num2 == "") { | |
displayLCD(1, ans); | |
} | |
else { | |
displayLCD(1, num1); | |
displayLCD(2, num2); | |
displayLCD(3, oprStr); | |
} | |
} | |
void displayLCD(int line, String value) { | |
//lcd.clear(); | |
if (line == 1) { | |
lcd.setCursor(0,0); | |
} | |
else if (line == 3) { | |
lcd.setCursor(15,0); | |
} | |
else { | |
lcd.setCursor(0,1); | |
} | |
lcd.print(value); | |
} | |
void displayLCD(int line, float value) { | |
//lcd.clear(); | |
if (line == 1) { | |
lcd.setCursor(0,0); | |
} | |
else { | |
lcd.setCursor(0,1); | |
} | |
lcd.print(value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment