/*
This software is provided "as-is," without any express or implied warranty. 
In no event shall CW Electronx be held liable for any damages arising from the use of the software.
*/

int IN0 = 12;
int DEN = 11;
int DSEL = 10;
int IN1 = 9;
int sensorValue;
float voltage;
float currentA;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 38400 bits per second:
  Serial.begin(38400);
  // initialize the digital pin as an output.
  pinMode(IN0, OUTPUT); 
  pinMode(DEN, OUTPUT); 
  pinMode(DSEL, OUTPUT); 
  pinMode(IN1, OUTPUT); 
}

// the loop routine runs over and over again forever:
void loop() {
  //Analog Pin Enable Pin Set High to enable Current Sensing pin
  digitalWrite(DEN,HIGH);
    
  //Request Output0 to be ON / Request Output1 to be OFF
  digitalWrite(IN0, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(IN1, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
  
//Read both of the current sensing
  Serial.println("Output 0 ON");
  // read the input on analog pin 5 (this case you  are reading Output 0)
  digitalWrite(DSEL,LOW);
  sensorValue = analogRead(A5);
  // Convert the analog reading (which goes from 0 - 1023) to a (0 - 5V):
  voltage = sensorValue * (5.0 / 1023.0);
  // IL = V*Is*IRatio/ Rs →  IL = 5V * 3000A/mA * Is / 1k2 
  currentA = voltage / 1200.0 * 1000 * 3; 
  Serial.print("Out 0: ");
  Serial.print(currentA);
  Serial.println(" Amps");
   
  // read the input on analog pin 5 (this case you  are reading Output 0)
  digitalWrite(DSEL,HIGH);
  sensorValue = analogRead(A5);
  // Convert the analog reading (which goes from 0 - 1023) to a (0 - 5V):
  voltage = sensorValue * (5.0 / 1023.0);
  // IL = V*Is*IRatio/ Rs →  IL = 5V * 3000A/mA * Is / 1k2 
  currentA = voltage / 1200.0 * 1000 * 3; 
  Serial.print("Out 1: ");
  Serial.print(currentA);
  Serial.println(" Amps");
  Serial.println("");
     
  // print out the value you read:
  // Serial.println(voltage);
  
  //Request Output0 to be OFF
  //Request Output1 to be OFF
  digitalWrite(IN0, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(IN1, LOW);    // turn the LED off by making the voltage LOW
  delay(500);               // wait for a second
    
  //Request Output0 to be OFF / Request Output1 to be ON
  digitalWrite(IN0, LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(IN1, HIGH);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
//Read both of the current sensing
  Serial.println("Output 1 ON");
  // read the input on analog pin 5 (this case you  are reading Output 0)
  digitalWrite(DSEL,LOW);
  sensorValue = analogRead(A5);
  // Convert the analog reading (which goes from 0 - 1023) to a (0 - 5V):
  voltage = sensorValue * (5.0 / 1023.0);
  // IL = V*Is*IRatio/ Rs →  IL = 5V * 3000A/mA * Is / 1k2 
  currentA = voltage / 1200.0 * 1000 * 3; 
  Serial.print("Out 0: ");
  Serial.print(currentA);
  Serial.println(" Amps");
   
  // read the input on analog pin 5 (this case you  are reading Output 0)
  digitalWrite(DSEL,HIGH);
  sensorValue = analogRead(A5);
  // Convert the analog reading (which goes from 0 - 1023) to a (0 - 5V):
  voltage = sensorValue * (5.0 / 1023.0);
  // IL = V*Is*IRatio/ Rs →  IL = 5V * 3000A/mA * Is / 1k2 
  currentA = voltage / 1200.0 * 1000 * 3; 
  Serial.print("Out 1: ");
  Serial.print(currentA);
  Serial.println(" Amps");
  Serial.println("");
      
  //Request Output0 to be OFF / Request Output1 to be OFF
  digitalWrite(IN0, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(IN1, LOW);    // turn the LED off by making the voltage LOW
  delay(500);               // wait for a second
}
