Skip to content

Instantly share code, notes, and snippets.

@raphaelyancey
Last active May 10, 2017 10:26
Show Gist options
  • Save raphaelyancey/6af052d624eeb27ec204cdad2c27f6b3 to your computer and use it in GitHub Desktop.
Save raphaelyancey/6af052d624eeb27ec204cdad2c27f6b3 to your computer and use it in GitHub Desktop.
// Raphael Yancey <[email protected]>
// Adami CED 2016
const int NOTE_B2=123;const int NOTE_C3=131;const int NOTE_CS3=139;const int NOTE_D3=147;const int NOTE_DS3=156;const int NOTE_E3=165;const int NOTE_F3=175;const int NOTE_FS3=185;const int NOTE_G3=196;const int NOTE_GS3=208;const int NOTE_A3=220;const int NOTE_AS3=233;const int NOTE_B3=247;const int NOTE_C4=262;const int NOTE_CS4=277;const int NOTE_D4=294;const int NOTE_DS4=311;const int NOTE_E4=330;const int NOTE_F4=349;const int NOTE_FS4=370;const int NOTE_G4=392;const int NOTE_GS4=415;const int NOTE_A4=440;const int NOTE_AS4=466;const int NOTE_B4=494;const int NOTE_C5=523;const int NOTE_CS5=554;const int NOTE_D5=587;const int NOTE_DS5=622;const int NOTE_E5=659;const int NOTE_F5=698;const int NOTE_FS5=740;const int NOTE_G5=784;const int NOTE_GS5=831;const int NOTE_A5=880;const int NOTE_AS5=932;const int NOTE_B5=988;const int NOTE_C6=1047;const int NOTE_CS6=1109;const int NOTE_D6=1175;const int NOTE_DS6=1245;const int NOTE_E6=1319;const int NOTE_F6=1397;const int NOTE_FS6=1480;const int NOTE_G6=1568;const int NOTE_GS6=1661;const int NOTE_A6=1760;const int NOTE_AS6=1865;const int NOTE_B6=1976;const int NOTE_C7=2093;const int NOTE_CS7=2217;const int NOTE_D7=2349;const int NOTE_DS7=2489;const int NOTE_E7=2637;const int NOTE_F7=2794;const int NOTE_FS7=2960;const int NOTE_G7=3136;const int NOTE_GS7=3322;const int NOTE_A7=3520;const int NOTE_AS7=3729;const int NOTE_B7=3951;const int NOTE_C8=4186;const int NOTE_CS8=4435;const int NOTE_D8=4699;const int NOTE_DS8=4978;
const int distancePin = 7;
const int ledPin1 = 5;
const int ledPin2 = 6;
const int fabricPinAnalog = A0;
const int vibrationPin = 10;
const int motorPin = 3; // remplacer par 3
const int microphoneAnalog = A5;
const int fanPin = 11; // analog!
const int speakerPin = 12;
const int eyesPin = 2;
// Seuils de détection
int proximityThreshold = 100; // en cm
int volumeThreshold = 150; // sur 100
boolean isTouched = false;
boolean hasBeenHeard = false;
boolean isClose = false;
int lastReaction, amount;
void setup() {
Serial.begin(9600);
pinMode(fabricPinAnalog, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(motorPin, OUTPUT);
pinMode(fanPin, OUTPUT);
pinMode(speakerPin, OUTPUT);
pinMode(eyesPin, OUTPUT);
eyesOn(); speak();
}
void loop() {
// Générateur de son
//tone(9, 7000); delay(50); tone(9, 9000);
// Détecteur de son
//Serial.print("debug micro: "); Serial.println(analogRead(microphoneAnalog));
hasBeenHeard = (analogRead(microphoneAnalog) > volumeThreshold);
// Détecteur de toucher
//Serial.print("[Debug] Touched: "); Serial.println(analogRead(fabricPinAnalog));
//isTouched = (analogRead(fabricPinAnalog) > 1020);
// Détecteur de distance
// ---------------------
long duration, cm;
pinMode(distancePin, OUTPUT);
digitalWrite(distancePin, LOW);
delayMicroseconds(2);
digitalWrite(distancePin, HIGH);
delayMicroseconds(5);
digitalWrite(distancePin, LOW);
pinMode(distancePin, INPUT);
duration = pulseIn(distancePin, HIGH);
cm = duration / 29 / 2;
//Serial.print("debug: "); Serial.print("cm -> "); Serial.println(cm);
isClose = (cm <= proximityThreshold);
// ---------------------
//if(isTouched) { Serial.println("[Trigger] Touched"); isTouched = false; doSomething(); }
if(isClose) { Serial.println("[Trigger] Proximity"); isClose = false; doSomething(); }
else if(hasBeenHeard) { Serial.println("[Trigger] Loudness"); hasBeenHeard = false; doSomething(); }
}
void motorOn(boolean low = false) {
if(low) amount = 500; else amount = 1023;
Serial.print("low ");
Serial.println("motorOn()");
analogWrite(motorPin, amount);
}
void motorOff() {
Serial.println("motorOff()");
analogWrite(motorPin, 0);
}
void leds1On() {
Serial.println("leds1On()");
digitalWrite(ledPin1, HIGH);
}
void leds1Off() {
Serial.println("ledsOff()");
digitalWrite(ledPin1, LOW);
}
void leds2On() {
Serial.println("leds1On()");
digitalWrite(ledPin2, HIGH);
}
void leds2Off() {
Serial.println("ledsOff()");
digitalWrite(ledPin2, LOW);
}
void fanOn() {
Serial.println("fanOn()");
analogWrite(fanPin, 1023);
}
void fanOff() {
Serial.println("fanOff()");
analogWrite(fanPin, 0);
}
void eyesOn() {
Serial.println("eyesOn()");
digitalWrite(eyesPin, HIGH);
}
void eyesOff() {
Serial.println("eyesOff()");
digitalWrite(eyesPin, LOW);
}
void speak() {
Serial.println("speak()");
randomSeed(analogRead(4));
switch(random(1, 5)) {
case 1:
tone(speakerPin, NOTE_CS4, 50); delay(200);
tone(speakerPin, NOTE_CS4, 50); delay(100);
tone(speakerPin, NOTE_B6, 100); delay(200);
break;
case 2:
tone(speakerPin, NOTE_CS8, 50); delay(100);
tone(speakerPin, NOTE_E7, 100); delay(200);
tone(speakerPin, NOTE_E7, 100); delay(200);
break;
case 3:
tone(speakerPin, NOTE_D7, 100); delay(200);
tone(speakerPin, NOTE_DS6, 50); delay(100);
tone(speakerPin, NOTE_C7, 25); delay(50);
break;
case 4:
tone(speakerPin, NOTE_D6, 50); delay(100);
tone(speakerPin, NOTE_DS6, 50); delay(100);
tone(speakerPin, NOTE_E6, 50); delay(100);
break;
}
}
void doSomething() {
delay(500); speak();
randomSeed(analogRead(4));
switch(random(1,6)) {
case 1:
Serial.println("Random: seq 1");
if(lastReaction == 1) { doSomething(); break; }
lastReaction = 1;
motorOn(true); delay(1000); motorOff(); delay(500); motorOn(); delay(500); motorOff(); delay(1000); motorOn(); delay(200); motorOff(); delay(200); motorOn(); delay(200); motorOff(); break;
case 2:
Serial.println("Random: seq 2");
if(lastReaction == 2) { doSomething(); break; }
lastReaction = 2;
leds1On(); delay(500); leds1Off(); leds2On(); delay(500); leds2Off(); leds1On(); delay(500); leds1Off(); break;
case 3:
Serial.println("Random: seq 3");
if(lastReaction == 3) { doSomething(); break; }
lastReaction = 3;
fanOn(); eyesOff(); delay(500); eyesOn(); delay(500); eyesOff(); delay(500); eyesOn(); delay(500); eyesOff(); delay(500); eyesOn(); delay(500); eyesOff(); delay(500); eyesOn(); delay(500); eyesOff(); delay(500); eyesOn(); delay(500); fanOff(); break;
case 4:
Serial.println("Random: seq 4 (nothing)");
if(lastReaction == 4) { doSomething(); break; }
lastReaction = 4;
case 5:
Serial.println("Random: BERSEEEEERK!");
if(lastReaction == 5) { doSomething(); break; }
lastReaction = 5;
motorOn(); delay(10000); motorOff(); leds1On(); fanOn(); delay(3000); speak(); leds1Off(); leds2On(); speak(); leds2Off(); leds1On(); delay(100); leds1Off(); delay(100);
for (int i=0; i <= 10; i++) { leds1On(); delay(100); leds1Off(); delay(100); }
for (int i=0; i <= 10; i++) { leds2On(); delay(100); leds2Off(); delay(100); }
delay(1000); speak(); break;
}
speak(); delay(3000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment