...

четверг, 5 февраля 2015 г.

Arduino Pro Mini + токовый датчик GY-712 ведут контроль перегорания ламп

float srab = 0.650;

const int currentPin1 = 0; // Аналоговый вход с датчика тока


const unsigned long sampleTime = 100000UL; // sample over 100ms, it is an exact number of cycles for both 50Hz and 60Hz mains

const unsigned long numSamples = 250UL; // choose the number of samples to divide sampleTime exactly, but low enough for the ADC to keep up

const unsigned long sampleInterval = sampleTime/numSamples; // the sampling interval, must be longer than then ADC conversion time

//const int adc_zero = 512; // relative digital zero of the arudino input from ACS712 (could make this a variable and auto-adjust it)

int adc_zero1; //Переменная автоматической калибровки


float first;


void setup()

{

pinMode(13, OUTPUT);// Пин индикатора

pinMode(12, OUTPUT); // пин звука

pinMode(2, INPUT); // пин входа реле (тумблер)


digitalWrite(13, LOW);

digitalWrite(12, LOW);


while(digitalRead(2)==0 ) { // Если включен тумблер то выдать звуковой и световой сиглак пока не отключат для возможности калибровки

tone(12,2000,500);

digitalWrite(13, HIGH);

delay(500);

digitalWrite(13, LOW);

delay(500);

}


tone(12,1500,100); // Звук старта калибровки

delay(180);

tone(12,1500,100);

delay(180);

tone(12,1500,100);


//Serial.begin(9600);

adc_zero1 = determineVQ(currentPin1); //Quiscent output voltage — the average voltage ACS712 shows with no load (0 A)

digitalWrite(13, HIGH);

tone(12,1000,100);


delay(150);

digitalWrite(13, LOW);


}


void loop(){

// Serial.print(«ACS712@A2_1:»);Serial.print(readCurrent(currentPin1,adc_zero1),3);Serial.println(" mA");

delay(300);


if(digitalRead(2)==0){ // Если включен тумблер то:

if (readCurrent(currentPin1,adc_zero1)> srab) // Если ток больше указанного порга сработки то:

{

digitalWrite(13, HIGH); // Включить индикатор


}

else // Иначе

{

if(digitalRead(2)==0){ //Если тумблер все еще включен то:

digitalWrite(13, LOW); // Погасить индикатор

tone(12,2000,500); } // и выдать звуковой сигнал

}


}

else { // Иначе

digitalWrite(13, LOW); // // Погасить индикатор

}

//-------------------------------------------------------------------------------------------------------------------------------------------------


delay(250);


}


int determineVQ(int PIN) {

//Serial.print(«estimating avg. quiscent voltage:»);

long VQ = 0;

//read 5000 samples to stabilise value

for (int i=0; i<5000; i++) {

VQ += analogRead(PIN);

delay(1);//depends on sampling (on filter capacitor), can be 1/80000 (80kHz) max.

}

VQ /= 5000;

//Serial.print(map(VQ, 0, 1023, 0, 5000));Serial.println(" mV");

return int(VQ);

}


float readCurrent(int PIN, int adc_zero0)

{

unsigned long currentAcc = 0;

unsigned int count = 0;

unsigned long prevMicros = micros() — sampleInterval;

while (count < numSamples)

{

if (micros() — prevMicros >= sampleInterval)

{

int adc_raw = analogRead(PIN) — adc_zero0;

currentAcc += (unsigned long)(adc_raw * adc_raw);

++count;

prevMicros += sampleInterval;

}

}

float rms = sqrt((float)currentAcc/(float)numSamples) * (75.7576 / 1024.0);

return rms;

//Serial.println(rms);


}


Recommended article: Chomsky: We Are All – Fill in the Blank.

This entry passed through the Full-Text RSS service - if this is your content and you're reading it on someone else's site, please read the FAQ at http://ift.tt/jcXqJW.


Комментариев нет:

Отправить комментарий