Demo of the ACS712 Library

Dependencies:   Servo mbed

Fork of ece4180_lab4_ACS712 by Gedeon Nyengele

Committer:
mikeb
Date:
Wed Mar 09 04:19:10 2016 +0000
Revision:
0:a35011a2fdaa
Child:
1:4f9effb20c29
Lab 4 Version A. Untested, but basics are there

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mikeb 0:a35011a2fdaa 1 #include <mbed.h>
mikeb 0:a35011a2fdaa 2
mikeb 0:a35011a2fdaa 3 class ACS712 {
mikeb 0:a35011a2fdaa 4
mikeb 0:a35011a2fdaa 5 public:
mikeb 0:a35011a2fdaa 6 ACS712(PinName _pin, float voltDivRatio = 1, short type = 5);
mikeb 0:a35011a2fdaa 7
mikeb 0:a35011a2fdaa 8 float read();
mikeb 0:a35011a2fdaa 9 float operator=(ACS712&);
mikeb 0:a35011a2fdaa 10 //void sampleInterval(float);
mikeb 0:a35011a2fdaa 11 //void sampleFrequency(float);
mikeb 0:a35011a2fdaa 12 //void bufferLength(int);
mikeb 0:a35011a2fdaa 13 //float[] readBuffer();
mikeb 0:a35011a2fdaa 14
mikeb 0:a35011a2fdaa 15 private:
mikeb 0:a35011a2fdaa 16 AnalogIn sensor;
mikeb 0:a35011a2fdaa 17 float translate(float);
mikeb 0:a35011a2fdaa 18 //float[] circularBuffer;
mikeb 0:a35011a2fdaa 19 //float interval;
mikeb 0:a35011a2fdaa 20 float ratio;
mikeb 0:a35011a2fdaa 21 short type;
mikeb 0:a35011a2fdaa 22
mikeb 0:a35011a2fdaa 23 };
mikeb 0:a35011a2fdaa 24
mikeb 0:a35011a2fdaa 25 ACS712::ACS712(PinName _pin, float voltDivRatio, short type) : sensor(_pin){
mikeb 0:a35011a2fdaa 26 ratio = voltDivRatio;
mikeb 0:a35011a2fdaa 27 type = type;
mikeb 0:a35011a2fdaa 28 }
mikeb 0:a35011a2fdaa 29
mikeb 0:a35011a2fdaa 30 float ACS712::translate(float val){
mikeb 0:a35011a2fdaa 31 switch(type){
mikeb 0:a35011a2fdaa 32 case 5:
mikeb 0:a35011a2fdaa 33 return (val*ratio - 2.5*ratio)/(.185*ratio);
mikeb 0:a35011a2fdaa 34 break;
mikeb 0:a35011a2fdaa 35 case 20:
mikeb 0:a35011a2fdaa 36 return (val*ratio - 2.5*ratio)/(.1*ratio);
mikeb 0:a35011a2fdaa 37 break;
mikeb 0:a35011a2fdaa 38 case 30:
mikeb 0:a35011a2fdaa 39 return (val*ratio - 2.5*ratio)/(.066*ratio);
mikeb 0:a35011a2fdaa 40 break;
mikeb 0:a35011a2fdaa 41 default:
mikeb 0:a35011a2fdaa 42 return 999;
mikeb 0:a35011a2fdaa 43 break;
mikeb 0:a35011a2fdaa 44
mikeb 0:a35011a2fdaa 45 }
mikeb 0:a35011a2fdaa 46 }
mikeb 0:a35011a2fdaa 47
mikeb 0:a35011a2fdaa 48
mikeb 0:a35011a2fdaa 49 float ACS712::read(){
mikeb 0:a35011a2fdaa 50 return ACS712::translate(sensor);
mikeb 0:a35011a2fdaa 51 }
mikeb 0:a35011a2fdaa 52
mikeb 0:a35011a2fdaa 53 float ACS712::operator=(ACS712& rhs){
mikeb 0:a35011a2fdaa 54 return rhs.read();
mikeb 0:a35011a2fdaa 55 }
mikeb 0:a35011a2fdaa 56