Pages: [1]   Go Down
Author Topic: Problem with "sound" function and laser harp  (Read 1634 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I'm trying to build a laser harp implementing Arduino Uno.

It works in this way: 6 lasers, each one hitting a photodiode. When the photodiode doesn't receive the laser (because the hand is interrupting it), a range sensor gives voltage based on the point where the hand is interrupting the laser (from 0 to 2.5V). There is a sensor for each laser.

I'm trying to write the code to load into the Arduino board, and there is something wrong.

Forgetting about the range sensors, I tried to build a code based exclusively on the status of the laser (interrupted/uninterrupted) and if it is interrupted, the board should send a "sound" to the speakers I connected.

This is the code (the frequences should be the same as a guitar):

Code:

int diodo1 = 2;
int diodo2 = 4;       
int diodo3 = 7;       
int diodo4 = 8;       
int diodo5 = 12;       
int diodo6 = 13;

int sens1 = A0;
int sens2 = A1;     
int sens3 = A2;
int sens4 = A3;
int sens5 = A4;
int sens6 = A5;

int suoni = 5;           

void setup()             
{                         
 pinMode(diodo1, INPUT);
 pinMode(diodo2, INPUT);
 pinMode(diodo3, INPUT);
 pinMode(diodo4, INPUT);
 pinMode(diodo5, INPUT);
 pinMode(diodo6, INPUT);
 pinMode(sens1, INPUT);
 pinMode(sens2, INPUT);
 pinMode(sens3, INPUT);
 pinMode(sens4, INPUT);
 pinMode(sens5, INPUT);
 pinMode(sens6, INPUT);
}

void loop()                                                     
{                                                               
  if (digitalRead(diodo1)==0)                               
  {                                                           
    noTone(suoni);                 
    tone (suoni,82.4);                   
  }                                                           
                                                   
  if (digitalRead(diodo1)==1 && digitalRead(diodo2)==1 && digitalRead(diodo3)==1 && digitalRead(diodo4)==1 && digitalRead(diodo5)==1 && digitalRead(diodo6)==1)
  {                                                             
    noTone(suoni);                                             
  }                                                             
                                                               
  if (digitalRead(diodo2)==0)                                   
  {                                                       
    noTone(suoni);
    tone (suoni,110);
  }
 
  if (digitalRead(diodo1)==1 && digitalRead(diodo2)==1 && digitalRead(diodo3)==1 && digitalRead(diodo4)==1 && digitalRead(diodo5)==1 && digitalRead(diodo6)==1)
  {
    noTone(suoni);
  }
 
  if (digitalRead(diodo3)==0)
  {
    noTone(suoni);
    tone (suoni,146.8);
  }
 
  if (digitalRead(diodo1)==1 && digitalRead(diodo2)==1 && digitalRead(diodo3)==1 && digitalRead(diodo4)==1 && digitalRead(diodo5)==1 && digitalRead(diodo6)==1)
  {
    noTone(suoni);
  }
 
  if (digitalRead(diodo4)==0)
  {
    noTone(suoni);
    tone (suoni,196);
  }
 
  if (digitalRead(diodo1)==1 && digitalRead(diodo2)==1 && digitalRead(diodo3)==1 && digitalRead(diodo4)==1 && digitalRead(diodo5)==1 && digitalRead(diodo6)==1)
  {
    noTone(suoni);
  }
 
  if (digitalRead(diodo5)==0)
  {
    noTone(suoni);
    tone (suoni,246.9);
  }
 
  if (digitalRead(diodo1)==1 && digitalRead(diodo2)==1 && digitalRead(diodo3)==1 && digitalRead(diodo4)==1 && digitalRead(diodo5)==1 && digitalRead(diodo6)==1)
  {
    noTone(suoni);
  }
 
  if (digitalRead(diodo6)==0)
  {
    noTone(suoni);
    tone (suoni,329.6);
  }
 
  if (digitalRead(diodo1)==1 && digitalRead(diodo2)==1 && digitalRead(diodo3)==1 && digitalRead(diodo4)==1 && digitalRead(diodo5)==1 && digitalRead(diodo6)==1)
  {
    noTone(suoni);
  }
}

But this is the result:



These are the problems I noticed:

1)I heard the "electric sound" and it shouldn't be so... dirty;
2)When there aren't interrupted lasers, I can hear a sound. This shouldn't happen.
3)Interrupting more lasers together makes a sound which becomes lower and lower, but what I wanted was that only THE LAST INTERRUPTED LASER makes the sound.

Can someone help me?

(I'm sorry for my english, in case of grammar mistakes)
Logged

USA
Offline Offline
Sr. Member
****
Karma: 13
Posts: 365
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1) Dirty sound:  With a single string  continuously active, noTone() is executed every time loop() executes, and then tone() is executed.  I don't know the internals of tone() and noTone(), but I'd expect that turning the tone on and off rapidly would have an effect on the sound that you hear.  I'd recommend executing noTone() only when the active string changes.

2) Sound all the time:  We don't have much information about this.  A schematic would help, and a description of the character and intensity of the sound.  It could be digital noise, or power supply noise, or an artifact caused by executing noTone() all the time.

3) Wanted last interrupted laser to determine the tone:  There's nothing in the code that remembers the last interrupted laser.  If you want this feature, you'll have to do something to make the program determine which laser was interrupted last.  I think you'll have to save the state of all the strings, and do manipulate the tone only when the state of any of them changes.
Logged

Offline Offline
Sr. Member
****
Karma: 8
Posts: 250
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Would you be willing to have the Arp Plugged to a Computer?
If so, maybe it would be a good idea to have it send MIDI messages instead of actual "sound".
Like this the sounds that you would hear could be any sound (piano, guitar, arp, saxophone, drums,...) and with much better quality that those sounds produced by the Arduino.

Also, it would be much less work for you if you would use arrays on your code
(http://arduino.cc/en/Tutorial/Array)

This project looks really nice and i hope you can get it to work!
Could you post your schematics? I bet there are more people here interested in such cool project (and it could also help us to understand what is going on...).

Good luck!
Logged

New England
Offline Offline
Sr. Member
****
Karma: 7
Posts: 295
Natural Semiinductor
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can get dirty sound if you keep the Arduino connected to a PC using a USB cable. Disconnect the USB cable and power the Arduino using the barrel jack. I use type K power coaxial plugs from radio shack. I have also gotten dirty audio music from my custom synthesizer when USB is attached. The noise is silenced when USB is disconnected.
Logged

I am going to get going.

Pages: [1]   Go Up
Jump to: