#define amplifierPin 7
#define bluetoothPin 8
int internalRef = 1.1; //1.095v
int sleepSetting = 0; // ss=0 -> Normal, ss=1 -> sleep timer setting mode, ss=2 -> sleep time has set and timer is on
int st=0; // timer sections number
int z = 0; // to take trial analog readings @ startup.
int bbn, bbd, bl; // integers defining LED on/off delays and number of blinks
int b = 0, c; // control brightness of led during sleeper mode on
int audioLevel; // value for vu meter
int i = 0, ln; // led number;
float R1 = 47000, R2 = 15000; // resistors to read battery voltage
float batVal, batVolti, batVolt, buttonReading, v1 = 100, v2 = 400, v3 = 650, v4 = 900;
float currentVal, voltageDrop, current, seriesResistance = 0.5, setValue = 30;
boolean currentStateSleepKey = LOW, currentStateMuteKey = LOW, currentStateBluetoothKey = LOW, currentStateStatusKey = LOW ;
boolean lastStateSleepKey = LOW, lastStateMuteKey = LOW, lastStateBluetoothKey = LOW, lastStateStatusKey = LOW;
boolean autoShut = false;
boolean amplifierStatus = LOW , bluetoothStatus = LOW, batStatus = LOW;
boolean maxb = false; // led variable to substitute for loop during sleep
boolean testModeActive = false;
boolean vuMeterActive = false;
boolean batLevelActive = false;
boolean serialSent = true;
unsigned long sleepTime, sleepTimeStart, currentTime, elapsedTime, totaltime ;
unsigned long startTimeSleepKey = 0, startTimeMuteKey = 0, startTimeBluetoothKey = 0, startTimeStatusKey = 0;
unsigned long autoShutStartTime, autoShutSetTime = 1200000; // 20 minutes (20*60*1000 millisec.)
unsigned long sleepInterval = 10000; // 30 minutes (30*60*1000 millisecond)
unsigned long sleepIntervalStart;
unsigned long sleepLEDDelay = 0;
unsigned long batLevelStart, serialSentStart;
const byte leds[] = {3, 5, 6, 9, 10, 2}; // the pins connecting to the LEDs in the order
const byte nbLeds = sizeof(leds) / sizeof(leds[0]);
void setup() {
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(amplifierPin, INPUT);
pinMode(bluetoothPin, INPUT);
for (i; i < 5; i++) {
pinMode(leds[i], OUTPUT);
digitalWrite(leds[i], LOW);
}
//Serial.begin(9600);
i = 0; //reset i to zero
pinMode(leds[5], INPUT); // keep red led as input bcz its connected to green led as well in reverse order
delay(10);
// To eliminate the first unstable readings of internalReference, take 10 trial readings.
batStatus = HIGH;
delay(50);
batVal = analogRead(A4);
batVolt = (batVal * 5) / 1023;
batStat();
}
void loop() {
batVal = analogRead(A4);
batVolt = (batVal * 5) / 1023;
pinMode(amplifierPin, OUTPUT);
digitalWrite (amplifierPin, amplifierStatus); //amplifier turns on
delay(20);
pinMode(bluetoothPin, OUTPUT);
digitalWrite (bluetoothPin, bluetoothStatus);
key();
sleep1();
batStat();
testMode();
}
void key() {
currentStateSleepKey = digitalRead(A0);
currentStateBluetoothKey = digitalRead(A1);
currentStateMuteKey = digitalRead(A2);
currentStateStatusKey = digitalRead(A3);
delay(50);//?? necessary??
//**********************************************************************************
if (currentStateSleepKey == HIGH && lastStateSleepKey == LOW) {
lastStateSleepKey = HIGH;
startTimeSleepKey = millis();
}
if (lastStateSleepKey == HIGH) {
if (currentStateSleepKey == LOW && millis() - startTimeSleepKey <= 400) {
if (sleepSetting == 1) {
st = st + 1;
if (st == 6) {
st = 1;
}
// Serial.print("st=");
// Serial.println(st);
}
startTimeSleepKey = 0;
delay(50);
}
else if (millis() - startTimeSleepKey > 2000) {
if (sleepSetting == 0) {
sleepSetting = 1;
}
else if (sleepSetting == 1) {
sleepSetting = 2;
}
else if (sleepSetting == 2) {
sleepSetting = 0;
for (i; i < 5; i++) {
digitalWrite(leds[i], LOW);
}
i = 0; //resetting i to 0.
}
for (i; i < 5; i++) {
digitalWrite(leds[i], HIGH); // LEDs turns on in order
}
delay(500);
for (i; i >= 0; i--) {
digitalWrite(leds[i], LOW);
}
//Serial.println(sleepSetting);
startTimeSleepKey = millis(); // necessary to set to millis. else the function would execute two times.
delay(200); // this delay is necessary , else the short press would get executed while releasing the switch.
}
}
lastStateSleepKey = currentStateSleepKey;
//*************************************************************************************
if (currentStateMuteKey == HIGH && lastStateMuteKey == LOW) {
lastStateMuteKey = HIGH;
startTimeMuteKey = millis();
}
if (lastStateMuteKey == HIGH) {
if (currentStateMuteKey == LOW && millis() - startTimeMuteKey <= 400) {
if (amplifierStatus == LOW) {
amplifierStatus = HIGH; // now muted.
}
else if (amplifierStatus == HIGH) {
amplifierStatus = LOW; // now unmuted.
}
startTimeMuteKey = 0;
delay(50);
}
else if (millis() - startTimeMuteKey >= 2000) {
// enter anything you want to do here
//Serial.println("long press of 2");
startTimeMuteKey = millis();
delay(200);
}
}
lastStateMuteKey = currentStateMuteKey;
//*********************************************************************************
if (currentStateBluetoothKey == HIGH && lastStateBluetoothKey == LOW) {
lastStateBluetoothKey = HIGH;
startTimeBluetoothKey = millis();
}
if (lastStateBluetoothKey == HIGH) {
if (currentStateBluetoothKey == LOW && millis() - startTimeBluetoothKey <= 400) {
if (bluetoothStatus == LOW) {
bluetoothStatus = HIGH;
}
else if (bluetoothStatus == HIGH) {
bluetoothStatus = LOW; // bluetooth turns on;
startTimeBluetoothKey = 0;
delay(50);
}
}
else if (millis() - startTimeBluetoothKey >= 5000) {
if (testModeActive == false) {
testModeActive = true;
Serial.begin(9600);
pinMode(leds[5], OUTPUT);
}
else if (testModeActive == true) {
testModeActive = false;
Serial.end();
//digitalWrite(leds[5],LOW);
pinMode(leds[5], INPUT);
}
startTimeBluetoothKey = millis();
delay(200);
}
}
lastStateBluetoothKey = currentStateBluetoothKey;
//*******************************************************************************
if (currentStateStatusKey == HIGH && lastStateStatusKey == LOW) {
lastStateStatusKey = HIGH;
startTimeStatusKey = millis();
}
if (lastStateStatusKey == HIGH) {
if (currentStateStatusKey == LOW && millis() - startTimeStatusKey <= 500) {
batStatus = HIGH;
startTimeStatusKey = 0;
delay(50);
}
else if (millis() - startTimeStatusKey >= 2000) {
if (vuMeterActive == false) {
vuMeterActive = true;
}
else if (vuMeterActive == true) {
vuMeterActive = false;
}
startTimeStatusKey = millis();
delay(200);
}
}
lastStateStatusKey = currentStateStatusKey;
}
//###########################################################################################################################
void sleep1() {
//if (sleepSetting==0){/*do nothing*/}
if (sleepSetting == 1) {
digitalWrite(leds[st-1], HIGH);
delay(50);
digitalWrite(leds[st-1], LOW);
//delay(500);
sleepIntervalStart = millis();
}
if (sleepSetting == 2) {
currentTime = millis();
// requiredTime = st * sleepInterval;
elapsedTime = millis() - sleepIntervalStart;
if (elapsedTime >= sleepInterval) {
st = st - 1;
sleepIntervalStart = millis(); // one interval section is over, next section starts.
}
if (currentTime - sleepLEDDelay >= 2500) {
/* if (maxb == false){
b= b+1;
analogWrite(leds[st - 1], b);
delay(2);
}
if (b == 255) {
maxb = true;
}
if ( maxb == true){
b = b-1;
analogWrite(leds[st - 1], b);
delay(2);
if ( b==0){
maxb = false;
}
}
*/
for (b; b < 256; b++) {
analogWrite(leds[st - 1], b);
delay(2);
}
for (b; b >= 0; b--) {
analogWrite(leds[st - 1], b);
delay(2);
}
//if (b == 0){
c= st-1;
for (c; c >=0; c--){
digitalWrite(leds[c],HIGH);
delay(50);
digitalWrite(leds[c],LOW);
delay(50);
}
//}
sleepLEDDelay = millis();
}
if (st == 0) {
bluetoothStatus = HIGH;
amplifierStatus = HIGH;
}
}
}
//#################################################################
void batStat() {
if (batStatus == HIGH&& batLevelActive == false){
if (batVolt > 3.99) {
ln = 5; //led number
}
for (i; i < ln; i++) {
digitalWrite(leds[i], HIGH); // LEDs turns on in order
delay(50);
}
batLevelStart = millis();
batLevelActive = true;
//delay(2000);
}
if (batStatus == HIGH && batLevelActive == true && millis()-batLevelStart >= 4000){
for (i; i >= 0; i--) {
digitalWrite(leds[i], LOW); // LEDs turns off in order
delay(50);
}
batLevelActive = false;
batStatus = LOW;
}
}
//###################################################################
void testMode(){
if (testModeActive == true){
if (serialSent == true){
serialSent = false;
serialSentStart = millis();
}
if (serialSent == false && millis()-serialSentStart >= 1000){
Serial.print("battery Volt=");
Serial.println(batVolt);
Serial.println("current");
Serial.println("***************************");
serialSent = true;
}
pinMode(leds[5],OUTPUT);
digitalWrite(leds[5],LOW);
delay(50);
pinMode(leds[5],INPUT);
//digitalWrite(leds[5],HIGH);
// delay(50);
delay(50);
}
}