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 ACS712 by
ACS712.h@1:4f9effb20c29, 2016-03-13 (annotated)
- Committer:
- nyengele
- Date:
- Sun Mar 13 16:43:23 2016 +0000
- Revision:
- 1:4f9effb20c29
- Parent:
- 0:a35011a2fdaa
- Child:
- 2:2b5233355986
updated device class, fixed a few bugs
Who changed what in which revision?
User | Revision | Line number | New 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&); |
nyengele | 1:4f9effb20c29 | 10 | operator float() { return read(); } |
mikeb | 0:a35011a2fdaa | 11 | |
mikeb | 0:a35011a2fdaa | 12 | private: |
mikeb | 0:a35011a2fdaa | 13 | AnalogIn sensor; |
mikeb | 0:a35011a2fdaa | 14 | float translate(float); |
mikeb | 0:a35011a2fdaa | 15 | float ratio; |
mikeb | 0:a35011a2fdaa | 16 | short type; |
mikeb | 0:a35011a2fdaa | 17 | |
mikeb | 0:a35011a2fdaa | 18 | }; |
mikeb | 0:a35011a2fdaa | 19 | |
mikeb | 0:a35011a2fdaa | 20 | ACS712::ACS712(PinName _pin, float voltDivRatio, short type) : sensor(_pin){ |
mikeb | 0:a35011a2fdaa | 21 | ratio = voltDivRatio; |
mikeb | 0:a35011a2fdaa | 22 | type = type; |
mikeb | 0:a35011a2fdaa | 23 | } |
mikeb | 0:a35011a2fdaa | 24 | |
mikeb | 0:a35011a2fdaa | 25 | float ACS712::translate(float val){ |
mikeb | 0:a35011a2fdaa | 26 | switch(type){ |
mikeb | 0:a35011a2fdaa | 27 | case 5: |
mikeb | 0:a35011a2fdaa | 28 | return (val*ratio - 2.5*ratio)/(.185*ratio); |
mikeb | 0:a35011a2fdaa | 29 | break; |
mikeb | 0:a35011a2fdaa | 30 | case 20: |
mikeb | 0:a35011a2fdaa | 31 | return (val*ratio - 2.5*ratio)/(.1*ratio); |
mikeb | 0:a35011a2fdaa | 32 | break; |
mikeb | 0:a35011a2fdaa | 33 | case 30: |
mikeb | 0:a35011a2fdaa | 34 | return (val*ratio - 2.5*ratio)/(.066*ratio); |
mikeb | 0:a35011a2fdaa | 35 | break; |
mikeb | 0:a35011a2fdaa | 36 | default: |
mikeb | 0:a35011a2fdaa | 37 | return 999; |
mikeb | 0:a35011a2fdaa | 38 | break; |
mikeb | 0:a35011a2fdaa | 39 | |
mikeb | 0:a35011a2fdaa | 40 | } |
mikeb | 0:a35011a2fdaa | 41 | } |
mikeb | 0:a35011a2fdaa | 42 | |
mikeb | 0:a35011a2fdaa | 43 | |
mikeb | 0:a35011a2fdaa | 44 | float ACS712::read(){ |
nyengele | 1:4f9effb20c29 | 45 | return ACS712::translate(sensor * 3.3); |
mikeb | 0:a35011a2fdaa | 46 | } |
mikeb | 0:a35011a2fdaa | 47 | |
nyengele | 1:4f9effb20c29 | 48 | ACS712& ACS712::operator=(ACS712& rhs){ |
nyengele | 1:4f9effb20c29 | 49 | sensor = rhs.sensor; |
nyengele | 1:4f9effb20c29 | 50 | ratio = rhs.ratio; |
nyengele | 1:4f9effb20c29 | 51 | type = rhs.type; |
nyengele | 1:4f9effb20c29 | 52 | return this; |
mikeb | 0:a35011a2fdaa | 53 | } |