Olá. Estou a criar uma harpa laser. Já fiz alguns projetos como rega com sensores de nível de água e de humidade e seguidor solar com dois eixos. A placa que estou a usar é arduino uno com o multplex cd 4051. O laser incide no ldr e quando é interrompido é imitido um som. Os sons controlados pelo arduino funcionam na perfeição. Quando acrescento um som isolado do multiplex também funciona. O problema surge a partir do 2º som e restantes (5 do arduino e 8 do multiplex). Os sons acumulam-se em vez de tocar um de cada vez, apesar de apresentar os valores corretos no monitor serial.
int wait= 10;
//int H = A0; // define your analog pins here
int C = A1;
int D = A2;
int E = A4;
int F = A3;
int G = A5;
//int A = A6;
const int zInput = 7;
int SPK = 3; // define pin for your speaker
int nc, nd, ne, nf, ng; //, nc, nd, ne, nf, ng, na; // define note
void setup() {
Serial.begin(9600); // serial monitor
pinMode(7,OUTPUT); //Inhibit set to off
digitalWrite(7,LOW);
DDRB = 0b00000111; //PORT 8,9,10 as output
PORTB = 0b00000000; //PORT 8,9,10 set to LOW
pinMode(A0,INPUT);
//pinMode(H, INPUT); // define pins as input
pinMode(C, INPUT);
pinMode(D, INPUT);
pinMode(E, INPUT);
pinMode(F, INPUT);
pinMode(G, INPUT);
//pinMode(A, INPUT);
pinMode(SPK, OUTPUT); // define speaker as output
}
void loop() {
//nh= analogRead(H); // analog read
//if (nh < 950) // if laser beam hit resistor this value is around 1 000 - 1 100.
// if you cover the laser beam this value goes down to 300
//tone (SPK, 130, 20); // 123 is sound frequency (you can change it and play wit it) 20 is a length of the sound (261 - D0)
//Serial.println(nh); // this funcion allows you to check on serial monitor analog values
nc= analogRead(C);
if (nc < 900)
tone (SPK, 130, 20);// Ré 294
nd= analogRead(D);
if (nd < 900)
tone (SPK, 147, 20);// Mi 330
ne= analogRead(E);
if (ne < 900)
tone (SPK, 198, 20);// Sol 396
nf= analogRead(F);
if (nf < 900)
tone (SPK, 165, 20);// Lá 440
ng= analogRead(G);
if (ng < 900)
tone (SPK, 260, 20);
//na= analogRead(A);
//if (na < 700)
//tone (SPK, 220, 20);
//Only reading input 0 of the multiplexer
//digitalWrite(pinS2, LOW);
//digitalWrite(pinS1, LOW);
//digitalWrite(pinS0, LOW);
//Serial.print(analogRead(analogPin0));
//Serial.print(" ");
//Only reading input 1 of the multiplexer
//digitalWrite(pinS2, LOW);
//digitalWrite(pinS1, LOW);
//digitalWrite(pinS0, HIGH);
//Serial.print(analogRead(analogPin0));
//Serial.print(" ");
PORTB=0b00000000; //Data flow - X0
int Sensor0 = analogRead(A0);//read data from X0
Serial.println("Sensor0");
Serial.println(Sensor0);
if (Sensor0 < 900)
tone (SPK, 440,20);
delay(wait);
PORTB=0b00000001; //Data flow - X1
int Sensor1 = analogRead(A0); //read data from X1
Serial.println("Sensor1");
Serial.println(Sensor1);
if (Sensor1 < 900)
tone (SPK, 1046, 20);// Ré 294
delay(wait);
Não coloquei os restantes sons do multiplex porque o problema começa no 2º som.
Obrigado pela atenção