/*
 A first shot minimal DTHC simulator with a pot to simulate divided down torch voltage.
 Outputs can be fed into Mach3 as opposed to the LEDs to see Torch UP & Down work.
 
 The circuit:
 * 2 LEDs and a potentiometer
 * center pin of the potentiometer to the analog pin 0
 * one side pin to ground
 * the other side pin to +5V
 * One LED anode (long leg) attached to digital output 12
 * LED cathode (short leg) attached to ground
 * The other LED anode (long leg) attached to digital output 13
 * LED cathode (short leg) attached to ground
 
 * Note: because most Arduinos have a built-in LED attached 
 to pin 13 on the board, one LED is optional.
 
 */

int sensorPin = 0;    // select the input pin for the potentiometer
int torchUpPin = 12;      // select the pin for the LED
int torchDownPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // define the pins
  pinMode(torchUpPin, OUTPUT);  
  pinMode(torchDownPin, OUTPUT);  

  // set up serial monitor
  Serial.begin(9600);
  
  // init LEDs
  // turn torch up off
  digitalWrite(torchUpPin, LOW);
  // turn torch down off
  digitalWrite(torchDownPin, LOW);
}

void loop() {
  // read the value from the sensor
  sensorValue = analogRead(sensorPin);    

  // print the result to the serial monitor
  Serial.println(sensorValue);

  // ADC range is 0-1023 so I've decided anything
  // above 600 signals torch down and anything below 500
  // signals torch up. The 100 gap is the dead zone.
  if (sensorValue > 600)
  {
    digitalWrite(torchUpPin, LOW);
    digitalWrite(torchDownPin, HIGH);
  }

  if (sensorValue < 500)
  {
    digitalWrite(torchDownPin, LOW);
    digitalWrite(torchUpPin, HIGH);
  }

  // keep things at a reasonable pace
  delay(100);          
}
