Hi!
I'm creating an interactive art installation but I'm not a programmer, so I need help. I have to use an HC-SR04 ultrasonic sensor to control a spotlight.
I tried the ultrasonic sensor without the spotlight and it works.
I bought a DMX shield (CQRobot DMX Shield MAX485) to control the spotlight with my Arduino Uno, I used the DMX library without the ultrasonic sensor, which works too.
Now I have to use them together, but I don't know how to write the code. When I tried to unite the code of the ultrasonic sensor and the DMX one, there was this error:
HardwareSerial0.cpp.o (symbol from plugin): In function Serial': (.text+0x0): multiple definition of __vector_18'
C:\Users\giada\AppData\Local\Temp\arduino\sketches\56EE4BF8C1043E7874A58BAAA57B454D\libraries\Conceptinetics\Conceptinetics.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
exit status 1
Compilation error: exit status 1
It seems that serial is the problem. I've tried not to write the serial, but obviously it didn't work. Can anyone help me?
This is my code at this moment:
#include <Conceptinetics.h>
const int echo = 13;
const int trig = 12;
const int LED1 = 3;
const int LED2 = 2;
int duration = 0;
int distance = 0;
DMX_Master dmx_master(4,1);
// the setup routine runs once when you press reset:
void setup() {
pinMode (trig, OUTPUT);
pinMode (echo, INPUT);
dmx_master.enable();
//Serial.begin(9600);
}
void loop()
{
digitalWrite (trig, HIGH);
delayMicroseconds (10000);
digitalWrite (trig, LOW);
duration = pulseIn(echo, HIGH);
distance = (duration/2) / 28.5;
//Serial.println(distance);
// dmx_master.setChannelValue(1,255);
if (distance <= 6)
{
dmx_master.setChannelValue(2,255); // Rosso
dmx_master.setChannelValue(3,0); // Verde
dmx_master.setChannelValue(4,0); // Blu
}
else
{
dmx_master.setChannelValue(2,0); // Rosso
dmx_master.setChannelValue(3,255); // Verde
dmx_master.setChannelValue(4,0); // Blu
}
}
Hi! I've used your code and it works. Thanks!
Now I have another problem. The spotlight has to work in this way:
when no one is in the range of the ultrasensor, the intensity of the light has to be 20. Then, when someone is in the range of the ultrasensor, the intensity of the light has to be 90. I also need a fade from 20 to 100. And, of course, when someone stands in the range, the intensity of 100 has to remain.
The problem is that now, when I use my code, at first the light is off, when someone is in the range it lights on, and then it remains on. I don't know why.
This is my code:
const int echo = 13;
const int trig = 12;
int i = 20;
DMX_Master dmx_master(4, 2);
void setup() {
pinMode (trig, OUTPUT);
dmx_master.enable();
dmx_master.setChannelValue(1, 255);
}
void loop() {
static long duration = 0;
digitalWrite (trig, LOW);
delayMicroseconds (2);
digitalWrite (trig, HIGH);
delayMicroseconds (5);
digitalWrite (trig, LOW);
duration = pulseIn(echo, HIGH);
int distance = duration / 57;
if (distance <= 100 && i == 20) // se l'oggetto e' sotto i 200mt e la luce NON e' gia' al massimo, cioe' 100...
{ // FADE IN
for (i = 20; i <= 100; i++) {
// se non funziona, commenta da qui...
if(i <= 90) {
dmx_master.setChannelRange(1,4,i); // Brightness has to be max 90
} else {
dmx_master.setChannelRange(2,4,i); // RGB channels are max 100
}
delay(10);
// ...a qui
/*
e decommenta questo
dmx_master.setChannelValue(2, i); // Rosso
dmx_master.setChannelValue(3, i); // Verde
dmx_master.setChannelValue(4, i); // Blu
if(i <= 90) {
dmx_master.setChannelValue(1, i); // luminosita
}
*/
}
}
else // se l'oggetto e' piu' lontano di 200mt e la luce e' gia' al massimo...
{ // FADE OUT
if (distance > 100 && i == 100) { // se nessuno e' ancora passato davanti al sensore (i == 20), l'intensita' NON va sfumata
for (i = 100; i >= 20; i--) {
// se non funziona, commenta da qui...
if(i <= 90){
dmx_master.setChannelRange(1,4,i); // It fades brightness ch too (1) because it starts from 90
} else {
dmx_master.setChannelRange(2,4,i); // RGB channels start from 100
}
delay(10);
// ...a qui
/*
e decommenta questo
dmx_master.setChannelValue(2, 0); // Rosso
dmx_master.setChannelValue(3, 0); // Verde
dmx_master.setChannelValue(4, 0); // Blu
if(i <= 90){
dmx_master.setChannelValue(1, 0);
}
*/
}
}
}
delay(1000);
}
Ok, now it lights off, but then it doesn't light on again.
Anyway, there's also the problem that it doesn't have to switch on/off, but it has to change the intensity from 20 to 90.
#include <Conceptinetics.h>
const int echo = 13;
const int trig = 12;
DMX_Master dmx_master(4, 2);
void setup() {
pinMode (trig, OUTPUT);
dmx_master.enable();
dmx_master.setChannelValue(1, 255);
}
void loop() {
static long duration = 0;
static int distance = 0;
static bool isDimmed = true;
digitalWrite (trig, LOW);
delayMicroseconds (2);
digitalWrite (trig, HIGH);
delayMicroseconds (5);
digitalWrite (trig, LOW);
duration = pulseIn(echo, HIGH);
if (duration)distance = duration / 57;
if (isDimmed) {
if (distance <= 100) // se l'oggetto e' sotto i 200mt e la luce NON e' gia' al massimo, cioe' 100...
{ // FADE IN
for (byte i = 20; i <= 100; i++) {
if (i <= 90)dmx_master.setChannelRange(1, 4, i); // Brightness has to be max 90
else dmx_master.setChannelRange(2, 4, i); // RGB channels are max 100
delay(10);
}
isDimmed = false;
}
}
else // se l'oggetto e' piu' lontano di 200mt e la luce e' gia' al massimo...
{ // FADE OUT
if (distance > 100 ) { // se nessuno e' ancora passato davanti al sensore (i == 20), l'intensita' NON va sfumata
for (byte i = 100; i >= 20; i--) {// se non funziona, commenta da qui...
if (i > 90)dmx_master.setChannelRange(2, 4, i); // RGB channels start from 100
else dmx_master.setChannelRange(1, 4, i); // It fades brightness ch too (1) because it starts from 90
delay(10);
}
}
isDimmed = true;
}
delay(1000);
}
C:\Users\giada\AppData\Local\Temp.arduinoIDE-unsaved202485-2192-1ii95ui.a6ee\sketch_sep5a\sketch_sep5a.ino:4:1: error: 'DMX_Master' does not name a type
DMX_Master dmx_master(4, 2);
^~~~~~~~~~
C:\Users\giada\AppData\Local\Temp.arduinoIDE-unsaved202485-2192-1ii95ui.a6ee\sketch_sep5a\sketch_sep5a.ino: In function 'void setup()':
C:\Users\giada\AppData\Local\Temp.arduinoIDE-unsaved202485-2192-1ii95ui.a6ee\sketch_sep5a\sketch_sep5a.ino:9:3: error: 'dmx_master' was not declared in this scope
dmx_master.enable();
^~~~~~~~~~
C:\Users\giada\AppData\Local\Temp.arduinoIDE-unsaved202485-2192-1ii95ui.a6ee\sketch_sep5a\sketch_sep5a.ino: In function 'void loop()':
C:\Users\giada\AppData\Local\Temp.arduinoIDE-unsaved202485-2192-1ii95ui.a6ee\sketch_sep5a\sketch_sep5a.ino:29:21: error: 'dmx_master' was not declared in this scope
if (i <= 90)dmx_master.setChannelRange(1, 4, i); // Brightness has to be max 90
^~~~~~~~~~
C:\Users\giada\AppData\Local\Temp.arduinoIDE-unsaved202485-2192-1ii95ui.a6ee\sketch_sep5a\sketch_sep5a.ino:30:14: error: 'dmx_master' was not declared in this scope
else dmx_master.setChannelRange(2, 4, i); // RGB channels are max 100
^~~~~~~~~~
C:\Users\giada\AppData\Local\Temp.arduinoIDE-unsaved202485-2192-1ii95ui.a6ee\sketch_sep5a\sketch_sep5a.ino:40:20: error: 'dmx_master' was not declared in this scope
if (i > 90)dmx_master.setChannelRange(2, 4, i); // RGB channels start from 100
^~~~~~~~~~
C:\Users\giada\AppData\Local\Temp.arduinoIDE-unsaved202485-2192-1ii95ui.a6ee\sketch_sep5a\sketch_sep5a.ino:41:14: error: 'dmx_master' was not declared in this scope
else dmx_master.setChannelRange(1, 4, i); // It fades brightness ch too (1) because it starts from 90
^~~~~~~~~~
exit status 1
Compilation error: 'DMX_Master' does not name a type