Pages: [1]   Go Down
Author Topic: knock sensor Play Fight  (Read 1225 times)
0 Members and 1 Guest are viewing this topic.
rotterdam NL
Offline Offline
Newbie
*
Karma: 0
Posts: 7
artist / designer / curator / showmaster
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: May 12, 2012, 09:50:35 am by baschz » Logged

Brunsbüttel, SH, F.Rep.GERM
Offline Offline
God Member
*****
Karma: 4
Posts: 596
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

-Arne

rotterdam NL
Offline Offline
Newbie
*
Karma: 0
Posts: 7
artist / designer / curator / showmaster
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Brunsbüttel, SH, F.Rep.GERM
Offline Offline
God Member
*****
Karma: 4
Posts: 596
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

u need a little more complex structure...

Code:
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);
  }
}
« Last Edit: May 08, 2012, 01:56:48 am by RIDDICK » Logged

-Arne

rotterdam NL
Offline Offline
Newbie
*
Karma: 0
Posts: 7
artist / designer / curator / showmaster
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
 

       
  }
}
Logged

Brunsbüttel, SH, F.Rep.GERM
Offline Offline
God Member
*****
Karma: 4
Posts: 596
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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...  smiley
Logged

-Arne

rotterdam NL
Offline Offline
Newbie
*
Karma: 0
Posts: 7
artist / designer / curator / showmaster
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Brunsbüttel, SH, F.Rep.GERM
Offline Offline
God Member
*****
Karma: 4
Posts: 596
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

*wag tail*  smiley
Logged

-Arne

rotterdam NL
Offline Offline
Newbie
*
Karma: 0
Posts: 7
artist / designer / curator / showmaster
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!











« Last Edit: May 12, 2012, 09:48:55 am by baschz » Logged

Brunsbüttel, SH, F.Rep.GERM
Offline Offline
God Member
*****
Karma: 4
Posts: 596
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

-Arne

rotterdam NL
Offline Offline
Newbie
*
Karma: 0
Posts: 7
artist / designer / curator / showmaster
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Brunsbüttel, SH, F.Rep.GERM
Offline Offline
God Member
*****
Karma: 4
Posts: 596
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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... smiley

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...
« Last Edit: May 15, 2012, 06:22:07 pm by RIDDICK » Logged

-Arne

Pages: [1]   Go Up
Jump to: