Piezo and LED

Hello,

I'm having trouble getting to work sound and lights together

the following is our code
(please bear with me I am very new to this)

#include "pitches.h"

int lightPin= A2;

int green= 13;
int red= 9;

int melody[]={mi,mi,fa,so,so,fa,mi,re,do,do,re,mi,mi,re,re,mi,mi,fa,so,so,fa,mi,re,do,do,re,mi,re,do,do,
re,re,mi,do,re,mi,fa,mi,do,re,mi,fa,mi,re,do,re,mi,mi,fa,so,so,fa,mi,re,do,do,re,mi,re,do,do};

int noteDurations[]={4,4,4,4,4,4,4,4,4,4,4,4,4,8,2,4,4,4,4,4,4,4,4,4,4,4,4,4,8,2,4,4,4,4,4,8,8,4,4,4,8,8,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,8,2};

void setup()
{
pinMode(green,OUTPUT);
pinMode(red,OUTPUT);

for (int thisNote = 0; thisNote < 62; thisNote++)
{
int noteDuration = 1000/noteDurations[thisNote];
tone(A4, melody[thisNote],noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(A4);
}
}

void loop()
{
int lightLevel= analogRead(lightPin);
lightLevel= map (lightLevel, 0 ,900, 0 ,255);

if (lightLevel > 0 && lightLevel < 255)
{
digitalWrite(green, LOW);
digitalWrite(red, HIGH);

}
else
{
digitalWrite(green, HIGH);
digitalWrite(red, LOW);
}
}

I configured that when I was using the transparent mode. Even in the API mode I have to config this?

You do the sounds in setup(), and then when that is done, loop() runs and does the lights. Of course, they are not going to happen together.

Describe what you want to happen, without worrying about how.

basically this is only 2/3 of my program. I want the sounds to play when a light sensor is covered, which will turn the light green and some other lights also.

Turning a light on or off is a nearly instantaneous event. Detecting light level is a nearly instantaneous event. Playing music is NOT a nearly instantaneous event.

You could START the music playing, and change notes periodically, using the blink without delay example for inspiration. When should it stop, though?

well, there is a light sensor which acts like a switch (purpose on our model is a kind of "scanner") and it would activate some music and keys. It stops when the sensor detects light

#include "pitches.h"

int lightPin= A2;    

int green= 13;    
int red= 9;     

int key1= 53;
int key2= 51;
int key3= 49;
int key4= 47;
int key5= 45;
int key6= 43;
int key7= 41;
int key8= 39;
int key9= 37;

int melody[]={mi,mi,fa,so,so,fa,mi,re,do,do,re,mi,mi,re,re,mi,mi,fa,so,so,fa,mi,re,do,do,re,mi,re,do,do,
re,re,mi,do,re,mi,fa,mi,do,re,mi,fa,mi,re,do,re,mi,mi,fa,so,so,fa,mi,re,do,do,re,mi,re,do,do}; //hohoho

int noteDurations[]={4,4,4,4,4,4,4,4,4,4,4,4,4,8,2,4,4,4,4,4,4,4,4,4,4,4,4,4,8,2,4,4,4,4,4,8,8,4,4,4,8,8,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,8,2}; //huhuhu

void setup()
{
  pinMode(green,OUTPUT);
  pinMode(red,OUTPUT);
  pinMode(53, OUTPUT);
  
  pinMode(51, OUTPUT);
  pinMode(49, OUTPUT);
  pinMode(47, OUTPUT);
  pinMode(45, OUTPUT);
  pinMode(43, OUTPUT);
  pinMode(41, OUTPUT);
  pinMode(39, OUTPUT);
  pinMode(37, OUTPUT);
  
  for (int thisNote = 0; thisNote < 63; thisNote++) 
{
   int noteDuration = 1000/noteDurations[thisNote];
   tone(A4, melody[thisNote],noteDuration);
   int pauseBetweenNotes = noteDuration * 1.30;
   delay(pauseBetweenNotes);
   noTone(A4);
}
}


void loop()
{
  int lightLevel= analogRead(lightPin);
  lightLevel= map (lightLevel, 0 ,900, 0 ,255);
  
  if (lightLevel > 0 && lightLevel < 255)
 {
  digitalWrite(green, LOW);
  digitalWrite(red, HIGH);
  
  digitalWrite(key3, HIGH);
  delay(250);
  digitalWrite(key3, LOW);
  digitalWrite(key3, HIGH);
  delay(250);
  digitalWrite(key3, LOW);
  digitalWrite(key4, HIGH);
  delay(250);
  digitalWrite(key4, LOW);
  digitalWrite(key5, HIGH);
  delay(250);
  digitalWrite(key5, LOW);
  digitalWrite(key5, HIGH);
  delay(250);
  digitalWrite(key5, LOW);
  digitalWrite(key4, HIGH);
  delay(250);
  digitalWrite(key4, LOW);
  
  digitalWrite(key3, HIGH);
  delay(250);
  digitalWrite(key3, LOW);
  digitalWrite(key2, HIGH);
  delay(250);
  digitalWrite(key2, LOW);
  digitalWrite(key1, HIGH);
  delay(250);
  digitalWrite(key1, LOW);
  digitalWrite(key1, HIGH);
  delay(250);
  digitalWrite(key1, LOW);
  digitalWrite(key2, HIGH);
  delay(250);
  digitalWrite(key2, LOW);
  digitalWrite(key3, HIGH);
  delay(250);
  digitalWrite(key3, LOW);
  digitalWrite(key3, HIGH);
  delay(250);
  digitalWrite(key3, LOW);
  
  digitalWrite(key2, HIGH);
  delay(125);
  digitalWrite(key2, LOW);
  digitalWrite(key2, HIGH);
  delay(500);
  digitalWrite(key2, LOW);
  digitalWrite(key3, HIGH);
  delay(250);
  digitalWrite(key3, LOW);
  digitalWrite(key3, HIGH);
  delay(250);
  digitalWrite(key3, LOW);
  digitalWrite(key4, HIGH);
  delay(250);
  digitalWrite(key4, LOW);
  digitalWrite(key5, HIGH);
  delay(250);
  digitalWrite(key5, LOW);
  digitalWrite(key5, HIGH);
  delay(250);
  digitalWrite(key5, LOW);
  
  digitalWrite(key4, HIGH);
  delay(250);
  digitalWrite(key4, LOW);
  digitalWrite(key3, HIGH);
  delay(250);
  digitalWrite(key3, LOW);
  digitalWrite(key2, HIGH);
  delay(250);
  digitalWrite(key2, LOW);
  digitalWrite(key1, HIGH);
  delay(250);
  digitalWrite(key1, LOW);
  digitalWrite(key1, HIGH);
  delay(250);
  digitalWrite(key1, LOW);
  digitalWrite(key2, HIGH);
  delay(250);
  digitalWrite(key2, LOW);
  
  digitalWrite(key3, HIGH);
  delay(250);
  digitalWrite(key3, LOW);
  digitalWrite(key2, HIGH);
  delay(250);
  digitalWrite(key2, LOW);
  digitalWrite(key1, HIGH);
  delay(125);
  digitalWrite(key1, LOW);
  digitalWrite(key1, HIGH);
  delay(500);
  digitalWrite(key1, LOW);
  digitalWrite(key2, HIGH);
  delay(250);
  digitalWrite(key2, LOW);
  digitalWrite(key2, HIGH);
  delay(250);
  digitalWrite(key2, LOW);
  digitalWrite(key3, HIGH);
  delay(250);
  digitalWrite(key3, LOW);
  
  digitalWrite(key1, HIGH);
  delay(250);
  digitalWrite(key1, LOW);
  digitalWrite(key2, HIGH);
  delay(250);
  digitalWrite(key2, LOW);
  digitalWrite(key3, HIGH);
  delay(125);
  digitalWrite(key3, LOW);
  digitalWrite(key4, HIGH);
  delay(125);
  digitalWrite(key4, LOW);
  digitalWrite(key3, HIGH);
  delay(250);
  digitalWrite(key3, LOW);
  digitalWrite(key1, HIGH);
  delay(250);
  digitalWrite(key1, LOW);
  digitalWrite(key2, HIGH);
  delay(250);
  digitalWrite(key2, LOW);
  digitalWrite(key3, HIGH);
  delay(125);
  digitalWrite(key3, LOW);
  digitalWrite(key4, HIGH);
  delay(125);
  digitalWrite(key4, LOW);
  
  digitalWrite(key3, HIGH);
  delay(250);
  digitalWrite(key3, LOW);
  digitalWrite(key2, HIGH);
  delay(250);
  digitalWrite(key2, LOW);
  digitalWrite(key1, HIGH);
  delay(250);
  digitalWrite(key1, LOW);
  digitalWrite(key2, HIGH);
  delay(250);
  digitalWrite(key2, LOW);
  digitalWrite(key3, HIGH);
  delay(250);
  digitalWrite(key3, LOW);
  
  digitalWrite(key3, HIGH);
  delay(250);
  digitalWrite(key3, LOW);
  digitalWrite(key4, HIGH);
  delay(250);
  digitalWrite(key4, LOW);
  digitalWrite(key5, HIGH);
  delay(250);
  digitalWrite(key5, LOW);
  digitalWrite(key5, HIGH);
  delay(250);
  digitalWrite(key5, LOW);
  digitalWrite(key4, HIGH);
  delay(250);
  digitalWrite(key4, LOW);
  digitalWrite(key3, HIGH);
  delay(250);
  digitalWrite(key3, LOW);
  digitalWrite(key2, HIGH);
  delay(250);
  digitalWrite(key2, LOW);
  
  digitalWrite(key1, HIGH);
  delay(250);
  digitalWrite(key1, LOW);
  digitalWrite(key1, HIGH);
  delay(250);
  digitalWrite(key1, LOW);
  digitalWrite(key2, HIGH);
  delay(250);
  digitalWrite(key2, LOW);
  digitalWrite(key3, HIGH);
  delay(250);
  digitalWrite(key3, LOW);
  digitalWrite(key2, HIGH);
  delay(250);
  digitalWrite(key2, LOW);
  digitalWrite(key1, HIGH);
  delay(125);
  digitalWrite(key1, LOW);
  digitalWrite(key1, HIGH);
  delay(500);
  digitalWrite(key1, LOW);
  
 } 
 else
 {
  digitalWrite(green, HIGH);
  digitalWrite(red, LOW);
  
   digitalWrite(key3, LOW);
  delay(250);
  digitalWrite(key3, LOW);
  delay(250);
  digitalWrite(key4, LOW);
  delay(250);
  digitalWrite(key5, LOW);
  delay(250);
  digitalWrite(key5, LOW);
  delay(250);
  digitalWrite(key4, LOW);
  delay(250);
  
  digitalWrite(key3, LOW);
  delay(250);
  digitalWrite(key2, LOW);
  delay(250);
  digitalWrite(key1, LOW);
  delay(250);
  digitalWrite(key1, LOW);
  delay(250);
  digitalWrite(key2, LOW);
  delay(250);
  digitalWrite(key3, LOW);
  delay(250);
  digitalWrite(key3, LOW);
  delay(250);
  
  digitalWrite(key2, LOW);
  delay(250);
  digitalWrite(key2, LOW);
  delay(250);
  digitalWrite(key3, LOW);
  delay(250);
  digitalWrite(key3, LOW);
  delay(250);
  digitalWrite(key4, LOW);
  delay(250);
  digitalWrite(key5, LOW);
  delay(250);
  digitalWrite(key5, LOW);
  delay(250);
  
  digitalWrite(key4, LOW);
  delay(250);
  digitalWrite(key3, LOW);
  delay(250);
  digitalWrite(key2, LOW);
  delay(250);
  digitalWrite(key1, LOW);
  delay(250);
  digitalWrite(key1, LOW);
  delay(250);
  digitalWrite(key2, LOW);
  delay(250);
  
  digitalWrite(key3, LOW);
  delay(250);
  digitalWrite(key2, LOW);
  delay(250);
  digitalWrite(key1, LOW);
  delay(250);
  digitalWrite(key1, LOW);
  delay(250);
  digitalWrite(key2, LOW);
  delay(250);
  digitalWrite(key2, LOW);
  delay(250);
  digitalWrite(key3, LOW);
  delay(250);
  
  digitalWrite(key1, LOW);
  delay(250);
  digitalWrite(key2, LOW);
  delay(250);
  digitalWrite(key3, LOW);
  delay(250);
  digitalWrite(key4, LOW);
  delay(250);
  digitalWrite(key3, LOW);
  delay(250);
  digitalWrite(key1, LOW);
  delay(250);
  digitalWrite(key2, LOW);
  delay(250);
  digitalWrite(key3, LOW);
  delay(250);
  digitalWrite(key4, LOW);
  delay(250);
  
  digitalWrite(key3, LOW);
  delay(250);
  digitalWrite(key2, LOW);
  delay(250);
  digitalWrite(key1, LOW);
  delay(250);
  digitalWrite(key2, LOW);
  delay(250);
  digitalWrite(key3, LOW);
  delay(250);
  
  digitalWrite(key3, LOW);
  delay(250);
  digitalWrite(key4, LOW);
  delay(250);
  digitalWrite(key5, LOW);
  delay(250);
  digitalWrite(key5, LOW);
  delay(250);
  digitalWrite(key4, LOW);
  delay(250);
  digitalWrite(key3, LOW);
  delay(250);
  digitalWrite(key2, LOW);
  delay(250);
  
  digitalWrite(key1, LOW);
  delay(250);
  digitalWrite(key1, LOW);
  delay(250);
  digitalWrite(key2, LOW);
  delay(250);
  digitalWrite(key3, LOW);
  delay(250);
  digitalWrite(key2, LOW);
  delay(250);
  digitalWrite(key1, LOW);
  delay(250);
  digitalWrite(key1, LOW);
  delay(250);
 }
}

please i really need help

How to use this forum

Point 13. Don't bump after 30 minutes, thanks.

please i really need help

When is your homework due?

I think you are going to have to start writing non-blocking code. That is, get rid of "delay".

I'm a little surprised Google hasn't turned up something about playing music, and flashing lights at the same time. Is it working where you are?