Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of FYDP_Final2 by
ACS712.cpp
00001 #include "ACS712.h" 00002 00003 ACS712::ACS712() : sensor(SENSOR_PIN), 00004 currentSwitch(SWITCH_PIN) 00005 { 00006 currentSwitch = 1; //turn off the currentSwitch (1 = off, 0 = on) 00007 m = 0; 00008 b = 0; 00009 } 00010 00011 void ACS712::calibrate(){ 00012 float V1,V2,A1,A2; 00013 A1 = LOW_CURRENT; 00014 A2 = HIGH_CURRENT; 00015 00016 currentSwitch = 1; 00017 wait_ms(100); 00018 //wait_ms(5000);//MARK 00019 V1 = read_sensor(CALIBRATION_SAMPLES,10); 00020 00021 currentSwitch = 0; 00022 //Thread::wait(100); 00023 //wait_ms(5000);//MARK 00024 wait_ms(100); 00025 V2 = read_sensor(CALIBRATION_SAMPLES,10); 00026 currentSwitch = 1; 00027 m = (A2-A1)/(V2-V1); 00028 b = A1 - V1*m; 00029 } 00030 float ACS712::get_current(){ 00031 return m*read_sensor(10,0) + b; 00032 } 00033 float ACS712::read_sensor(){ 00034 sensor_val = sensor.read(); 00035 return sensor_val; 00036 } 00037 float ACS712::read_sensor(int n, int t){ 00038 sensor_val = 0; 00039 for(int i = 0; i<n; i++){ 00040 sensor_val += sensor.read(); 00041 wait_ms(t); 00042 //Thread::wait(10); 00043 } 00044 sensor_val = sensor_val/float(n); 00045 return sensor_val; 00046 }
Generated on Tue Jul 12 2022 16:56:35 by
1.7.2
