#include "pitches.h"
int Full_led = 2;
int half_led = 3;
int motor = 6;
int battery = A1;
int Charge = A2;
int headphone = 8;
int Switch = 7;
int value = 0;
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
void setup()
{
Serial.begin(115200);
pinMode(Full_led, OUTPUT);
pinMode(half_led, OUTPUT);
pinMode(motor, OUTPUT);
pinMode(battery, INPUT);
pinMode(Charge, OUTPUT);
pinMode(headphone, OUTPUT);
pinMode(Switch, INPUT);
digitalWrite(Full_led, HIGH);
digitalWrite(half_led, HIGH);
digitalWrite(motor, HIGH);
delay(250);
digitalWrite(Full_led, LOW);
digitalWrite(half_led, LOW);
digitalWrite(motor, LOW);
}
void loop()
{
value = analogRead(battery);
float vin = value * (5.0 / 1023.0);
Serial.println("Input Voltage = ");
Serial.println(vin);
if(vin<3.7)
{
digitalWrite(half_led, HIGH);
digitalWrite(Full_led, HIGH);
digitalWrite(motor, HIGH);
digitalWrite(Charge, HIGH);
delay(250);
digitalWrite(motor, LOW);
}
else
{
digitalWrite(half_led, LOW);
digitalWrite(Full_led, LOW);
digitalWrite(motor, LOW);
}
if(vin == 4.2)
{
digitalWrite(Full_led, HIGH);
digitalWrite(Charge, LOW);
}
else
{
digitalWrite(Full_led, LOW);
}
if(vin == 3.9)
{
digitalWrite(half_led, HIGH);
digitalWrite(motor, HIGH);
delay(250);
digitalWrite(motor, LOW);
}
else
{
digitalWrite(half_led, LOW);
digitalWrite(motor, LOW);
}
if(Switch == HIGH)
{
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
tone(headphone, melody[thisNote], noteDuration);
delay(pauseBetweenNotes);
noTone(headphone);
}
}
}