Saw Zheng Yi / Mbed 2 deprecated IDP_LCD_POWER

Dependencies:   ESP8266 Servo TextLCD mbed

Fork of ACS712HelloWorldDemo by m b

Committer:
nyengele
Date:
Sun Mar 13 17:14:35 2016 +0000
Revision:
3:9cae7baf7ccd
Parent:
2:2b5233355986
Child:
4:f6c160553ca3
added comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mikeb 0:a35011a2fdaa 1 #include <mbed.h>
nyengele 3:9cae7baf7ccd 2 /** A Hall-Effect sensor for measuring current levels in a given path
nyengele 3:9cae7baf7ccd 3 *
nyengele 3:9cae7baf7ccd 4 * Can be used as a current detector
nyengele 3:9cae7baf7ccd 5 *
nyengele 3:9cae7baf7ccd 6 * Example:
nyengele 3:9cae7baf7ccd 7 * @code
nyengele 3:9cae7baf7ccd 8 * // Periodically read current levels in a circuit and
nyengele 3:9cae7baf7ccd 9 * // send output to PC terminal
nyengele 3:9cae7baf7ccd 10 *
nyengele 3:9cae7baf7ccd 11 * #include "mbed.h"
nyengele 3:9cae7baf7ccd 12 * #include "ACS712.h"
nyengele 3:9cae7baf7ccd 13 *
nyengele 3:9cae7baf7ccd 14 * // Connect the sensor analog output pin to mbed's AnalogIn pin
nyengele 3:9cae7baf7ccd 15 * ACS712 dev(p18);
nyengele 3:9cae7baf7ccd 16 * // Connect mbed to pc's USB port
nyengele 3:9cae7baf7ccd 17 * Serial pc(USBTX, USBRX);
nyengele 3:9cae7baf7ccd 18 *
nyengele 3:9cae7baf7ccd 19 * int main() {
nyengele 3:9cae7baf7ccd 20 * pc.printf("Sensor Log: \n\n\r");
nyengele 3:9cae7baf7ccd 21 * while (1) {
nyengele 3:9cae7baf7ccd 22 * // Read current from sensor and output to pc terminal
nyengele 3:9cae7baf7ccd 23 * pc.printf("Sensor Value: %2.2f A\n\r", dev);
nyengele 3:9cae7baf7ccd 24 * wait(0.200);
nyengele 3:9cae7baf7ccd 25 * }
nyengele 3:9cae7baf7ccd 26 * }
nyengele 3:9cae7baf7ccd 27 * @endcode
nyengele 3:9cae7baf7ccd 28 */
nyengele 3:9cae7baf7ccd 29
mikeb 0:a35011a2fdaa 30 class ACS712 {
mikeb 0:a35011a2fdaa 31
mikeb 0:a35011a2fdaa 32 public:
nyengele 3:9cae7baf7ccd 33 /** Create a hall-effect sensor of the specified type
nyengele 3:9cae7baf7ccd 34 *
nyengele 3:9cae7baf7ccd 35 * @param _pin mbed AnalogIn pin where the analog output of sensor is connected
nyengele 3:9cae7baf7ccd 36 * @param voltDivRatio resistor voltage division ratio at output of the sensor
nyengele 3:9cae7baf7ccd 37 * @param type type of ACS712 sensor used
nyengele 3:9cae7baf7ccd 38 *
nyengele 3:9cae7baf7ccd 39 * @note Supported types of sensors:
nyengele 3:9cae7baf7ccd 40 */
mikeb 0:a35011a2fdaa 41 ACS712(PinName _pin, float voltDivRatio = 1, short type = 5);
mikeb 0:a35011a2fdaa 42
nyengele 3:9cae7baf7ccd 43 /** Read the value of the measured current in amps
nyengele 3:9cae7baf7ccd 44 *
nyengele 3:9cae7baf7ccd 45 * @return current value in amps
nyengele 3:9cae7baf7ccd 46 */
mikeb 0:a35011a2fdaa 47 float read();
mikeb 0:a35011a2fdaa 48 float operator=(ACS712&);
nyengele 3:9cae7baf7ccd 49
nyengele 3:9cae7baf7ccd 50 /** Read the value of the measured current in amps
nyengele 3:9cae7baf7ccd 51 * Allows the ACS712 object to be used in a float context
nyengele 3:9cae7baf7ccd 52 *
nyengele 3:9cae7baf7ccd 53 * @return current value in amps
nyengele 3:9cae7baf7ccd 54 */
nyengele 1:4f9effb20c29 55 operator float() { return read(); }
mikeb 0:a35011a2fdaa 56
mikeb 0:a35011a2fdaa 57 private:
mikeb 0:a35011a2fdaa 58 AnalogIn sensor;
mikeb 0:a35011a2fdaa 59 float translate(float);
mikeb 0:a35011a2fdaa 60 float ratio;
mikeb 0:a35011a2fdaa 61 short type;
mikeb 0:a35011a2fdaa 62
mikeb 0:a35011a2fdaa 63 };
mikeb 0:a35011a2fdaa 64
mikeb 0:a35011a2fdaa 65 ACS712::ACS712(PinName _pin, float voltDivRatio, short type) : sensor(_pin){
mikeb 0:a35011a2fdaa 66 ratio = voltDivRatio;
nyengele 2:2b5233355986 67 this.type = type;
mikeb 0:a35011a2fdaa 68 }
mikeb 0:a35011a2fdaa 69
mikeb 0:a35011a2fdaa 70 float ACS712::translate(float val){
mikeb 0:a35011a2fdaa 71 switch(type){
mikeb 0:a35011a2fdaa 72 case 5:
mikeb 0:a35011a2fdaa 73 return (val*ratio - 2.5*ratio)/(.185*ratio);
mikeb 0:a35011a2fdaa 74 break;
mikeb 0:a35011a2fdaa 75 case 20:
mikeb 0:a35011a2fdaa 76 return (val*ratio - 2.5*ratio)/(.1*ratio);
mikeb 0:a35011a2fdaa 77 break;
mikeb 0:a35011a2fdaa 78 case 30:
mikeb 0:a35011a2fdaa 79 return (val*ratio - 2.5*ratio)/(.066*ratio);
mikeb 0:a35011a2fdaa 80 break;
mikeb 0:a35011a2fdaa 81 default:
mikeb 0:a35011a2fdaa 82 return 999;
mikeb 0:a35011a2fdaa 83 break;
mikeb 0:a35011a2fdaa 84
mikeb 0:a35011a2fdaa 85 }
mikeb 0:a35011a2fdaa 86 }
mikeb 0:a35011a2fdaa 87
mikeb 0:a35011a2fdaa 88
mikeb 0:a35011a2fdaa 89 float ACS712::read(){
nyengele 1:4f9effb20c29 90 return ACS712::translate(sensor * 3.3);
mikeb 0:a35011a2fdaa 91 }
mikeb 0:a35011a2fdaa 92
nyengele 1:4f9effb20c29 93 ACS712& ACS712::operator=(ACS712& rhs){
nyengele 1:4f9effb20c29 94 sensor = rhs.sensor;
nyengele 1:4f9effb20c29 95 ratio = rhs.ratio;
nyengele 1:4f9effb20c29 96 type = rhs.type;
nyengele 1:4f9effb20c29 97 return this;
mikeb 0:a35011a2fdaa 98 }