diff --git a/earthquake/earthquake.ino b/earthquake/earthquake.ino index 33ff706760c5363cdd686ed5215579ad44ddf525..e46d12dffff1d7523d6a1a0c8c600d83043dc777 100644 --- a/earthquake/earthquake.ino +++ b/earthquake/earthquake.ino @@ -10,6 +10,10 @@ --------------------*/ LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); +const int stateOn = 1; +const int stateOff = 2; +const int stateConnect = 3; + /*-------------------- * Pin const @@ -29,6 +33,10 @@ const int seismicPin = A0; /*-------------------- * Global variable * --------------------*/ +// +int stateLED = 1; + +int valGlobal = 0; // Toggle button int state = HIGH; // the current state of the output pin @@ -37,8 +45,6 @@ int previous = LOW; // the previous reading from the input pin long time = 0; // the last time the output pin was toggled long debounce = 200; // the debounce time, increase if the output flickers -// LCD -// LiquidCrystal lcd(0, 1, 8, 9, 10, 11); /// REGISTER SELECT PIN,ENABLE PIN,D4 PIN,D5 PIN, D6 PIN, D7 PIN // Seven Segment const byte digit[] = {64, 121, 36, 48, 25, 18, 2, 120, 0, 16}; @@ -91,13 +97,11 @@ void gyroOn(); void gyroOff(); void printLCD(int,int,String); void clearLCD(); +bool isState(int); void setup() { Serial.begin(115200); - - lcd.begin(16,2); - lcd.backlight(); // Toggle button toggleButtonInit(); @@ -127,13 +131,19 @@ void loop() { previous = reading; if(state == HIGH){ + if(!isState(stateOn)){ + clearLCD(); + printLCD(0,0,"Arduino ON"); + delay(1000); + } gyroOn(); - clearLCD(); - printLCD(0,0,"Arduino ON"); } else { gyroOff(); - clearLCD(); - printLCD(0,0,"Arduino OFF"); + if(!isState(stateOff)){ + clearLCD(); + printLCD(0,0,"Arduino OFF"); + delay(1000); + } } } @@ -159,7 +169,10 @@ void sevenSegmentInit(){ magnitude = 0; } -void lcdInit(){} +void lcdInit(){ + lcd.begin(16,2); + lcd.backlight(); +} void gyroInit(){ @@ -240,9 +253,12 @@ double magnitudeConvert() { } void displaySevenSegment(int val){ - digitalWrite(latchPin, LOW); - shiftOut(dataPin, clockPin, MSBFIRST, digit[val]); - digitalWrite(latchPin, HIGH); + if(val!=valGlobal){ + digitalWrite(latchPin, LOW); + shiftOut(dataPin, clockPin, MSBFIRST, digit[val]); + digitalWrite(latchPin, HIGH); + valGlobal=val; + } } void ledActive(){ @@ -317,3 +333,9 @@ void clearLCD(){ lcd.clear(); } +bool isState(int state){ + int temp = stateLED; + stateLED = state; + return temp==state; +} +