Stap 2: De Code...
#define orangePin 11
#define redPin 9#define whitePin 10#define SPKpin 3
int pinChange;volatile int BadDreamsCount, DCounter;
void setup() { pinMode (orangePin, OUTPUT); pinMode (redPin, OUTPUT); pinMode (whitePin, OUTPUT); pinMode (SPKpin, OUTPUT); pinMode (2, INPUT_PULLUP); Serial.begin (9600); randomSeed(millis()); BadDreamsCount = random (1, 5);}
void loop() { randomSeed(millis()); dreamsScan(); if (random (1,100) <= 10 && DCounter <= BadDreamsCount) { CND(); DCounter ++; Serial.print ("BadDreamsCount = "); Serial.print (BadDreamsCount); Serial.print (" DCounter = "); Serial.println (DCounter); } zero(); noTone (SPKpin);}
// Catch and Destroy animation procedurevoid CND() { randomSeed(millis()); // Closing on the dream for (int a=200; a>=30; a=a-10) { Serial.println (a); digitalWrite (orangePin, HIGH); digitalWrite (redPin, LOW); tone (SPKpin, 500-a); delay (a); digitalWrite (orangePin, LOW); digitalWrite (redPin, HIGH); delay (a); //noTone(SPKpin); } // Holding it tone (SPKpin, 300, 4000); for (int a=1; a<=50; a++) { digitalWrite (orangePin, HIGH); digitalWrite (redPin, LOW); delay (30); digitalWrite (orangePin, LOW); digitalWrite (redPin, HIGH); delay (30); //noTone (SPKpin); } // ZAPPING it for (int a=1; a<= random (10, 50); a++) { analogWrite (whitePin, random (10, 255)); tone (SPKpin, random (20,300)); delay (30); } noTone (SPKpin); zero ();}
// Turning all lights offvoid zero() { digitalWrite (orangePin, LOW); digitalWrite (redPin, LOW); digitalWrite (whitePin, LOW);}
// Scan for deam animation procedurevoid dreamsScan() { tone (SPKpin, 2000, 100); for (int i=9; i<=11; i++){ /* digitalWrite (i, !digitalRead(i)); digitalWrite (i, !digitalRead(i)); digitalWrite (i, !digitalRead(i)); Serial.println (i); delay (200); */ digitalWrite (orangePin, HIGH); digitalWrite (redPin, LOW); delay (400); digitalWrite (orangePin, LOW); digitalWrite (redPin, HIGH); delay (400); }}