/* YourSmartDuino-EM  EZP-AnalogInOut-Strobe
  - WHAT IT DOES: Reads analog values, controls devices
  - Uses the KEYES Easy-Plug Modules

  - V3. 2/12/2019
   Questions: terry@yourduino.com
  - SEE the comments after "//" on each line below
  - CONNECTIONS: KEYES Easy-Plug Control Board V2.0
    NOTE: Different Software examples use a different sets of modules.


  /*-----( Import needed libraries )-----*/

// Get the LCD I2C Library here:
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
// Move any other LCD libraries to another folder or delete them
// See Library "Docs" folder for possible commands etc.
#include <LiquidCrystal_I2C.h>

/*----------------( Declare Constants and Pin Numbers )-------------------*/

//----( ANALOG INPUT Pins)----
#define ROTATION_PIN       A0    // Analog inputs from Potentiometer etc
#define LIGHT_SENSOR_PIN   A1    // Photoresistor

//----( DIGITAL OUTPUT Pins)----
#define LED_MODULE_PIN    6
#define STROBE_LED_PIN    9
#define BUZZER_PIN        7
#define ONBOARD_LED_PIN   13
#define SERVO_PIN         A2  //Used as digital output

//----( DIGITAL INPUT Pins)----
#define SWITCH_PIN        A3   // Used for a variety of On-OFF Devices


//------( LOCAL DEFINITIONS )---------

#define ON       1
#define OFF      0
#define DarkValue 500       // Onboard Light Sensor

/*--------------------( Declare objects )-------------------------*/
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);   // Define the LCD Display


/*------------------------( Declare Variables )-----------------------------------*/
int   KnobValue;        // Analog values from 0..1023
int   PhotosensorValue; // Analog values from 0..1023

//------( Variables for Strobe LED )-------
unsigned long     StrobeLastMs ;
unsigned long     StrobeLedOnTime  = 10L ;
unsigned long     StrobeLedOffTime = 1000L ;
boolean InStrobeTime = false;
boolean StrobeIsOn   = false;

int     ServoPosition ;

void setup()   /******************* SETUP: RUNS ONCE *************************/
{
  Serial.begin(115200);        // Start up the Serial Monitor

  lcd.begin(16, 2);  // initialize the lcd for 16 chars 2 lines, turn on backlight
  lcd.noBacklight();
  delay(500);
  lcd.backlight(); // finish with backlight on

  lcd.setCursor(0, 0); //Start at character 0 on line 0
  lcd.print(F("YourSmart!Duino"));
  lcd.setCursor(0, 1); //Start at character 0 on line 0
  lcd.print(F("YourDuino.com"));
  //***
  pinMode(LED_MODULE_PIN, OUTPUT);
  pinMode(ONBOARD_LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN,     OUTPUT);
  pinMode(STROBE_LED_PIN, OUTPUT);


  Serial.println("Option  3: Analog Input to LED Controls");

  lcd.setCursor(0, 0); //Start at character 0 on line 0
  //-------------------1234567890123456----------
  lcd.print(F("3:KNOB sets LEDS"));

  delay(2000);
}//--(end setup )---


void loop()   /********************** LOOP: RUNS CONSTANTLY ************************/
{

  /*-------------( Option *** )---------------

  */
KnobValue = analogRead(ROTATION_PIN);   // Read the position of the Potentiometer
    if (KnobValue < 15) 
    {
      KnobValue = 0;
    }

    analogWrite(LED_MODULE_PIN, ((KnobValue / 4) ) );

    /*-----------( Set up and control strobe frequency and duration )-----------------------------*/
    StrobeLedOffTime = (1023 - KnobValue) / 2L;  // Set Strobe parameters from Analog Input
    StrobeLedOnTime  = StrobeLedOffTime / 10L;

    if (InStrobeTime)
    {
      if
      (
        ((StrobeLastMs + StrobeLedOnTime) < millis())
        &&
        StrobeIsOn
      )
      {
        StrobeIsOn = false;
        digitalWrite(STROBE_LED_PIN, LOW);
      }

      if
      (
        ((StrobeLastMs + StrobeLedOnTime + StrobeLedOffTime) < millis())
        &&
        ! StrobeIsOn
      )
      {
        InStrobeTime = false;
      }


    } // END InStrobeTime

    else // Begin another Strobe cycle
    {
      StrobeLastMs = millis();
      InStrobeTime = true;
      digitalWrite(STROBE_LED_PIN, HIGH);
      StrobeIsOn   = true;

    }

    /*----Set Servo position from Potentiometer value and Send Servo Position to Servo )-----*/

    ServoPosition  = map((1023 - KnobValue), 0, 1023, 900 , 2000);
    PointServo(ServoPosition);


    

}//--(end main loop )---


/*--------------------( Declare User-written Functions )------------------------*/
//-------------( PointServo )--------------
void PointServo(int ServoAngle)
{

  for (int i = 0; i < 3; i++) // Send the pulse 10 times
  {
    digitalWrite(SERVO_PIN, HIGH);
    delayMicroseconds(ServoAngle);
    digitalWrite(SERVO_PIN, LOW);
    delay(1);
  }
}//END  PointServo
/*---------------------------*/
//*********( THE END )***********
