Happy Birthday project 1st prototype code
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 = 20;
int relay1 = 8; // select the realy pin for mp3 player
int relay2 = 9;
int relay3 = 10;
int firstCounter = 0;
int secondCounter = 0;
int thirdCounter =0;
void setup() {
pinMode(firstLayerLEDs, OUTPUT);
pinMode(secondLayerLEDs, OUTPUT);
pinMode(candleSwitch, INPUT);
pinMode(candleLEDs, OUTPUT);
pinMode(resetSwitch, INPUT);
pinMode(relay1, OUTPUT); // declare the relay Pin as an OUTPUT
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
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.println("micCounter = ");
//Serial.println(micCounter);
}
//}
//micPreviousState = micCurrentState;
if (switchState == 0) {
thirdCounter = 0;
secondCounter =0;
firstCounter= firstCounter + 1;
if (firstCounter == 1){
digitalWrite(relay1,HIGH);
delay (500);
digitalWrite(relay1, LOW);
//delay (500);
//digitalWrite(relay3,HIGH);
//delay (500);
//digitalWrite(relay3, LOW);
//delay (3000);
Serial.println("relay2 & relay3, each was pressed once");
}
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) {
firstCounter =0;
thirdCounter =0;
secondCounter= secondCounter + 1;
if (secondCounter == 1){
digitalWrite(relay1,HIGH);
delay (500);
digitalWrite(relay1, LOW);
//delay (3000);
Serial.println("relay2 was pressed onece");
}
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) {
firstCounter = 0;
secondCounter = 0;
thirdCounter= thirdCounter + 1;
if (thirdCounter == 1){
digitalWrite(relay1,HIGH);
delay (500);
digitalWrite(relay1, LOW);
//delay (3000);
Serial.println("relay1 was pressed once");
}
digitalWrite(firstLayerLEDs, LOW);
digitalWrite(secondLayerLEDs, LOW);
digitalWrite(candleLEDs, LOW);
delay(100);
}
//if (resetVal == 0) {
//if (resetVal == 0 || candleVal == LOW) {
if (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