// Define global variables.
// assign meaningful names to digital and analog pins.
// the Arduino will recognize A0, A1, etc. for the analog pins.
// Analog pins on the Arduino are INPUT ONLY.
// variable type "byte" is an 8 bits unsined number from 0 to 255.
// "bytes" use less memory than "integers".

const byte POT1_Pin = A0;  // 10K Linear Potentiometer - Wiper
const byte G_Pin = 8;  // Green LED Digital Pin
const byte Y_Pin = 9;  // Yellow LED Digital Pin
const byte R_Pin = 10; // Red LED Digital Pin


// the setup function runs once when you press reset or power the board
void setup() 
{// start setup loop
  

  // initialize the serial port at selected baud rate
  Serial.begin(9600); // open serial port at 9600 baud
  
  // initialize digital pins as an output.
  pinMode(G_Pin, OUTPUT);
  pinMode(Y_Pin, OUTPUT);
  pinMode(R_Pin, OUTPUT);
  
  // Turn all LED's off to start
  // digitalWrite(LEDG1_Pin, LOW);
  digitalWrite(G_Pin, LOW);
  digitalWrite(Y_Pin, LOW);
  digitalWrite(R_Pin, LOW);
  
}// endof setup loop

// the loop function runs over and over again forever
// it is fast, millionths of a second if you don't delay it somehow
void loop() 
{// start infinite loop
  
  // initialize variables uses to read and map pot wiper reading
  int pot1Value = analogRead(POT1_Pin);  // read value (0-5vdc) of Pot1 wiper
  
  // for float type: must use decimals for at least one operand (ex) int/1023.0 NOT int/1023
  float pot1Volts = (pot1Value/1023.0) * 5.0;  // coververt 0-1023 analogRead values to 0-5vdc
  
  if((pot1Value >= 0) && (pot1Value <= 341)){ // 1023/3 = 341
    digitalWrite(R_Pin, HIGH);   // light red led
    digitalWrite(Y_Pin, LOW);
    digitalWrite(G_Pin, LOW);
    Serial.println("Red LED:");  // prints caption "ln" starts new line
    Serial.print("A0 Value");
    Serial.print(" ");                  // prints a space
    Serial.println(pot1Value);             // send one line of data to serial port
    Serial.print("A0 Volts");
    Serial.print(" ");                  // prints a space
    Serial.print(pot1Volts,2);  // float format is (defaullt base 10, decimal places)
    Serial.print(" "); 
    Serial.println("vdc"); 
    delay(2000);
  }
   
  else if((pot1Value > 341) && (pot1Value <= 682)){
    digitalWrite(Y_Pin, HIGH);   
    digitalWrite(R_Pin, LOW);
    digitalWrite(G_Pin, LOW);
    Serial.println("Yellow LED:");  // prints caption "ln" starts new line
    Serial.print("A0 Value");
    Serial.print(" ");                  // prints a space
    Serial.println(pot1Value);             // send one line of data to serial port
    Serial.print("A0 Volts");
    Serial.print(" ");                  // prints a space
    Serial.print(pot1Volts,2);  // float format is (defaullt base 10, decimal places)
    Serial.print(" "); 
    Serial.println("vdc"); 
    delay(2000);
  }
  
  else if((pot1Value > 682) && (pot1Value <= 1023)){  
    digitalWrite(G_Pin, HIGH);   
    digitalWrite(Y_Pin, LOW);
    digitalWrite(R_Pin, LOW);
    Serial.println("Green LED:");  // prints caption "ln" starts new line
    Serial.print("A0 Value");
    Serial.print(" ");                  // prints a space
    Serial.println(pot1Value);             // send one line of data to serial port
    Serial.print("A0 Volts");
    Serial.print(" ");                  // prints a space
    Serial.print(pot1Volts,2);  // float format is (defaullt base 10, decimal places)
    Serial.print(" "); 
    Serial.println("vdc"); 
    delay(2000);
  }
        
}// endof infinite loop
