Skip to content

Instantly share code, notes, and snippets.

@samr28
Last active June 29, 2016 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samr28/77f58d20b81d54c106eafe26a843dabe to your computer and use it in GitHub Desktop.
Save samr28/77f58d20b81d54c106eafe26a843dabe to your computer and use it in GitHub Desktop.
#include <LiquidCrystal.h>
#include <Servo.h>
LiquidCrystal lcd(12,11,5,4,3,2); //Set LCD Pins
Servo servo1;
bool isOn = false; //Default state is off
const int BUTTON1_PIN = 7, BUTTON2_PIN = 8, BUTTON3_PIN = 6; //Button pins
const int LED_PIN = 13; //LED Pin
const int FLEX_PIN = 1; //Flex Sensor Pin
const int SERVO_PIN = 10; //Sevo pin
const int PHOTO_PIN = 0; //Photo Sensor Pin
const int DELAY_TIME = 1000, LDELAY_TIME = 4000; //DELAY_TIME is the time required to hold down the button to change states (default 1000). LDELAY_TIME is time to show screen 2 before going back to main screen.
bool button1 = false; //Button 1 value
bool button2 = false; //Button 2 value
bool button3 = false; //Button 3 value
int flexValue = 0; //Value of the flex sesor
int servoPos = 0; //Poaition of the servo
int photoValue = 0; //Photo sensor value
void setup()
{
//Serial.begin(9600); //Begins the serial monitor to log
pinMode(BUTTON1_PIN, INPUT); //Sets pinMode for buttons 1, 2, and 3 to input
pinMode(BUTTON2_PIN, INPUT);
pinMode(BUTTON3_PIN, INPUT);
pinMode(LED_PIN, OUTPUT); //Sets the LED pin to output
servo1.attach(SERVO_PIN); //Sets the servo pin
lcd.begin(16, 2); //Set LCD size to 16 char by 2 char
lcd.clear(); //Make sure LCD is clear
lcd.print(" Power: Off");
}
void loop()
{
button1 = digitalRead(BUTTON1_PIN); //Sets the value of button1 to the digital value of button1
button2 = digitalRead(BUTTON2_PIN);
button3 = digitalRead(BUTTON3_PIN);
flexValue = analogRead(FLEX_PIN); //Sets flexvalue to the value given by the sensor
servoPos = map(flexValue, 670, 920, 0, 90); //Maps flexvalue to servoPos and constrains it from 0-90
servoPos = constrain(servoPos, 0, 90);
photoValue = analogRead(PHOTO_PIN); //Sets photo value
lcd.setCursor(0,1); //Moves cursor down to second line
if (button3 == true) { //If button3 is pressed, open up screen2
screen2();
}
if (button1 == true && isOn == false) { //If device is off and the on button is pressed, wait for DELAY_TIME and if the button is still pressed, turn the device on
delay(DELAY_TIME);
button1 = digitalRead(BUTTON1_PIN);
if (button1 == true) {
lcd.setCursor(0,0);
lcd.clear();
lcd.print(" Power: On");
digitalWrite(LED_PIN, HIGH);
servo1.write(90);
isOn = true;
}
}
else if (button2 == true && isOn == true) { //If device is on and the off button is pressed, wait for DELAY_TIME and if the button is still pressed, turn the device off
delay(DELAY_TIME);
button2 = digitalRead(BUTTON2_PIN);
if (button2 == true) {
lcd.setCursor(0,0);
lcd.clear();
lcd.print(" Power: Off");
digitalWrite(LED_PIN, LOW);
isOn = false;
}
}
if (isOn) {
lcd.setCursor(0,1);
if (photoValue < 600) {
servo1.write(servoPos);
lcd.write("Lts:Off");
}
else {
servo1.write(90+(servoPos*-1));
lcd.write("Lts: On");
}
lcd.print("|Srvo: ");
lcd.print(servoPos);
//Serial.println(servoPos);
}
}
void screen2() {
button3 = false;
/*lcd.clear();
lcd.print(" B1:");
lcd.print(button1);
lcd.print("|B2:");
lcd.print(button2);
lcd.print("|B3:");
lcd.print(button3);
lcd.setCursor(0,1);
lcd.print("Lt:");
lcd.print(photoValue);
lcd.print("Flx:");
lcd.print(flexValue);
lcd.print("T1:");
lcd.print(DELAY_TIME);
lcd.print("T2:");
lcd.print(LDELAY_TIME);*/
Serial.print("[DEBUG] Pwr: ");
Serial.print(isOn);
Serial.print(" | B1: ");
Serial.print(button1);
Serial.print(" - B2: ");
Serial.print(button2);
Serial.print(" - B3: ");
Serial.print(button3);
Serial.print(" | Lt: ");
Serial.print(photoValue);
Serial.print(" | Flex: ");
Serial.print(flexValue);
Serial.print(" | Time: ");
Serial.print(DELAY_TIME);
Serial.print(" | LTime: ");
Serial.println(LDELAY_TIME);
//delay(LDELAY_TIME);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment