top of page

Arduino

// Pin numbers of the sensor's output and input

const int TrigPin = 14;

const int EchoPin = 15;

float distance;

float distanceBuf[5] = {0};

void setup()

{ // initialize serial communication

  Serial.begin(9600);

  pinMode(TrigPin, OUTPUT);

  //In order to detect the signal input to the pin,

  //Set it as the input state first.

  pinMode(EchoPin, INPUT);

  //It detect the signal from the button connected to digital pin 8

  pinMode(8, INPUT_PULLUP);

}

void loop()

{

  // The device is triggered by a HIGH pulse of 2 or more microseconds.

  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse.

  digitalWrite(TrigPin, LOW);

  delayMicroseconds(2);

  digitalWrite(TrigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(TrigPin, LOW);

  // Read the signal from the TrigPin: a HIGH pulse

  // Distance is the time (in microseconds) from the sending of the EchoPin

  // to the reception of its echo off of an object.

  distance = float( pulseIn(EchoPin, HIGH, 17648) * 17 ) / 1000; 

  //transfer the counting unit to "cm"

  

  if (distance == 0.0)

  {

    distance = 300.0;

  }

 

  for (int i = 4; i > 0; i--)

  {

    distanceBuf[i] = distanceBuf[i - 1];

  }

  distanceBuf[0] = distance;

 

//set a minimun distance to block the signal 

  //from distance beyond 300cm

  float mindistance = 300.0;

  for (int i = 0; i < 5; i++)

  {

    if (mindistance > distanceBuf[i])

    {

      mindistance = distanceBuf[i];

    }

  }

  Serial.print(mindistance);

  int buttonstate = 0;

  if (digitalRead(8) == LOW)

  {

    delay(10);

    {

      if (digitalRead(8) == LOW)

      {

        buttonstate = 1;

      }

    }

  }

  Serial.print(",");

  Serial.print(buttonstate);

  Serial.println();

  delay(600);

}

Processing

import processing.video.*;

Movie movie_1;

Movie movie_2;

Movie movie_3;

//*****************************************************

// imports library for serial communication

import processing.serial.*; 

// defines Object Serial

Serial myPort; 

//*****************************************************

String RecBuf="";

float Distance=0.0;

float Button=0;

 

//Define the elements (variables)

int NewCMDFlag=0;

int NewCMDFlag_BUF=0;

int progressFlag=0;

 

void setup() {

  size(960, 540);

  background(0);

  // Load and play the video in a loop

  movie_1 = new Movie(this, "4.mov");

  movie_2 = new Movie(this, "6.mov");

  movie_3 = new Movie(this, "5.mov");

  printArray(Serial.list());

  // starts the serial communication

  myPort = new Serial(this, Serial.list()[3], 9600); 

}

 

void movieEvent(Movie m) {

  m.read();

}

 

void draw() {

  while (myPort.available() > 0) 

  {

    char inByte= myPort.readChar();

 

    if (inByte==0x0A)

    {

      //println(RecBuf);

      String[] oss=splitTokens(RecBuf, ",");

      //Transfer the data from Arduino to difine the distance and button

      //printArray(oss);

      Distance=Float.valueOf(oss[0]);

      println(Distance);

      Button=Float.valueOf(oss[1]);

      println(Button);

      RecBuf="";

    } else

    {

      if (inByte!=0x0D||inByte!=0x0A)

      {

        RecBuf+=inByte;

      }

    }

  }

 

 

  if (progressFlag==0)

  {

    progressFlag=1;

    if (Distance>80.0 &&Button==0)

    {

      //println("People Away");

      NewCMDFlag=1;

    } else if (Distance<=80.0 &&Button==0)

    {

      //println("People Close");

      NewCMDFlag=2;

    } else if (Distance<=80.0 &&Button==1)

    {

      //println("People Close,Feed Button");

      NewCMDFlag=3;

    }

  }

  if (NewCMDFlag>0 &&(NewCMDFlag_BUF!=NewCMDFlag))

  {

   // println("new one");

    if (NewCMDFlag==1)

    {

      movie_2.jump(0);

      movie_2.play();

    } else if (NewCMDFlag==2)

    {

      movie_1.jump(0);

      movie_1.play();

    } else if (NewCMDFlag==3)

    {

      movie_3.jump(0);

      movie_3.play();

    }

  }

  NewCMDFlag_BUF=NewCMDFlag;

  if (NewCMDFlag==2) {

    image(movie_1, 0, 0, width, height);

    if (progressFlag==1)

    {

      if ((movie_1.time()>=movie_1.duration()-1)&&progressFlag>0)

      {

        //println("People Close movie end");

        progressFlag=0;

      }

    }

  }

  if (NewCMDFlag==1) {

    image(movie_2, 0, 0, width, height);

    if (progressFlag==1)

    {

      if ((movie_2.time()>=movie_2.duration()-1)&&progressFlag>0)

      {

        //println("People Away movie end");

        progressFlag=0;

      }

    }

  }

  if (NewCMDFlag==3) {

    image(movie_3, 0, 0, width, height);

    if (progressFlag==1)

    {

      if ((movie_3.time()>=movie_3.duration()-1)&&progressFlag>0)

      {

        //println("People Close,Feed Button movie end");

        progressFlag=0;

      }

    }

  }

}

bottom of page