Go Down

Topic: Interactive Poster Series Using HCSR04 Ultrasonic Sensor, Arduino and Processing (Read 430 times) previous topic - next topic

un-nadeen_alattar_1999

Hi

I'm currently working on an interactive poster that should basically show 1 image when you get closer to it, show a 2nd image when you get farther away, then show a 3rd image when you get close again, and a 4th when you get farther away again, and then it loops.


I found these two codes online

this is the one for the ultrasonic sensor on arduino

Code: [Select]

#include <HCSR04.h>

// Initialize sensor that uses digital pins 13 and 12.
int triggerPin = 13;
int echoPin = 12;
UltraSonicDistanceSensor distanceSensor(triggerPin, echoPin);


void setup() {
  // put your setup code here, to run once:
   Serial.begin(9600);  // We initialize serial connection so that we could print values from sensor.

}

void loop() {
  // put your main code here, to run repeatedly:
   // Every 500 miliseconds, do a measurement using the sensor and print the distance in centimeters.
    double distance = distanceSensor.measureDistanceCm();
    Serial.println(distance);
    delay(500);

}



and this is the processing code

Code: [Select]

import processing.serial.*; 
PImage PB_100;
PImage PB_80;
PImage PB_60;
PImage PB_40;
PImage PB_20;
PImage PB_CB;
Serial myPort; 
String data="" ;
int distance;
float transparency = 255;
 
 
void setup() {
  size(595, 842); // size of processing window
  PB_100 = loadImage("PB_100.png");
  PB_80 = loadImage("PB_80.png");
  PB_60 = loadImage("PB_60.png");
  PB_40 = loadImage("PB_40.png");
  PB_20 = loadImage("PB_20.png");
  PB_CB = loadImage("PB_CB.png");
 
  PB_100.resize(595, 842);
  PB_80.resize(595, 842);
  PB_60.resize(595, 842);
  PB_40.resize(595, 842);
  PB_20.resize(595, 842);
  PB_CB.resize(595, 842);
 
  background(255);
 
  myPort = new Serial(this, "COM7", 9600);
  myPort.bufferUntil('\n');
}
 
void draw() {
 
  println(distance);
  if (distance >= 150) {
    image(PB_100, 0, 0); // 100
  }
 
  if (distance <= 145) {
    image(PB_80, 0, 0); // 80
  }
 
  if (distance <= 130) {
    image(PB_60, 0, 0); // 60
  }
 
  if (distance <= 110) {
    image(PB_40, 0, 0); // 40
  }
 
  if (distance <= 90) {
    image(PB_20, 0, 0); // 20
  }
 
  if (distance <= 70) {
    image(PB_CB, 0, 0); // 0
  }
 
}
 
void serialEvent(Serial myPort) {
 
  data=myPort.readStringUntil('\n');
  distance=int(trim(data));
}


The 4 out of the six images are just transitions it's the first and last one that matter. So far I've managed to get it to show the 1st and 2nd images, but I can't figure out a way to add the 3rd and 4th. When I try to add code for them it just overrides the other two.

Is there a way to make it go through all four and then loop either from arduino or processing? 

Go Up