Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  *      EduIntro Library v0.1
  2.  *
  3.  *
  4.  *
  5.  *      created on Aug 2018
  6.  *      by D. Cuartielles
  7.  *      based on work from Dec 2011 by F. Vanzati
  8.  *
  9.  *      This program is free software; you can redistribute it and/or modify
  10.  *      it under the terms of the GNU General Public License as published by
  11.  *      the Free Software Foundation; either version 2 of the License, or
  12.  *      (at your option) any later version.
  13.  *
  14.  *      This program is distributed in the hope that it will be useful,
  15.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  *      GNU General Public License for more details.
  18.  *
  19.  *      You should have received a copy of the GNU General Public License
  20.  *      along with this program; if not, write to the Free Software
  21.  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22.  *      MA 02110-1301, USA.
  23.  */
  24.  
  25.  #include "Arduino.h"
  26.  #include "Servo.h"
  27.  #include <pitches.h>
  28.  
  29. #ifndef EduIntro_h
  30. #define EduIntro_h
  31.  
  32.  
  33. // Minimum Analog In/Out that each platform have
  34. #define D13 13
  35. #define D12 12
  36. #define D11 11
  37. #define D10 10
  38. #define D9 9
  39. #define D8 8
  40. #define D7 7
  41. #define D6 6
  42. #define D5 5
  43. #define D4 4
  44. #define D3 3
  45. #define D2 2
  46. #define D1 1
  47. #define D0 0
  48.  
  49. #define ANALOG_MAX 1023
  50.  
  51.  
  52. /*
  53. -----------------------------------------------------------------------------
  54.                                 Generic Classes
  55. -----------------------------------------------------------------------------
  56. */
  57.  
  58. class DigitalInput
  59. {
  60. public:
  61.     DigitalInput(uint8_t _pin);
  62.     DigitalInput(uint8_t _pin, uint8_t _mode);
  63.     boolean read();
  64.  
  65. protected:
  66.     uint8_t pin;
  67. };
  68.  
  69.  
  70. class AnalogInput
  71. {
  72. public:
  73.     AnalogInput(uint8_t _pin);
  74.     int read();
  75.     boolean increasing();
  76.     boolean decreasing();
  77.  
  78. protected:
  79.     uint8_t pin;
  80.     int _oldVal;
  81.     boolean _increasing;
  82.     boolean _decreasing;
  83. };
  84.  
  85. class AnalogInput2
  86. {
  87. public:
  88.     AnalogInput2(uint8_t _pinX, uint8_t _pinY);
  89.     AnalogInput2(uint8_t _pinX, uint8_t _pinY, uint8_t _pinZ);
  90.     int readX();
  91.     int readY();
  92.     int readZ();
  93.  
  94. protected:
  95.     uint8_t pinX, pinY, pinZ;
  96. };
  97.  
  98.  
  99. class Output
  100. {
  101.     public:
  102.     Output (uint8_t _pin);
  103.     void write(int value);
  104.     inline int state() { return _state; }
  105.     void on();
  106.     void off();
  107.     void blink(int delay);
  108.     void blink(int delay1, int delay2);
  109.  
  110. protected:
  111.     uint8_t pin;
  112.     int _state;
  113.     boolean isPWM();
  114. };
  115.  
  116. /*
  117.  -----------------------------------------------------------------------------
  118.                                 Digital Inputs
  119.  -----------------------------------------------------------------------------
  120. */
  121.  
  122.  
  123. /*      Button      */
  124.  
  125. class Button: public DigitalInput
  126. {
  127.     public:
  128.         Button(uint8_t _pin);
  129.         boolean readSwitch();
  130.         boolean pressed();
  131.         boolean held();
  132.         boolean released();
  133.  
  134.     protected:
  135.         boolean _toggleState, _oldState;
  136.         boolean _pressedState, _releasedState;
  137.         boolean _heldState;
  138.         int _heldTime;
  139.         int _millisMark;
  140.  
  141.         void update();
  142. };
  143.  
  144. /*      PIR      */
  145.  
  146. class PIR: public DigitalInput
  147. {
  148.     public:
  149.     PIR(uint8_t _pin);
  150.     PIR(uint8_t _pin, uint8_t _mode);
  151.         boolean hadActivity();   // was it active since last reset?
  152.     boolean resetActivity(); // reset activity variable
  153.     boolean readSwitch();
  154.     boolean activated();
  155.         boolean active();
  156.         boolean deactivated();
  157.  
  158.     protected:
  159.         boolean _toggleState, _oldState;
  160.         boolean _pressedState, _releasedState;
  161.         boolean _heldState, _activityState;
  162.         int _heldTime;
  163.         int _millisMark;
  164.         int _mode;
  165.  
  166.         void update();
  167. };
  168.  
  169. /*      DHT11      */
  170.  
  171. class DHT11 : public DigitalInput
  172. {
  173. public:
  174.     //DHT11(uint8_t _pin): humidity(-1), temperatureC(-1), pin(_pin);
  175.     DHT11(uint8_t _pin);
  176.     // An enumeration modeling the read status of the sensor.
  177.     enum ReadStatus {
  178.         OK,
  179.         ERROR_CHECKSUM,
  180.         ERROR_TIMEOUT,
  181.       };
  182.     ReadStatus update();  // read the data and return status
  183.     inline int readCelsius() const {
  184.         return this->temperatureC;
  185.       }
  186.     inline float readFahrenheit() const {
  187.         return this->temperatureF;
  188.       }
  189.     inline int readHumidity() const {
  190.         return this->humidity;
  191.       }
  192.  
  193. private:
  194.     int pin;
  195.     int humidity;
  196.     int temperatureC;
  197.     float temperatureF;
  198.     enum {
  199.       MAX_PIN_CHANGE_ITERATIONS = 10000, // timeout variable
  200.     };
  201.     inline ReadStatus waitForPinChange(const int oldValue,
  202.                                        unsigned  maxIterations =
  203.                                               MAX_PIN_CHANGE_ITERATIONS) const {
  204.         while ((--maxIterations > 0) && (digitalRead(this->pin) == oldValue)) {
  205.             // Just keep looping...
  206.         }
  207.  
  208.         return (maxIterations > 0) ? OK : ERROR_TIMEOUT;
  209.       }
  210. };
  211.  
  212. /*
  213.  -----------------------------------------------------------------------------
  214.                                 Analog Inputs
  215.  -----------------------------------------------------------------------------
  216.  */
  217.  
  218. /*      Potentiometer        */
  219.  
  220. class Potentiometer: public AnalogInput
  221. {
  222. public:
  223.     Potentiometer(uint8_t pin);
  224.     int read();
  225.     int readStep(int steps);
  226.  
  227. protected:
  228.     int _minVal, _maxVal;
  229.     int _mappedVal;
  230.     int _steps;
  231. };
  232.  
  233.  
  234. /*      Light Sensor        */
  235.  
  236. class LightSensor : public AnalogInput
  237. {
  238.     public:
  239.         LightSensor(uint8_t _pin);
  240. };
  241.  
  242. /*      Temperature Sensor        */
  243.  
  244. class Thermistor : public AnalogInput
  245. {
  246. public:
  247.     Thermistor(uint8_t _pin);
  248.     float readCelsius();
  249.     float readFahrenheit();
  250.  
  251. protected:
  252.     constexpr static float ADCres = 1023.0;
  253.     constexpr static int Beta = 3950;           // Beta parameter
  254.     constexpr static float Kelvin = 273.15; // 0°C = 273.15 K
  255.     constexpr static int Rb = 10000;            // 10 kOhm
  256.     constexpr static float Ginf = 120.6685; // Ginf = 1/Rinf
  257.     // Rinf = R0*e^(-Beta/T0) = 4700*e^(-3950/298.15)
  258. };
  259.  
  260. class LM35 : public AnalogInput
  261. {
  262. public:
  263.     LM35(uint8_t _pin);
  264.     float readCelsius();
  265.     float readFahrenheit();
  266.  
  267. protected:
  268.     constexpr static float ADCres = 1023.0;
  269.     constexpr static float Acc = 0.01;          // Accuracy mV/C
  270. };
  271.  
  272. /*
  273.  -----------------------------------------------------------------------------
  274.                                     Outputs
  275.  -----------------------------------------------------------------------------
  276.  */
  277.  
  278. /*      LED     */
  279.  
  280. class Led : public Output
  281. {
  282.     public:
  283.         Led(uint8_t _pin);
  284.         inline void brightness(int value) { write(value); }
  285. };
  286.  
  287. /*      Piezo     */
  288.  
  289. class Piezo : public Output
  290. {
  291.     public:
  292.         Piezo(uint8_t _pin);
  293.         void beep(int _tone);
  294.         void beep(int _tone, int _duration);
  295.         void play(int melody[]);
  296.         void play(int n, int melody[]);
  297.   protected:
  298.         int getMelodySize(int melody[]);
  299. };
  300.  
  301. /*      MosFet      */
  302.  
  303. class MosFet : public Output
  304. {
  305.     public:
  306.         MosFet(uint8_t _pin);
  307. };
  308.  
  309. /*      Relay       */
  310.  
  311. class Relay : public Output
  312. {
  313.     public:
  314.         Relay(uint8_t _pin);
  315. };
  316.  
  317. /*      Servo       */
  318.  
  319. class ServoMotor : public Servo
  320. {
  321.     public:
  322.         ServoMotor(uint8_t _pin);
  323.     int write(uint8_t _value);
  324.   protected:
  325.     Servo _servo;
  326. };
  327.  
  328. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement