Projeto Harpa laser

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

O teu tópico foi movido para a secção em português do fórum. Por favor, utilize o idioma inglês nas secções em inglês do fórum.

Seu código não é compilado porque você não define "pinS0", "pinS1" ou "pinS2" ou "analogPin0"

Você deve usar variáveis mais legíveis. "C" está em toda parte, no entanto, "Cnote" é compreensível.

Para não perturbar outros pinos usando PORTB, use o operador bit a bit OR "|"

  // PORTB = 0b00000000; //Data flow - X0 // NO
  PORTB |= 0b11111000; //Data flow - X0 // YES

A escala musical é de "A" a "G", então "H" não existe.

Isso é compilado.

int wait = 10;

int analogPin0 = A0;
// int Hpin = A0;  // define your analog pins here
int Cpin = A1;
int Dpin = A2;
int Epin = A4;
int Fpin = A3;
int Gpin = A5;
int Apin = A6;

const int zInput = 7;
int SPKpin = 3;  // define pin for your speaker
int pinS0 = 4, pinS1 = 5, pinS2 = 6, Sensor0, Sensor1;

int Cnote, Dnote, Enote, Fnote, Gnote; //, nc, nd, ne, nf, ng, na; // define note

void setup() {
  Serial.begin(9600);  // serial monitor
  
  pinMode(zInput, OUTPUT); //Inhibit set to off
  digitalWrite(zInput, LOW);

  DDRB &= 0b00000111;  //PORT 8,9,10 as output
  PORTB |= 0b11111000; //PORT 8,9,10 set to LOW
  
  // pinMode(Hpin, INPUT);
  pinMode(Cpin, INPUT);
  pinMode(Dpin, INPUT);
  pinMode(Epin, INPUT);
  pinMode(Fpin, INPUT);
  pinMode(Gpin, INPUT);
  pinMode(Apin, INPUT);

  pinMode(SPKpin, 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 (SPKpin, 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

  Cnote = analogRead(Cpin);
  if (Cnote < 900)
    tone (SPKpin, 130, 20);// Ré 294

  Dnote = analogRead(Dpin);
  if (Dnote < 900)
    tone (SPKpin, 147, 20);// Mi 330

  Enote = analogRead(Epin);
  if (Enote < 900)
    tone (SPKpin, 198, 20);// Sol 396

  Fnote = analogRead(Fpin);
  if (Fnote < 900)
    tone (SPKpin, 165, 20);// Lá 440

  Gnote = analogRead(Gpin);
  if (Gnote < 900)
    tone (SPKpin, 260, 20);

  //na= analogRead(Apin);
  //if (na < 700)
  //tone (SPKpin, 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
  Sensor0 =  analogRead(A0);//read data from X0
  // int Sensor0 =  analogRead(A0);//read data from X0
  Serial.print(" Sensor0: ");
  Serial.print(Sensor0);
  if (Sensor0 < 900)
    tone (SPKpin, 440, 20);
  delay(wait);

  PORTB |= 0b00000001; //Data flow - X1
  Sensor1 =  analogRead(A0); //read data from X1
  // int Sensor1 =  analogRead(A0); //read data from X1
  Serial.print("  Sensor1: ");
  Serial.println(Sensor1);
  if (Sensor1 < 900)
    tone (SPKpin, 1046, 20);// Ré 294
  delay(wait);
}

Obrigado pela dica da PORTB mas o problema persiste. Continua a emitir os sons do mux sobrepostos aos do arduino. Obrigado pela atenção

Para conhecimento geral a nota H existe na nomenclatura alemã, é o nosso si (B).

Eu não sei como separar o áudio. Eu não sobrepunha era possível no Arduino. Desculpa.

Muito fresco. Desculpe por adicionar informações ruins. Estou ciente de que a Ásia também tem uma escala diferente.

Rogerio, alem da função TONE para executar as notas, aconselho que você use a função noTONE() para finalizar a execução de notas e limpar o buffer, o noTone tambem deve ser executado ao iniciar uma nova nota, caso se deseje cancelar a anterior, senão da problema.

Com a função noTone (antes e depois) as notas do arduino ficam alteradas (graves) e as do cd 4051 continuam sobrepostas. Obrigado pela atenção.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.