Go Down

Topic: knock sensor Play Fight (Read 1 time) previous topic - next topic

baschz

May 07, 2012, 01:44 pm Last Edit: May 12, 2012, 04:50 pm by baschz Reason: 1
hi everyone, i'm pretty new to Arduino and i hope someone can help me with what is probably a simple question

for a project i'm doing for an upcoming exhibition i'm using the piezo knock sensor example;

- i have a drawing of a face i made and attached a piezo element to the back to work as a knock sensor
- when something is thrown against it LEDs will light up behind the drawing, revealing a second drawing
 on the backside as it is now showing through the paper
- after a few seconds the LEDs will go out
- back to beginning

i have this working with 1 drawing so far. what i now want to do is have 2 seperate drawings with each
their own knock sensor and LEDs reacting to it. is this just a matter of copypasting parts of the code or
should i have 2 Arduinos for this? if possible, can anyone please help me with some copypasting tips /
examples?

thanks in advance! cheers Baschz

RIDDICK

hi baschz!

do u have a datasheet of that sensor?

the arduino has more that 10 digital input lines, that u can use simultaneously...
or u use a mux (e. g. 74HC4051 or HEF4067), which can connect one of its many inputs to 1 digital/analog input...

have fun...
-Arne

baschz

hi Riddick, and thanks for your quick reply! i'm mostly curious about what to do with the code to keep the two apart. can i just copypaste parts and put them on different pins? how do i tell one thing to react to the other and not the other other. i'm using the example code from the knock sensor and replaced the LED-bit so it just turns the LEDs on when a knock is sensed, and then it turns off again:

// these constants won't change:
const int ledPin = 13;      // led connected to digital pin 13
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 1000;  // threshold value to decide when the detected sound is a knock or not

// these variables will change:
int sensorReading = 0;      // variable to store the value read from the sensor pin
int ledState = LOW;         // variable used to store the last LED status, to toggle the light

void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
Serial.begin(9600);       // use the serial port
}

void loop() {
  // read the sensor and store it in the variable sensorReading:
  sensorReading = analogRead(knockSensor);   
 
  // if the sensor reading is greater than the threshold:
  if (sensorReading >= threshold) {
   
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
 
    // send the string "Knock!" back to the computer, followed by newline
    Serial.println("Knock!");         
  }}


so my question is; what do i add/change to the code to have two seperate piezo knock sensors, activating two seperate LEDs

RIDDICK

u need a little more complex structure...

Code: [Select]

const uint8_t C = 5; // sensor pins: analog pins 0, ..., (C-1)
const uint16_t thresh = 1000; // ADC
const uint16_t DT = 2000; // msec
uint16_t when[C];
void setup() {
 for (uint8_t i=0; i<C; i++) {
   when[i] = i*500;
   digitalWrite(13-i,HIGH); // all LEDs on at start for self-test
   pinMode(13-i,OUTPUT);  // led pins: (13-C+1), ..., 13
 }
}

void loop() {
 const uint16_t now = millis();
 for (uint8_t i=0; i<C; i++) {
   if (analogRead(i)>thresh) when[i]=now;
   if (now-when[i]>DT) {
     when[i] = now-(DT+1U);
     digitalWrite(13-i,LOW);
   } else
     digitalWrite(13-i,HIGH);
 }
}
-Arne

baschz

Thanks Arne!

i will definitely try yours as well. i tried copypasting around a bit myself and come up with this, which also seems to work

// these constants won't change:
const int ledPinA = 12;      // led connected to digital pin 12
const int ledPinB = 13;      // led connected to digital pin 13
const int knockSensorA = A0; // the piezo is connected to analog pin 0
const int knockSensorB = A1; // the piezo is connected to analog pin 0
const int threshold = 1000;  // threshold value to decide when the detected sound is a knock or not


// these variables will change:
int sensorReadingA = 0;      // variable to store the value read from the sensor pin
int sensorReadingB = 0;      // variable to store the value read from the sensor pin
int ledState = LOW;         // variable used to store the last LED status, to toggle the light
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

void setup() {
pinMode(ledPinA, OUTPUT); // declare the ledPin as as OUTPUT
pinMode(ledPinB, OUTPUT); // declare the ledPin as as OUTPUT
pinMode(9, OUTPUT);       // declare the ledPin as as OUTPUT
Serial.begin(9600);       // use the serial port
}

void loop() {
  // read the sensor and store it in the variable sensorReading:
  sensorReadingA = analogRead(knockSensorA);   
  sensorReadingB = analogRead(knockSensorB);   
 
  // if the sensor reading is greater than the threshold:
  if (sensorReadingA >= threshold) {
   
  digitalWrite(12, HIGH);   // set the LED on
  delay(2000);              // wait for a second
  digitalWrite(12, LOW);    // set the LED off
       
  }
 
  if (sensorReadingB >= threshold) {
   
  digitalWrite(13, HIGH);   // set the LED on
  delay(2000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
 

       
  }
}

RIDDICK

ok

but what if two areas r knocked within the same second?
in ur program just one LED can be turned on (the others must be off)...
if that is the wanted behaviour my proposal would be not what u want...  :)
-Arne

baschz

a very good reason to use yours. since that was indeed the one thing that was a bit unfortunate with my copypasting. being new at this is was just happy i got it to work with some simple logic in the first place. i am definitely going to try yours


baschz

#8
May 12, 2012, 04:28 pm Last Edit: May 12, 2012, 04:48 pm by baschz Reason: 1
UPDATE:

Here's what i made. I ended up adding another lesson for myself by replacing the LEDs with 220V lightbulbs thus adding relays to the equation. It all worked great and came out just like i wanted it to. Very happy with the result and people were playing with it the whole night!

http://www.youtube.com/watch?v=agwJWLmuyxg










RIDDICK

cool
does 2 at the same time work, 2?
or did i miss that in the video?

how comes that the light fades out?
looks not just like a relay... more like a dimmer...

btw:
there r high power LEDs that can be as good as a light blub...
-Arne

baschz

hi RIDDICK,

thanxxx

because of time issues i ended up using my scruffy copypaste script for the opening since i already had that working, so no two at the same time yet, but it didn't matter that much either, since that hasn't happened that much

the light doesn't fade out, that's just m'iphone having trouble adjusting to the lights

i want to take some time and rebuild them into stand(hang actually)alone boxxxes, so LEDs will probably be the best option in that case. high power LEDs sounds like the still need .. well .. high power. or do you think the same effect can be achieved without using 220 volts, which would be perfect for standalone boxxxes. i guess boxxxing the whole thing (and probably coating the inside of it with a reflective material) would already help me 'keep' a lot of the light inside

RIDDICK

#11
May 15, 2012, 12:38 am Last Edit: May 16, 2012, 01:22 am by RIDDICK Reason: 1
high power LEDs:

i use them for my bedroom... Cree XP-G R5, white, 493 lumen...
3 of them need about 10V and 500mA (=5W)...
and they deliver 800lumen, which is as bright as a 50W light bulb...
u could give them 3 times more current and get about two times more photons... :)

u will need a current source for them:
u can use a converter that uses a low-side-switch, that uses a  series resistor between the switch and ground as feed back...
i use ATtiny-s for such converters...
the HV9910 uses the same topology...
-Arne

Go Up