Skip to content

Instantly share code, notes, and snippets.

@bilinin
Created November 30, 2015 11:26
Show Gist options
  • Save bilinin/e79f70b38bcd46d4b4b4 to your computer and use it in GitHub Desktop.
Save bilinin/e79f70b38bcd46d4b4b4 to your computer and use it in GitHub Desktop.
/*
* Displays text sent over the serial port (e.g. from the Serial Monitor) on
* an attached LCD.
* YWROBOT
*Compatible with the Arduino IDE 1.0
*Library version:1.1
*/
#include <Wire.h>
#include <math.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
long first = 0;
long second = 0;
double total = 0;
char customKey;
int menuFlag=0;
int menuPosition=0;
int needToClear=0;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'+','3','2','1'},
{'-','6','5','4'},
{'*','9','8','7'},
{'/','=','0','C'}
};
byte rowPins[ROWS] = {1,2,3,4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8,7,6,5}; //connect to the column pinouts of the keypad
char string[500]="Nedavno prochital ochen' interesnuju stat'ju pro obrabotku 50 gigabit/s na servere i vspomnil, chto u menja v chernovikah lezhit stat'ja pro to, kak my god nazad razrabatyvali sistemu monitoringa video-potokov s obshhim ob#jomom trafika do 100 Gbit/s.";
char showString[16];
char showString2[16];
int currentStringPos;
Keypad keypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS );
byte ledPin = 13;
boolean blink = false;
boolean ledPin_state;
///////////////////////////////////////////////////////////////
int miFlag=0;
int charBuff=0;
int buff[200];
int buffTail;
int buffCurrent;
///////////////////////////////Буффер для ввода нового числа///////////////////////////
long inputBuff;
///////////////////////////////////////////////////////////////////////////////////////
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
void makePrintStr(int currentStringPos){
for(int i=0;i<16;i++){
showString[i]=string[currentStringPos*16+i];
showString2[i]=string[(currentStringPos+1)*16+i];
}
}
void setup()
{
lcd.init(); // initialize the lcd
lcd.backlight();
Serial.begin(9600);
lcd.print('>');
pinMode(ledPin, OUTPUT); // Sets the digital pin as output.
digitalWrite(ledPin, HIGH); // Turn the LED on.
ledPin_state = digitalRead(ledPin); // Store initial LED state. HIGH when LED is on.
keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
}
void showPosName(){
lcd.clear();
if(menuPosition==1){
lcd.print("Calculator");
}
if(menuPosition==2){
lcd.print("Reader");
}
if(menuPosition==3){
lcd.print("SIN");
}
if(menuPosition==4){
lcd.print("COS");
}
needToClear=1;
}
long inputNumber()
{
while( 1 )
{
customKey = keypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
inputBuff = inputBuff * 10 + (customKey - '0');
lcd.clear();
lcd.print(inputBuff);
lcd.setCursor(0,1);
if(menuPosition==3){
lcd.print("SIN=");
lcd.print(sin(inputBuff*3.14/180));
}
if(menuPosition==4){
lcd.print("COS=");
lcd.print(cos(inputBuff*3.14/180));
}
}
if((customKey == '=')||(customKey == 'C') ) break; //return second;
}
return inputBuff;
}
long SecondNumber()
{
while( 1 )
{
customKey = keypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
second = second * 10 + (customKey - '0');
lcd.print(customKey);
}
if(customKey == '=') break; //return second;
}
return second;
}
void simpleCalc(char customKey){
switch(customKey)
{
case '0' ... '9': // This keeps collecting the first value until a operator is pressed "+-*/"
lcd.setCursor(0,0);
first = first * 10 + (customKey - '0');
lcd.print(first);
break;
case '+':
first = (total != 0 ? total : first);
lcd.print("+");
second = SecondNumber(); // get the collected the second number
total = first + second;
lcd.setCursor(0,1);
lcd.print(total);
total = 0;
first = 0, second = 0; // reset values back to zero for next use
needToClear=1;
break;
case '-':
first = (total != 0 ? total : first);
lcd.print("-");
second = SecondNumber();
total = first - second;
lcd.setCursor(0,1);
lcd.print(total);
needToClear=1;
total = 0;
first = 0, second = 0;
break;
case '*':
first = (total != 0 ? total : first);
lcd.print("*");
second = SecondNumber();
total = first * second;
lcd.setCursor(0,1);
lcd.print(total);
needToClear=1;
total = 0;
first = 0, second = 0;
break;
case '/':
first = (total != 0 ? total : first);
lcd.print("/");
second = SecondNumber();
lcd.setCursor(0,1);
second == 0 ? lcd.print("Invalid") : total = (float)first / (float)second;
lcd.print(total);
first = 0, second = 0;
total = 0;
needToClear=1;
break;
case 'C':
total = 0;
first = 0, second = 0;
lcd.clear();
break;
}
}
void reader(){
makePrintStr(currentStringPos);
lcd.clear();
lcd.print(showString);
lcd.setCursor(0,1);
lcd.print(showString2);
}
long res;
void loop(){
char key = keypad.getKey();
if (key) {
if(needToClear){
lcd.clear();
needToClear=0;
}
if(menuFlag==0){
if(menuPosition==1){
simpleCalc(key);
}
else if((menuPosition==3)||(menuPosition==4)){
res=inputNumber();
inputBuff=0;
lcd.clear();
}
}
Serial.print(key);
}
if(key=='/') {
if(menuFlag==1){
menuPosition++;
lcd.clear();
lcd.print("Position ");
lcd.print(menuPosition);
showPosName();
}
else if(menuFlag==0){
if(menuPosition==2){
currentStringPos+=2;
reader();
}
}
}
else if(key=='*') {
if(menuFlag==1){
if(menuPosition>0) menuPosition--;
lcd.clear();
lcd.print("Position ");
lcd.print(menuPosition);
showPosName();
}
else if(menuFlag==0){
if(menuPosition==2){
if(currentStringPos>1) currentStringPos-=2;
reader();
}
}
}
if (blink){
digitalWrite(ledPin,!digitalRead(ledPin)); // Change the ledPin from Hi2Lo or Lo2Hi.
delay(100);
}
}
// Taking care of some special events.
void keypadEvent(KeypadEvent key){
switch (keypad.getState()){
case PRESSED:
break;
case RELEASED:
if (key == 'C') {
digitalWrite(ledPin,ledPin_state); // Restore LED state from before it started blinking.
blink = false;
}
break;
case HOLD:
if (key=='C') {
lcd.clear();
if(menuFlag==0){
lcd.print("menu mode on");
needToClear=1;
menuFlag=1;
}
else if(menuFlag==1){
lcd.print("menu mode off");
needToClear=1;
menuFlag=0;
}
}
if (key == '=') {
digitalWrite(ledPin,!digitalRead(ledPin));
ledPin_state = digitalRead(ledPin); // Remember LED state, lit or unlit.
lcd.clear();
menuPosition=1;
menuFlag=0;
lcd.print("0");
}
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment