|
| 1 | +#include <ACS712.h> |
| 2 | +#include <Stepper.h> |
| 3 | +#include <Adafruit_SSD1306.h> |
| 4 | +#include <Adafruit_GFX.h> |
| 5 | + |
| 6 | +// OLED display. |
| 7 | +#define OLED_ADDR 0x3C |
| 8 | +Adafruit_SSD1306 display(-1); |
| 9 | + |
| 10 | +// Motor. |
| 11 | +const int steps = 32; // the number of motor steps. |
| 12 | +const int rotate_360 = 2048; // 32*64 - because of gear. |
| 13 | +Stepper motor(steps, 6, 8, 7, 9); // pay attention to pins order. Current order is proper. |
| 14 | + |
| 15 | +// HALL effect sensor. |
| 16 | +// SparFun has 5.0 volt with a max value of 1023 steps. |
| 17 | +// ACS712 5A uses 185 mV per A. |
| 18 | +// ACS712 20A uses 100 mV per A. <<----------- |
| 19 | +// ACS712 30A uses 66 mV per A. |
| 20 | +ACS712 acs(A0, 5.0, 1023, 100); |
| 21 | + |
| 22 | + |
| 23 | +void setup() { |
| 24 | + // Init rotor. |
| 25 | + motor.setSpeed(800); |
| 26 | + |
| 27 | + // Init HALL sensor. |
| 28 | + //pinMode(A0, INPUT); |
| 29 | + acs.autoMidPoint(); |
| 30 | + |
| 31 | + // Init and clear OLED display. |
| 32 | + display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR); |
| 33 | + display.clearDisplay(); |
| 34 | + display.display(); |
| 35 | + display.setTextColor(WHITE); |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +bool is_stove_running = false; |
| 40 | +bool is_throttle_opened = false; |
| 41 | +int mA; // the value taken from Hall sensor (in miliampers). |
| 42 | + |
| 43 | +void loop() { |
| 44 | + // Read current. |
| 45 | + mA = acs.mA_AC(); |
| 46 | + display_hall(mA, is_throttle_opened); |
| 47 | + //Serial.println(mA); |
| 48 | + |
| 49 | + // Stove is running. |
| 50 | + is_stove_running = (mA > 200) ? true : false; |
| 51 | + |
| 52 | + // Run motor if needed. |
| 53 | + if (is_stove_running && !is_throttle_opened) { |
| 54 | + display_throttle_state("opening..."); |
| 55 | + open_throttle(motor); |
| 56 | + is_throttle_opened = true; |
| 57 | + } |
| 58 | + else if (!is_stove_running && is_throttle_opened) { |
| 59 | + display_throttle_state("closing..."); |
| 60 | + close_throttle(motor); |
| 61 | + is_throttle_opened = false; |
| 62 | + } |
| 63 | + |
| 64 | + delay(500); |
| 65 | +} |
| 66 | + |
| 67 | +void open_throttle (Stepper motor) { |
| 68 | + motor.step(rotate_360/4); // 512 == turn 90 deg. |
| 69 | +} |
| 70 | + |
| 71 | +void close_throttle (Stepper motor) { |
| 72 | + motor.step(-rotate_360/4); // 512 == turn 90 deg. |
| 73 | +} |
| 74 | + |
| 75 | +// Views definitions. |
| 76 | +void display_hall(int hall, bool is_throttle_opened) { |
| 77 | + display.clearDisplay(); |
| 78 | + display.setTextSize(1); |
| 79 | + display.setCursor(0, 0); |
| 80 | + display.print("Hall [mA]:\n"); |
| 81 | + display.setTextSize(2); |
| 82 | + display.print(hall); |
| 83 | + display.print("\n"); |
| 84 | + display.setTextSize(1); |
| 85 | + display.print("Throttle: "); |
| 86 | + if (is_throttle_opened) { |
| 87 | + display.print("opened"); |
| 88 | + } else { |
| 89 | + display.print("closed"); |
| 90 | + } |
| 91 | + display.display(); |
| 92 | +} |
| 93 | + |
| 94 | +void display_throttle_state(String msg) { |
| 95 | + display.clearDisplay(); |
| 96 | + display.setTextSize(1); |
| 97 | + display.setCursor(0, 0); |
| 98 | + display.print("Throttle:\n"); |
| 99 | + display.setTextSize(2); |
| 100 | + display.print(msg); |
| 101 | + display.display(); |
| 102 | +} |
| 103 | + |
0 commit comments