/*
 * 180207: Began using the statistical and rate of change in interval (intergal area) and state machine
 *     See 180207_1343_statisitcs.ods and the associated *_raw.csv
 *     Over 2k black and white sample reads.  Note that the White read various over distance
 *     the closer to the sensor the tighter the White reading.  Half inch seems the most reliable.
 *
 *
 * 180203:  Need Light Sensor measure of RPM for Sherline Spindle
 * Search on "arduino Light Sensor measure of rotation"
 * See MeLightSensor.h and associated MakeBlock Example for MeOrion
 * See http://forum.arduino.cc/index.php?topic=226695.0 for example of reading rotation speed
 *     of DC motor.
 * See alternates
 *     https://warwick.ac.uk/fac/sci/wmg/about/outreach/resources/rotation_sensor_worksheet.pdf
 *     http://www.instructables.com/id/Measure-RPM-DIY-Portable-Digital-Tachometer/
 *     http://arduinoprojects101.com/arduino-rpm-counter-tachometer/   IR LED and Ir Photoresceptor
 * See LCD_LED.ino
 *
 * Sherline End Mill  Holder 1/8
 *   Used reflective aluminum mylar strip and then wrapped holder
 *   with heavy duty velco with the fuzzy section used.  Left about
 *   a 1/8 section gap in the velco exposing the mylar.  Tried small
 *   section of whitepaper.  Not noticable differnece between white
 *   paper and aluminum mylar tape.
 *
 *   Pos1 & Pos2 seemed to give same results
 *   ===> Direction of rotation ===>
 *    Pos1:      Led + Sensor Parallel to rotation
 *    Pos2:      Sensor + Led Parallel to rotation
 *    Pos3:      LED & Sensor Pependicular to rotation seemed worst
 *
 *    Use GtkTerm with Configuarion Port: /dev/ttyUSB set to 9600 baud
 */



#include "Arduino.h"
#include "MeOrion.h"         // for Orion board
#include <SoftwareSerial.h>
// 4 Grove#include "rgb_lcd.h"
#include <Wire.h>
//#include "rpm_lightCntr.h"


/*  Grove and mega2560
#ifdef __AVR_ATmega2560__

#include "rgb_lcd.h"
rgb_lcd lcd;
const int  LED = 2;
int ledState = LOW;
#else
MeLightSensor lightSensor(PORT_6);
#endif
*/





/* Private variables ---------------------------------------------------------*/
//int value = 0;      /* a variable for the lightSensor's value */
MeLightSensor lightSensor(PORT_6);  //6,7,8

const int THRESHOLD = 483;
const int XCARE = -1;
int state = XCARE;
// LOW or 0:      Last DAC valie black urrent DAC val is white
// HIGH or 1:     Prior DAC value (black) followed by current DAC val of white or black
// XCARE or -1:   Last DAC value (black) and current DAC reading value is black

int knt = 0;
int count = 0;
int nbr = 0;
int omega;
int rpm = 0;
//static int matrix[3] = {-1,4,-1};
// times are in terms of when the program started clicking away.
int oldVal = 0;
int curVal = 0;
long ti1 = 0;  // time as event enters the interval of interest
long ti2 = 0;  // time as event exits the interval of interest.
long tr  = 0;  //time that the reading current was marked.
const int rpmCnv = 9.5492965964254;  //https://www.convertunits.com/from/radian/second/to/RPM



void printdatawRPM()
{
  Serial.print(nbr);
  Serial.print(",");
  Serial.print(curVal);
  Serial.print(",");
  Serial.print(ti1);
  Serial.print(",");
  Serial.print(ti2);
  Serial.print(",");
  Serial.print(omega);
  Serial.print(",");
  Serial.println(rpm);

}


void printdatawoORPM()
{
  Serial.print(nbr);
  Serial.print(",");
  Serial.print(curVal);
  Serial.print(",");
  Serial.print(ti1);
  Serial.print(",");
  Serial.print(ti2);
  Serial.print(",");
  Serial.print(0);
  Serial.print(",");
  Serial.println(999999);

}


void printdatawoRPM()
{
  Serial.print(nbr);
  Serial.print(",");
  Serial.print(curVal);
  Serial.print(",");
  Serial.print(ti1);
  Serial.print(",");
  Serial.print(tr);
  Serial.print(",");
  Serial.print(-1);
  Serial.print(",");
  Serial.println(-1);

}


void markRPM()
{
if((curVal > 483) and (state == LOW))
{
    ti2=millis();
    omega = 360000/(ti2 - ti1);  //radians/second
    rpm = omega * rpmCnv;
    printdatawRPM();
}
 else // state is HIGH
 {printdatawoORPM();}
 ti1=ti2;
}




int readDAC()
{
// Insure light LED is lit.
// MePort::aRead2() - DAC value of current light sensor's output.
int value = lightSensor.read();
tr = millis();;
nbr++;
return value;
}


void setup()
{
  lightSensor.lightOn();
  Serial.begin(9600);
  delay(5000);  //delay for gkterm port setup
}

void loop()
{
	oldVal = curVal;
    curVal = readDAC;
	switch (state)
	{
    case 0:   // LOW transisition from low to high
    	if ((curVal >= THRESHOLD) and (oldVal < THRESHOLD))
    	{
    		state = HIGH;
    		markRPM();
    		printdatawRPM();
    	}

      break;
    case 1:   //HIGH
    	if ((curVal >= THRESHOLD) and (oldVal >= THRESHOLD))
    	{
    		state = HIGH;
    		printdatawoORPM();
    	}
    	else
    	{
    		state = XCARE;
    		printdatawoRPM();
    	}
       break;
    default: //XCARE or -1
        printdatawoRPM();

      break;
	} // end of switch - case

  delay(2);
 // delay(1);  probably too fast
}



