Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int relay = 2;
- const int sensorIn = A0;
- int mVperAmp = 100; // use 100 for 20A Module and 66 for 30A Module
- int relaystate;
- double sensorVal;
- void setup() {
- Serial.begin(9600);
- pinMode(relay, OUTPUT);
- pinMode(sensorIn, INPUT);
- digitalWrite(relay, LOW);
- }
- void loop() {
- delay(500);
- relaystate = digitalRead(relay); //Check if the fan is on/off
- sensorVal = getVPP();
- Serial.println(sensorVal); //Printing out the AmpsRMS
- while(sensorVal >= .50) {
- digitalWrite(relay, HIGH); //If the current is drawing, then turn on fan
- delay(3000);
- Serial.println(sensorVal);
- }
- if (sensorVal < .50) { //If projector turns off, wait 1 minute and turn off fan
- Serial.println("Projector off, waiting one minute to cool off.");
- delay(60000);
- digitalWrite(relay, LOW);
- }
- }
- float getVPP() {
- double Voltage = 0;
- double VRMS = 0;
- double AmpsRMS = 0;
- int readValue; //value read from the sensor
- int maxValue = 0; // store max value here
- int minValue = 1024; // store min value here
- uint32_t start_time = millis();
- while ((millis() - start_time) < 1000) //sample for 1 Sec
- {
- readValue = analogRead(sensorIn);
- // see if you have a new maxValue
- if (readValue > maxValue)
- {
- /*record the maximum sensor value*/
- maxValue = readValue;
- }
- if (readValue < minValue)
- {
- /*record the maximum sensor value*/
- minValue = readValue;
- }
- }
- // Subtract min from max
- // Serial.print(maxValue);
- // Serial.println(" Max Value");
- // Serial.print(minValue);
- // Serial.println(" Min Value");
- Voltage = ((maxValue - minValue) * 5.0) / 1024.0;
- VRMS = (Voltage / 2.0) * 0.707;
- AmpsRMS = (VRMS * 1000) / mVperAmp;
- return AmpsRMS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement