Happy Birthday project prototype code without mp3 function
int firstLayerLEDs = 3;
int secondLayerLEDs = 4;
int candleVal = 0;
int candleSwitch = 5;
int switchState = 0; // the state of all situation
int candleLEDs = 6;
int microPhone = 0;
int AmpVar = 0;
int micValue =0;
int micCounter = 0;
int micCurrentState = 0;
int micPreviousState = 0;
int resetVal = 0;
int resetSwitch = 7;
int micVal = 0;
int statePin = HIGH;
int THRESHOLD = 100;
void setup() {
pinMode(firstLayerLEDs, OUTPUT);
pinMode(secondLayerLEDs, OUTPUT);
pinMode(candleSwitch, INPUT);
pinMode(candleLEDs, OUTPUT);
pinMode(resetSwitch, INPUT);
Serial.begin(9600);
}
void loop() {
candleVal = digitalRead(candleSwitch);
micVal = analogRead(microPhone);
if (micVal>=512){
AmpVar = micVal-512;
}
else {
AmpVar = 512-micVal;
}
micValue = AmpVar; // read the analog input
Serial.println(AmpVar);
resetVal = digitalRead(resetSwitch);
if (micValue >= THRESHOLD) { // check if the input is HIGH (button released)
micCurrentState = 1;
}
else {
micCurrentState = 0;
}
//if(micCurrentState != micPreviousState){
if(micCurrentState == 1){
micCounter = micCounter + 1;
Serial.print("micCounter = ");
Serial.println(micCounter);
}
//}
//micPreviousState = micCurrentState;
if (switchState == 0) {
digitalWrite(firstLayerLEDs, HIGH);
digitalWrite(secondLayerLEDs, HIGH);
digitalWrite(candleLEDs, LOW);
delay(100);
}
//if (candleVal == HIGH && micValue < THRESHOLD) {
if (candleVal == HIGH && micCounter == 0) {
switchState = 1;
}
if (switchState == 1) {
digitalWrite(firstLayerLEDs, HIGH);
digitalWrite(secondLayerLEDs, HIGH);
digitalWrite(candleLEDs, HIGH);
delay(100);
digitalWrite(firstLayerLEDs, HIGH);
digitalWrite(secondLayerLEDs, HIGH);
digitalWrite(candleLEDs, HIGH);
delay(100);
}
//comment your code!
if (candleVal == HIGH && micCounter > 0) {
switchState = 2;
}
if (switchState == 2) {
digitalWrite(firstLayerLEDs, LOW);
digitalWrite(secondLayerLEDs, LOW);
digitalWrite(candleLEDs, LOW);
delay(100);
}
//if (resetVal == 0) {
if (resetVal == 0 || candleVal == LOW) {
switchState = 0;
micCounter = 0;
}
}
/*
when the cake is built all lights are on
when the candle is blown all lightw go off
*/


0 Comments:
Post a Comment
<< Home