Hi everyone! I have a question in regards to an interaction design project that I am currently setting up for an exhibition.
The concept involves a lamp that reacts with breathing light once a user presents themselves to it. It makes use of a HC-SR04 distance sensor, Arduino Uno board, LED array and a warm bulb connected to a relay.
Originally, the project had a counterpart overseas and the two communicated using an online server to interpret and send back instructions. Each lamp's distance would be sent, and then a value would be pushed out by the server to trigger a specific state back on each of the lamps. The LED array reacts almost as a local feature of each lamp, and is just dependent on the user's distance away from the sensor. The bulb however is triggered when a user is present at both lamps, showing that someone else is there, standing in front of theirs. If only one user is present at a lamp, then only local LED functions will happen.
As of now, the lamp is just to be exhibited in a gallery and will not be connected to the internet, meaning that I have had to redo the code for the lamp we have. Currently I have created the desired breathing/intensity effect of the LED array based upon specific distance zones I have defined, also being the only reaction that needs to happen real time.
For the purpose of the exhibition, I would just like to replicate the toggling of the bulb relay. I am hoping someone would be able to help me with code that randomly toggles the relay switch, at relatively long intervals? Would this even be feasible?
My thoughts is that it would give the same effect of someone walking past at the other hypothetical lamp. I am not quite sure how to go about randomizing a switch at the same time as my breathing script, as I am still pretty new to Arduino. This randomising code would only need to run concurrently to the LED breathing, and not when the lamp is in resting state and not detecting a user.
Let me know what you think! And thank you for any help you may give ![]()
#define echoPin 2
#define trigPin 3
long duration;
int distance;
const int ledPin = 9;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("Ultrasonic Sensor HC-SR04 Test");
Serial.println("with Arduino UNO R3");
digitalWrite (ledPin, LOW);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(1);
digitalWrite(trigPin, HIGH);
delayMicroseconds(1);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if ((distance < 60) && (distance > 10)){
for (int a=20; a<=255;a++)
{
analogWrite(ledPin, a);
delay(2);
}
for (int a=255; a>=20;a--)
{
analogWrite(ledPin, a);
delay(2);
}
delay(100);
digitalWrite (ledPin, LOW);
}
if ((distance < 130) && (distance >= 60)){
for (int a=3; a<=60;a++)
{
analogWrite(ledPin, a);
delay(15);
}
for (int a=60; a>=3;a--)
{
analogWrite(ledPin, a);
delay(15);
}
delay(50);
digitalWrite (ledPin, LOW);
}
if (distance >= 130){
for (int a=0; a<=10;a++)
{
analogWrite(ledPin, a);
delay(100);
}
for (int a=10; a>=0;a--)
{
analogWrite(ledPin, a);
delay(100);
}
delay(800);
digitalWrite (ledPin, LOW);
}
if (distance <= 10){
digitalWrite (ledPin, LOW);
}
else{
digitalWrite (ledPin, LOW);
}
}