Processing+Arduino+DistanceSensor
in
Integration and Hardware
•
1 year ago
Trying to figure out how to code in processing to read values from my distance sensor (HC-SR04) and have the values change an image on the screen. Ideally I'd like to be able to change the brightness of an image, and eventually be able to change frames in a short animation using the distance sensor.
I've followed this tutorial on youtube http://www.youtube.com/watch?v=g0pSfyXOXj8 which teaches how to use a potentiometer to change the brightness of a drawn rectangle, but I couldn't figure out how to replace the potentiometer with the distance sensor.
Any ideas?
Here's the code I used for both the arduino and processing.
Any help is much appreciated!
Arduino code:
Processing Code:
I've followed this tutorial on youtube http://www.youtube.com/watch?v=g0pSfyXOXj8 which teaches how to use a potentiometer to change the brightness of a drawn rectangle, but I couldn't figure out how to replace the potentiometer with the distance sensor.
Any ideas?
Here's the code I used for both the arduino and processing.
Any help is much appreciated!
Arduino code:
- #include "Ultrasonic.h"
int TRIG = 2; // Trigger Pin
int ECHO = 3; // Echo Pin
int Range; // The range of the object from the HC-SR04 Ultarsonic Module
Ultrasonic ultrasonic(TRIG,ECHO); // Create and initialize the Ultrasonic object.
void setup()
{
Serial.begin(9600);
}
void loop() {
Range = ultrasonic.Ranging(CM); // Range is calculated in Centimeters.
// Range = ultrasonic.Ranging(INC); // Range is calculated in Inches.
int val = map(analogRead(Range), 0, 1023, 0, 255);
Serial.print(Range);
Serial.println("CM");
Serial.println(val);
delay(50);
}
Processing Code:
- import processing.serial.*;
Serial port;
float brightness = 0;
void setup()
{
size (500,500);
port = new Serial(this, "/dev/tty.usbmodem1d11", 9600);
port.bufferUntil('\n');
}
void draw()
{
background(0,0,brightness);
}
void serialEvent (Serial Port)
{
brightness = float(port.readStringUntil('\n'));
}
1