Small Speaker Delay

Just messing around with the arduino. I want the speaker to turn on when the last led turns off which it does but its vary delayed. Its delaying itself to turn on when ledPin1 comes on.

int ledPin1 = 1;
int ledPin2 = 2;
int ledPin3 = 3;
int speaker = 6;
int speakerLevel = 0;


void setup() {
  // put your setup code here, to run once:
pinMode (ledPin1 , OUTPUT) ;
pinMode (ledPin2 , OUTPUT) ;
pinMode (ledPin3 , OUTPUT) ;
pinMode (speaker , OUTPUT) ;
}

void loop() {
  // put your main code here, to run repeatedly:
if (digitalRead(ledPin1) == HIGH)
{
 delay (499);
digitalWrite (ledPin2 , HIGH); 
}
else
{
  digitalWrite (ledPin2 , LOW);
}
if (digitalRead(ledPin2) == HIGH)
{
  delay (499) ;
  digitalWrite (ledPin3 , HIGH);
}
else
{ delay (500) ;
  digitalWrite (ledPin3 , LOW);
}
delay (499);
if (digitalRead(ledPin2) == HIGH)
{
  digitalWrite (ledPin1 , LOW);
}
else
{
  delay (499);
  digitalWrite (ledPin1 , HIGH);
}
if (digitalRead(ledPin3) == LOW)
{
analogWrite(speaker , 1);
}
else 
{
  analogWrite (speaker , 0) ;
}
 delay (50);
 analogWrite (speaker , 0);
delay (500);
}

Don't use delay() . During this delay, Arduino is doing nothing. It's not responding to anything. Look at BlinkWithoutDelay example in arduino IDE .

Ah yes. Alright makes sense. I will have to look into that. Thanks

EDIT: I looked into it... close but no. It would get the speaker going off at the correct time but I want it to be going off in reaction to the led. Not in reaction to it just being time for it to go off. Mostly because I have a project planned that will have the leds going off at different times. Such as the first led blinking every X amount of seconds and the next blinking every X amount of seconds with the third only turning off when a button is pressed.

The mills would make it work so long as all three leds went off at exactly the same time for forever.

The mills would make it work so long as all three leds went off at exactly the same time for forever.

This doesn't make sense. The millis() function doesn't care what else is happening. If you are having trouble using millis(), then the problem is in the code you didn't post. Only you can fix that.

HeroesNeverQuit: Just messing around with the arduino. I want the speaker to turn on when the last led turns off which it does but its vary delayed. Its delaying itself to turn on when ledPin1 comes on.

int ledPin1 = 1;
int ledPin2 = 2;
int ledPin3 = 3;
int speaker = 6;
int speakerLevel = 0;

void setup() {   // put your setup code here, to run once: pinMode (ledPin1 , OUTPUT) ; pinMode (ledPin2 , OUTPUT) ; pinMode (ledPin3 , OUTPUT) ; pinMode (speaker , OUTPUT) ; }

void loop() {   // put your main code here, to run repeatedly: if (digitalRead(ledPin1) == HIGH) { delay (499); digitalWrite (ledPin2 , HIGH); } else {   digitalWrite (ledPin2 , LOW); } if (digitalRead(ledPin2) == HIGH) {   delay (499) ;   digitalWrite (ledPin3 , HIGH); } else { delay (500) ;   digitalWrite (ledPin3 , LOW); } delay (499); if (digitalRead(ledPin2) == HIGH) {   digitalWrite (ledPin1 , LOW); } else {   delay (499);   digitalWrite (ledPin1 , HIGH); } if (digitalRead(ledPin3) == LOW) { analogWrite(speaker , 1); } else {   analogWrite (speaker , 0) ; } delay (50); analogWrite (speaker , 0); delay (500); }

Take a closer look at your code setup and sequence. Here are some points to examine: All pins are OUTPUTS. Than you read / input their values as what ?? The if sequence has build in delays - are they in right sequence , place? The speaker always turns off for 500 ms before anything else will or (you) want to be processed. You are really doing a great job "messing around with Arduino" and getting correct (messed up) results.