#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//oled
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define MOS_PIN 3
// Create the Audio components
AudioPlaySdWav playWav;
AudioOutputI2S audioOutput;
AudioAmplifier amp1; // Audio amplifier object for volume control
AudioAmplifier amp2; // You might need two if you have stereo output
AudioConnection patchCord1(playWav, 0, amp1, 0);
AudioConnection patchCord2(amp1, 0, audioOutput, 0);
AudioConnection patchCord3(playWav, 1, amp2, 0);
AudioConnection patchCord4(amp2, 0, audioOutput, 1);
int butt;
unsigned long myTime;
const int batteryPin = A0;
int flag;
int counter;
unsigned long Vsummary;
// int R1 = 1;
// int R2 = 5;
// const float 3sMaxV = 12.6;
// const float 3sMinV = 11.1;
// const float MAX_VOLTAGE = 4.2; // Maximum battery voltage
// const float MIN_VOLTAGE = 3.0; // Minimum battery voltage
void setup() {
pinMode(MOS_PIN, OUTPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{ // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
display.display();
delay(2000); // Pause for 2 seconds
display.setTextSize(2); // Size of the text to be displayed
display.setTextColor(SSD1306_WHITE);
Serial.begin(9600);
pinMode(PIND6, INPUT_PULLUP);
AudioMemory(8); // allocate memory blocks for audio processing
if (!SD.begin(10)) { // SD card CS pin is 10
Serial.println("SD Card initialization failed!");
return;
}
// Set initial volume (0.1 = 10%)
amp1.gain(0.9);
amp2.gain(0.9);
playWav.play("0001.wav");
}
void loop() {
if (millis() - myTime > 100) {
myTime = millis();
if (counter <100) {
counter++;
Vsummary = Vsummary + analogRead(batteryPin);
//Serial.println(analogRead(batteryPin));
//Serial.println(Vsummary);
// Serial.println(counter);
}
if (counter == 100) {
counter = 0;
Serial.println(Vsummary);
Serial.println(Vsummary/100);
int percentage = map(Vsummary/100, 574, 656, 0, 100);
percentage = constrain(percentage, 0, 100); // Ensure percentage is between 0 and 100
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(4);
display.print(percentage);
display.print("%");
display.display();
Serial.println(percentage);
Serial.println("%");
Vsummary =0;
}
}
butt = digitalRead(PIND6);
if (butt == 0 && flag == 0) {
flag = 1;
playWav.stop();
Serial.println("button is pressed");
Serial.println(butt);
Serial.println(flag);
playWav.play("0002.wav");
digitalWrite(MOS_PIN, HIGH);
}
if (butt == 1 && flag == 1) {
flag = 0;
digitalWrite(MOS_PIN, LOW);
playWav.stop();
Serial.println("button is relesed");
Serial.println(butt);
Serial.println(flag);
playWav.play("0003.wav");
delay(400);
playWav.stop();
playWav.play("0001.wav");
}
}