"INTO THE DREAM"
INSTALLATION ARTISTIQUE
"INTO THE DREAM" ARTISTIC INSTALLATION

Support technique pour l'installation artistique d'une amie (câblage, électronique Arduino + relais, programmation Arduino + Python).

Les codes, pour les programmeurs-ses qui veulent rigoler :

Code Arduino



#include <Servo.h>

Servo servo;

int ultrasoundTriggerPin = 2;

int ultrasoundEchoPin = 5;

long ultrasoundDuration = 0;

int ultrasoundDistance = 0;

bool phoneRinging = false;

int lampPin = 8;

int switchPin = 7;

int switchState = 0;

int smokeSwitchPin = 9;

int previousSwitchState = 0;

int switchHasChanged = 0;

bool servoHasMoved = false;

unsigned long currentMillis = 0;

unsigned long servoMillis = 0;

unsigned long previousMillis = 0;

unsigned long currentRingMillis = 0;

unsigned long ringMillis = 0;

unsigned long previousRingMillis = 0;

int servoInterval = 3000;

int ringLength = 17000;

bool servoTimerExpired = false;

bool servoTimerHasStarted = false;

bool attenteHasStarted = false;

int relay1Pin = 4;

bool presenceStarted = false;

bool ringTimerStarted = false;

bool ringTimerExpired = false;


unsigned long currentDaughterMillis = 0;

unsigned long daughterMillis = 0;

unsigned long previousDaughterMillis = 0;

int daughterLength = 6000;

bool daughterTimerStarted = false;

bool daughterTimerExpired = false;


unsigned long currentFinalMillis = 0;

unsigned long finalMillis = 0;

unsigned long previousFinalMillis = 0;

int finalLength = 13000;

bool finalTimerStarted = false;

bool finalTimerExpired = false;


bool hasPickedUp = false;





void setup() {


  servo.attach(smokeSwitchPin);

  servo.write(10);

  Serial.begin(9600);

  Serial.setTimeout(1);

  pinMode(ultrasoundTriggerPin, OUTPUT);

  pinMode(ultrasoundEchoPin, INPUT);

  pinMode(lampPin, OUTPUT);

  pinMode(switchPin, INPUT);

  pinMode(smokeSwitchPin, OUTPUT);

  pinMode(relay1Pin, OUTPUT);

  digitalWrite(lampPin, HIGH);

  digitalWrite(relay1Pin, HIGH);

}


void daughterTimerReset() {

  daughterTimerExpired = false;

  daughterTimerStarted = false;

  daughterMillis = 0;

}


void finalTimerReset() {

  finalTimerExpired = false;

  finalTimerStarted = false;

  finalMillis = 0;

}


void daughterTimer() {

  if (daughterTimerStarted == false) previousDaughterMillis = currentDaughterMillis;

  if (daughterTimerExpired == true) daughterTimerReset();

  if (daughterTimerStarted == true) {

    if (daughterTimerExpired == false) daughterMillis = currentDaughterMillis - previousDaughterMillis;

    if (daughterMillis > daughterLength) daughterTimerExpired = true;

  }

}



void finalTimer() {

  if (finalTimerStarted == false) previousFinalMillis = currentFinalMillis;

  if (finalTimerExpired == true) finalTimerReset();

  if (finalTimerStarted == true) {

    if (finalTimerExpired == false) finalMillis = currentFinalMillis - previousFinalMillis;

    if (finalMillis > finalLength) finalTimerExpired = true;

  }

}



void servoStart() {

  servo.write(40);

  servoTimerHasStarted = true;

}



void servoReset() {

  servo.write(10);

  servoTimerExpired = false;

  servoTimerHasStarted = false;

}


void servoTimer() {

  if (servoTimerHasStarted == false) previousMillis = currentMillis;

  if (servoTimerHasStarted == true) {

    if (servoTimerExpired == false) servoMillis = currentMillis - previousMillis;

    if (servoTimerExpired == true) servoReset();

    if (servoMillis > servoInterval) servoTimerExpired = true;

  }

}



void ringTimerReset() {

  ringTimerExpired = false;

  ringTimerStarted = false;

  ringMillis = 0;

}


void ringingTimer() {

  if (ringTimerStarted == false) previousRingMillis = currentRingMillis;

  if (ringTimerStarted == true) {

    if (ringTimerExpired == false) ringMillis = currentRingMillis - previousRingMillis;

    if (ringMillis > ringLength) ringTimerExpired = true;

    if (ringTimerExpired == true) ringTimerReset();

  }

}





void loop() {


  digitalWrite(ultrasoundTriggerPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(ultrasoundTriggerPin, LOW);

  ultrasoundDuration = pulseIn(ultrasoundEchoPin, HIGH);

  ultrasoundDistance = ultrasoundDuration * 0.034 / 2;

  delay(100);

  switchState = digitalRead(switchPin);



  if (previousSwitchState != switchState) {

    switchHasChanged = 1;

    previousSwitchState = switchState;

  }


  if (finalTimerStarted == false) digitalWrite(lampPin, HIGH);

  //if (finalTimerStarted == true) digitalWrite(relay1Pin, HIGH);


  currentMillis = millis();

  currentRingMillis = millis();

  currentDaughterMillis = millis();

  currentFinalMillis = millis();






  servoTimer();

  ringingTimer();

  daughterTimer();

  finalTimer();


  if (switchState == 1 && finalTimerExpired == true) {  //EST RESTE DECROCHE APRES FINALE

    Serial.println("estdecrocheavide"); delay(20);

    digitalWrite(relay1Pin, LOW);

  }



  if (finalTimerStarted == false && switchState == 0 && switchHasChanged == 1 && daughterTimerStarted == false) {  //ON RACCROCHE EN GENERAL

    switchHasChanged = 0;

    switchState = 1;

    digitalWrite(relay1Pin, HIGH);

    Serial.println("raccroche");

    servoReset();

    ringTimerReset();

    daughterTimerReset();

  }


  if (finalTimerStarted == false && switchState == 0 && switchHasChanged == 1 && daughterTimerStarted == true) {  //ON RACCROCHE PENDANT DAUGHTER SOUND

    switchHasChanged = 0;

    switchState = 1;

    digitalWrite(relay1Pin, HIGH);

    Serial.println("araccrocheaunez");

    servoReset();

    ringTimerReset();

    daughterTimerReset();

    delay(700);

    ringingTimer();

    ringTimerStarted = true;

  }


  if (finalTimerStarted == false && switchState == 0 && attenteHasStarted == false) {  //TELEPHONE EN ATTENTE

    Serial.println("attente");

    attenteHasStarted = true;

  }



  if (finalTimerStarted == false && presenceStarted == false && ultrasoundDistance < 10) {

    Serial.println("presence"); delay (20);

    //digitalWrite(relay1Pin, HIGH);

    presenceStarted = true;

    attenteHasStarted = true;

    ringTimerStarted = true;

    ringingTimer();

  }


  if (finalTimerStarted == false && presenceStarted == true && ultrasoundDistance > 50) {

    Serial.println("absence"); delay (20);

    //digitalWrite(relay1Pin, HIGH);

    attenteHasStarted = true;

    presenceStarted = false;

  }



  if (finalTimerStarted == false && switchState == 1 && switchHasChanged == 1 && ringTimerStarted == true) {  //ON DECROCHE

    switchHasChanged = 0;

    switchState = 0;

    attenteHasStarted = false;

    presenceStarted = false;

    daughterTimerStarted = true;

    digitalWrite(relay1Pin, LOW);

    Serial.println("decroche");

    hasPickedUp = true;

    ringTimerReset();

  }


  if (finalTimerStarted == false && switchState == 1 && hasPickedUp == true) {  //ON GARDE DECROCHE

    daughterTimerStarted = true;

    hasPickedUp = false;

    //servoStart();

  }



  if (finalTimerStarted == false && switchState == 1 && daughterTimerExpired == true) {  //ON GARDE DECROCHE ASSEZ LONGTEMPS

    Serial.println("gardedecrocheassezlongtemps");

    digitalWrite(lampPin, LOW);

    servoStart();

    daughterTimerReset();

    finalTimerStarted = true;

  }




  if (finalTimerStarted == false && switchState == 1 && switchHasChanged == 1 && ringTimerStarted == false) {  //ON DECROCHE A VIDE

    switchHasChanged = 0;

    switchState = 0;

    attenteHasStarted = false;

    presenceStarted = false;

    digitalWrite(relay1Pin, LOW);

    Serial.println("adecrocheavide"); delay(20);

  }





}

Code Python



import time

import serial

import subprocess

import keyboard

import vlc


ArduinoSerial = serial.Serial('com5',9600) 

time.sleep(1)

#ArduinoSerial.flushInput()

#ArduinoSerial.flushOutput()


player = vlc.Instance()

media_player = player.media_player_new()

ring = player.media_new("ring.mp3")

daughter = player.media_new("daughter.mp3")

lowtone = player.media_new("lowtone.mp3")


incoming = "0"

phonePickedUp = False

daughterIsPlaying = False

ringIsPlaying = False

lowtoneIsPlaying = False


time.sleep(2)       #gives me 2 seconds to switch to VLC windows fullscreen




while True:

   

    incoming = str (ArduinoSerial.readline())

    print (incoming)



    if 'estdecrocheavide' in incoming:

        incoming = "adecrocheavide"


    if 'attente' in incoming:

        keyboard.send('ù')              #press play



    if ('presence' in incoming and ringIsPlaying == False and phonePickedUp == False):   #rings the phone when somebody comes close

        media_player.stop()

        media_player.set_media(ring)

        time.sleep(0.05)

        media_player.play()

        time.sleep(0.5)

        print (media_player.get_state())

        ringIsPlaying = True


        


    if ('decroche' in incoming and ringIsPlaying == True):     #if phone picked up while ringing, plays daughter sound

        phonePickedUp = True

        media_player.stop()

        media_player.set_media(daughter)

        media_player.play()

        daughterIsPlaying = True

        lowtoneIsPlaying = False

        ringIsPlaying = False



    if ('gardedecrocheassezlongtemps' in incoming):     #if Arduino's daughterTimer expired, sends Next to VLC and hopefully plays trees falling video

        media_player.stop()

        keyboard.send('$')

        time.sleep(13)

        keyboard.send('$')

        incoming = "0"

        ringIsPlaying = False

        daughterIsPlaying = False

        lowtoneIsPlaying = False

        

        



    if ('adecrocheavide' in incoming):       #if phone picked up without Arduino's ringTimer started, plays dial line low tone sound

        phonePickedUp = True

        media_player.stop()

        media_player.set_media(lowtone)

        media_player.play()

        ringIsPlaying = False

        daughterIsPlaying = False

        lowtoneIsPlaying = True

        



    if ('araccrocheaunez' in incoming and ringIsPlaying == False):

        media_player.stop()

        media_player.set_media(ring)

        time.sleep(0.7)

        media_player.play()

        ringIsPlaying = True

        daughterIsPlaying = False

        lowtoneIsPlaying = False

        phonePickedUp = False

        



    

       

    if ('raccroche' in incoming and lowtoneIsPlaying == True):     #if phone hanged up, stops all media

        media_player.stop()

        ringIsPlaying = False

        lowtoneIsPlaying = False

        daughterIsPlaying = False

        phonePickedUp = False