/*
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 CS_DIS = 8;

int IN1 = 9;
int IN2 = 10;
int IN3 = 11;
int IN4 = 12;

int sensorValue_IS1;
float voltage_IS1;
float currentA_IS1;

// 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(CS_DIS, OUTPUT); 
  pinMode(IN1, OUTPUT); 
  //pinMode(IN2, OUTPUT);
  //pinMode(IN3, OUTPUT);
  //pinMode(IN4, OUTPUT);
}

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