Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Battery Charging Controller
- *
- * Uses an Arduino HERO XL to simulate controlling the charging and use of a battery array,
- * with a photoresistor simulating solar panels and variables to track battery levels.
- */
- /*
- * Concepts introduced in this lesson:
- * - Analog input pins
- * - Serial console
- * - Serial plotter
- * - Floating point numbers/variables
- * - map()
- * - += (compound addition)
- */
- #include "Arduino.h"
- // Pin definitions
- const uint8_t LIGHT = 22; // LED on pin 22
- const uint8_t LIGHT_BUTTON = 23; // Button (light switch) on pin 23
- const uint8_t CHARGING_RATE = A8; // Photoresistor input simulating battery charge rate
- // Constants for button and light states
- const uint8_t PRESSED = LOW;
- const uint8_t NOT_PRESSED = HIGH;
- const uint8_t ON = HIGH;
- const uint8_t OFF = LOW;
- // Battery charge limits
- const float LOW_BATTERY_LIMIT = 10;
- const float HIGH_BATTERY_LIMIT = 90;
- const float RESUME_CHARGING_AT = HIGH_BATTERY_LIMIT - 5.0;
- // Simulation constants
- const uint8_t SECONDS_TO_FULL = 15;
- const uint8_t LOOPS_PER_SECOND = 20;
- const int AVERAGE_CHARGE_LEVEL = 530;
- // Charge calculations
- const float PERCENTAGE_FROM_EMPTY_TO_FULL = HIGH_BATTERY_LIMIT - LOW_BATTERY_LIMIT;
- const float PERCENTAGE_PER_SECOND = PERCENTAGE_FROM_EMPTY_TO_FULL / SECONDS_TO_FULL;
- const float PERCENTAGE_PER_LOOP = PERCENTAGE_PER_SECOND / LOOPS_PER_SECOND;
- const float CHARGE_PER_LIGHT_UNIT = PERCENTAGE_PER_LOOP / AVERAGE_CHARGE_LEVEL;
- void setup() {
- Serial.begin(9600);
- while (!Serial) { ; }
- pinMode(LIGHT, OUTPUT);
- pinMode(LIGHT_BUTTON, INPUT_PULLUP);
- pinMode(CHARGING_RATE, INPUT);
- }
- // Global variables
- float battery_charge_percentage = LOW_BATTERY_LIMIT;
- bool light_on = false;
- bool previous_button_state = NOT_PRESSED;
- bool charging = true;
- void loop() {
- // Read charging rate
- int current_charging_rate = analogRead(CHARGING_RATE);
- float new_charge = current_charging_rate * CHARGE_PER_LIGHT_UNIT;
- // Update battery charge
- if (charging) {
- battery_charge_percentage += new_charge;
- }
- // Decrease battery if light is on
- if (light_on) {
- battery_charge_percentage -= CHARGE_PER_LIGHT_UNIT * AVERAGE_CHARGE_LEVEL * 0.8;
- }
- // Turn off light if battery is low
- if (light_on && battery_charge_percentage < LOW_BATTERY_LIMIT) {
- digitalWrite(LIGHT, OFF);
- light_on = false;
- }
- // Manage charging state
- if (battery_charge_percentage < RESUME_CHARGING_AT) {
- charging = true;
- }
- if (battery_charge_percentage > HIGH_BATTERY_LIMIT) {
- charging = false;
- }
- // Output to Serial Plotter
- Serial.print(0);
- Serial.print(", ");
- Serial.print(100);
- Serial.print(", ");
- Serial.print(battery_charge_percentage);
- Serial.print(", ");
- Serial.println(map(current_charging_rate, 0, 1023, 0, 100));
- // Button handling
- uint8_t button_state = digitalRead(LIGHT_BUTTON);
- if (button_state != previous_button_state) {
- if (button_state == PRESSED) {
- if (light_on) {
- digitalWrite(LIGHT, LOW);
- light_on = false;
- } else {
- digitalWrite(LIGHT, HIGH);
- light_on = true;
- }
- }
- previous_button_state = button_state;
- }
- // Delay for loop timing
- delay(1000 / LOOPS_PER_SECOND);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement