Advertisement
inventrkits

Solar Simulation Shinanigans – (Power Grid Issues!)

May 29th, 2024 (edited)
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Battery Charging Controller
  3.  *
  4.  * Uses an Arduino HERO XL to simulate controlling the charging and use of a battery array,
  5.  * with a photoresistor simulating solar panels and variables to track battery levels.
  6.  */
  7.  
  8. /*
  9.  * Concepts introduced in this lesson:
  10.  * - Analog input pins
  11.  * - Serial console
  12.  * - Serial plotter
  13.  * - Floating point numbers/variables
  14.  * - map()
  15.  * - += (compound addition)
  16.  */
  17. #include "Arduino.h"
  18.  
  19. // Pin definitions
  20. const uint8_t LIGHT = 22;          // LED on pin 22
  21. const uint8_t LIGHT_BUTTON = 23;   // Button (light switch) on pin 23
  22. const uint8_t CHARGING_RATE = A8;  // Photoresistor input simulating battery charge rate
  23.  
  24. // Constants for button and light states
  25. const uint8_t PRESSED = LOW;
  26. const uint8_t NOT_PRESSED = HIGH;
  27. const uint8_t ON = HIGH;
  28. const uint8_t OFF = LOW;
  29.  
  30. // Battery charge limits
  31. const float LOW_BATTERY_LIMIT = 10;
  32. const float HIGH_BATTERY_LIMIT = 90;
  33. const float RESUME_CHARGING_AT = HIGH_BATTERY_LIMIT - 5.0;
  34.  
  35. // Simulation constants
  36. const uint8_t SECONDS_TO_FULL = 15;
  37. const uint8_t LOOPS_PER_SECOND = 20;
  38. const int AVERAGE_CHARGE_LEVEL = 530;
  39.  
  40. // Charge calculations
  41. const float PERCENTAGE_FROM_EMPTY_TO_FULL = HIGH_BATTERY_LIMIT - LOW_BATTERY_LIMIT;
  42. const float PERCENTAGE_PER_SECOND = PERCENTAGE_FROM_EMPTY_TO_FULL / SECONDS_TO_FULL;
  43. const float PERCENTAGE_PER_LOOP = PERCENTAGE_PER_SECOND / LOOPS_PER_SECOND;
  44. const float CHARGE_PER_LIGHT_UNIT = PERCENTAGE_PER_LOOP / AVERAGE_CHARGE_LEVEL;
  45.  
  46. void setup() {
  47.   Serial.begin(9600);
  48.   while (!Serial) { ; }
  49.   pinMode(LIGHT, OUTPUT);
  50.   pinMode(LIGHT_BUTTON, INPUT_PULLUP);
  51.   pinMode(CHARGING_RATE, INPUT);
  52. }
  53.  
  54. // Global variables
  55. float battery_charge_percentage = LOW_BATTERY_LIMIT;
  56. bool light_on = false;
  57. bool previous_button_state = NOT_PRESSED;
  58. bool charging = true;
  59.  
  60. void loop() {
  61.   // Read charging rate
  62.   int current_charging_rate = analogRead(CHARGING_RATE);
  63.   float new_charge = current_charging_rate * CHARGE_PER_LIGHT_UNIT;
  64.  
  65.   // Update battery charge
  66.   if (charging) {
  67.     battery_charge_percentage += new_charge;
  68.   }
  69.  
  70.   // Decrease battery if light is on
  71.   if (light_on) {
  72.     battery_charge_percentage -= CHARGE_PER_LIGHT_UNIT * AVERAGE_CHARGE_LEVEL * 0.8;
  73.   }
  74.  
  75.   // Turn off light if battery is low
  76.   if (light_on && battery_charge_percentage < LOW_BATTERY_LIMIT) {
  77.     digitalWrite(LIGHT, OFF);
  78.     light_on = false;
  79.   }
  80.  
  81.   // Manage charging state
  82.   if (battery_charge_percentage < RESUME_CHARGING_AT) {
  83.     charging = true;
  84.   }
  85.   if (battery_charge_percentage > HIGH_BATTERY_LIMIT) {
  86.     charging = false;
  87.   }
  88.  
  89.   // Output to Serial Plotter
  90.   Serial.print(0);
  91.   Serial.print(", ");
  92.   Serial.print(100);
  93.   Serial.print(", ");
  94.   Serial.print(battery_charge_percentage);
  95.   Serial.print(", ");
  96.   Serial.println(map(current_charging_rate, 0, 1023, 0, 100));
  97.  
  98.   // Button handling
  99.   uint8_t button_state = digitalRead(LIGHT_BUTTON);
  100.   if (button_state != previous_button_state) {
  101.     if (button_state == PRESSED) {
  102.       if (light_on) {
  103.         digitalWrite(LIGHT, LOW);
  104.         light_on = false;
  105.       } else {
  106.         digitalWrite(LIGHT, HIGH);
  107.         light_on = true;
  108.       }
  109.     }
  110.     previous_button_state = button_state;
  111.   }
  112.  
  113.   // Delay for loop timing
  114.   delay(1000 / LOOPS_PER_SECOND);
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement