Guest User

Untitled

a guest
Jun 5th, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.23 KB | None | 0 0
  1. #include <driver/rtc_io.h>
  2. #include <esp_sleep.h>
  3. #include "driver/ledc.h"
  4.  
  5. //==========================================
  6. // SETTINGS
  7.  
  8. // SETUP YOUR PINS
  9.  
  10. // Define the GPIO pin for the mosfets switching the motor
  11. const int mosfetPin = 2;
  12.  
  13. // Define the Analog pin for the voltage sensor
  14. const int voltagePin = 34;
  15.  
  16. // Define the voltage threshold where the device will save a bit of power by vibrating less.
  17. // For when you have long cloudy autumns and the solar panel is not making enough power.
  18. const float voltageThreshold = 11.8;
  19.  
  20. // Short buzz length
  21. const int shortBuzz = 1000; //ms
  22.  
  23. // Long buzz length
  24. const int longBuzz = 3000; //ms
  25.  
  26. // Define the voltage divider resistor values (R1 and R2 in ohms)
  27. // If you have used just two resistors you already know.
  28. // If you purchased a sensor module chances are the below is correct
  29. const float R1 = 300000.0;  // 300k ohms
  30. const float R2 = 7500.0;  // 7.5k ohms
  31.  
  32. // Boolean to enable/disable serial debug
  33. bool enableDebug = true;
  34.  
  35. // Test mode - in testmode the ESP will not go into deep sleep so you can read the serial messages
  36. bool testMode = true;
  37.  
  38. // Motor max speed - Define the max speed of the motor on the range of 0-8191
  39. // 8191 is 100% duty cycle for 13-bit resolution
  40. // With some motors slower vibration is more effective as high speed makes it smoother
  41.  
  42. const int maxSpeed = 6000;
  43.  
  44. // END OF SETTINGS
  45. //=========================================
  46.  
  47. // Motor PWM settings, ideally don't change it
  48. #define LEDC_TIMER              LEDC_TIMER_0
  49. #define LEDC_MODE               LEDC_LOW_SPEED_MODE
  50. #define LEDC_OUTPUT_IO          mosfetPin // Define the output GPIO
  51. #define LEDC_CHANNEL            LEDC_CHANNEL_0
  52. #define LEDC_DUTY_RES           LEDC_TIMER_13_BIT // Set duty resolution to 13 bits
  53. #define LEDC_DUTY               maxSpeed // Set duty cycle
  54. #define LEDC_FREQUENCY          (4000) // Frequency in Hertz. Set frequency at 4 kHz
  55.  
  56. void setup() {
  57.   if (enableDebug) {
  58.     Serial.begin(9600);  // Initialize serial communication for debugging
  59.   }
  60.  
  61.   // Initialize the MOSFET pin
  62.   pinMode(mosfetPin, OUTPUT);
  63.   digitalWrite(mosfetPin, LOW);  // Ensure the MOSFET is off initially
  64.  
  65.   // Initialize the voltage pin
  66.   pinMode(voltagePin, INPUT);
  67.  
  68.   // Seed the random number generator with a unique value
  69.   randomSeed(esp_random());
  70. }
  71.  
  72. // LEDC config to create PWM
  73. void ledc_init() {
  74.     ledc_timer_config_t ledc_timer = {
  75.         .speed_mode       = LEDC_MODE,
  76.         .timer_num        = LEDC_TIMER,
  77.         .duty_resolution  = LEDC_DUTY_RES,
  78.         .freq_hz          = LEDC_FREQUENCY,  // Set output frequency at 4 kHz
  79.         .clk_cfg          = LEDC_AUTO_CLK
  80.     };
  81.     ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
  82.  
  83.     // Prepare and then apply the LEDC PWM channel configuration
  84.     ledc_channel_config_t ledc_channel = {
  85.         .speed_mode     = LEDC_MODE,
  86.         .channel        = LEDC_CHANNEL,
  87.         .timer_sel      = LEDC_TIMER,
  88.         .intr_type      = LEDC_INTR_DISABLE,
  89.         .gpio_num       = LEDC_OUTPUT_IO,
  90.         .duty           = 0, // Set duty to 0%
  91.         .hpoint         = 0
  92.     };
  93.     ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
  94. }
  95.  
  96. float readVoltage() {
  97.   int analogValue = analogRead(voltagePin);
  98.   float voltage = analogValue * (3.3 / 4095.0);  // Convert analog reading to voltage
  99.   float inputVoltage = voltage * ((R1 + R2) / R2);  // Calculate the input voltage
  100.   return inputVoltage;
  101. }
  102.  
  103. void setMosfet(bool state) {
  104.   ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, state ? LEDC_DUTY : 0));
  105.   ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  106. }
  107.  
  108. // Generate a random time between 30 and 60 seconds
  109. uint32_t sleepTime = 30 + esp_random() % 31;  // Random time between 30 and 60 seconds
  110.  
  111. void goToSleep() {
  112.     if (enableDebug) {
  113.     Serial.println("Going to sleep for: ");
  114.     Serial.print(sleepTime);
  115.     Serial.print(" seconds.");
  116.   }
  117.     if (testMode == true) {
  118.       // In sleep mode we will just wait for the timeout
  119.       delay(sleepTime * 1000);
  120.     } else {
  121.       // Convert sleep time to microseconds and set up deep sleep
  122.       esp_sleep_enable_timer_wakeup(sleepTime * 1000000);
  123.       esp_deep_sleep_start();
  124.     }
  125. }
  126.  
  127. void loop() {
  128.   float voltage = readVoltage();
  129.   if (enableDebug) {
  130.   Serial.println("Voltage: ");
  131.   Serial.print(voltage);
  132.   Serial.print("V");
  133.   }
  134.  
  135.   ledc_init();
  136.  
  137.   if (voltage < voltageThreshold) {
  138.     for (int i = 0; i < 2; i++) {
  139.       if (enableDebug) {
  140.         Serial.println("Voltage low - Short buzz");
  141.       }
  142.       setMosfet(true);
  143.       delay(shortBuzz);
  144.       setMosfet(false);
  145.       delay(shortBuzz);
  146.     }
  147.     // Ensure MOSFET pin stays in the correct state during sleep
  148.     setMosfet(false);
  149.     // Initiate deep sleep
  150.     goToSleep();
  151.  
  152.   } else {
  153.     // Short buzzes
  154.     for (int i = 0; i < 3; i++) {
  155.       if (enableDebug) {
  156.         Serial.println("Voltage OK - Normal buzz sequence.");
  157.       }
  158.       setMosfet(true);
  159.       delay(shortBuzz);
  160.       setMosfet(false);
  161.       delay(shortBuzz);
  162.     }
  163.  
  164.     setMosfet(true);
  165.     delay(longBuzz);
  166.     setMosfet(false);
  167.  
  168.     // Ensure MOSFET pin stays in the correct state during sleep
  169.     setMosfet(false);
  170.  
  171.     // Initiate deep sleep
  172.     goToSleep();
  173.   }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment