View difference between Paste ID: pmrLEifA and BPCuE3gy
SHOW: | | - or go back to the newest paste.
1
const int relay = 2;
2
const int sensorIn = A0;
3
int mVperAmp = 100; // use 100 for 20A Module and 66 for 30A Module
4
int relaystate;
5
double sensorVal;
6
7
void setup() {
8
  Serial.begin(9600);
9
  pinMode(relay, OUTPUT);
10
  pinMode(sensorIn, INPUT);
11
  digitalWrite(relay, LOW);
12
}
13
14
void loop() {
15
16
  delay(500);
17
  relaystate = digitalRead(relay);   //Check if the fan is on/off
18
  sensorVal = getVPP();
19
  Serial.println(sensorVal); //Printing out the AmpsRMS
20
21-
  if (sensorVal >= .50) {
21+
  while(sensorVal >= .50) {
22
    digitalWrite(relay, HIGH);  //If the current is drawing, then turn on fan
23
    delay(3000);
24
    Serial.println(sensorVal);
25-
    if ((sensorVal < .50) && (relaystate = HIGH)) { //If projector turns off, wait 1 minute and turn off fan
25+
26-
      Serial.println("Projector off, waiting one minute to cool off.");
26+
  if (sensorVal < .50) { //If projector turns off, wait 1 minute and turn off fan
27-
      delay(60000);
27+
    Serial.println("Projector off, waiting one minute to cool off.");
28-
      digitalWrite(relay, LOW);
28+
    delay(60000);
29
    digitalWrite(relay, LOW);
30
   }
31
}
32
33
float getVPP() {
34
  double Voltage = 0;
35
  double VRMS = 0;
36
  double AmpsRMS = 0;
37
38
  int readValue;             //value read from the sensor
39
  int maxValue = 0;          // store max value here
40
  int minValue = 1024;          // store min value here
41
42
  uint32_t start_time = millis();
43
  while ((millis() - start_time) < 1000) //sample for 1 Sec
44
  {
45
    readValue = analogRead(sensorIn);
46
    // see if you have a new maxValue
47
    if (readValue > maxValue)
48
    {
49
      /*record the maximum sensor value*/
50
      maxValue = readValue;
51
    }
52
    if (readValue < minValue)
53
    {
54
      /*record the maximum sensor value*/
55
      minValue = readValue;
56
    }
57
  }
58
  // Subtract min from max
59
  //  Serial.print(maxValue);
60
  //  Serial.println(" Max Value");
61
  //  Serial.print(minValue);
62
  //  Serial.println(" Min Value");
63
  Voltage = ((maxValue - minValue) * 5.0) / 1024.0;
64
  VRMS = (Voltage / 2.0) * 0.707;
65
  AmpsRMS = (VRMS * 1000) / mVperAmp;
66
67
  return AmpsRMS;
68
}