Programme randomly repeats it self, why??

Hi all,

New to this, thank you for taking the time to look.

I have written a very simple count down programme to let my son start races with his Scalextric. The programme seems to work fine and is activated with a button. The issue is that after it has run though its script is sometime randomly restarts, sometimes in a continues loop or at with quiet periods.

I am assuming that the random nature points to hardware and removing the switch helps but doesn't cure but any advice would be amazing...

Thank you

int switchstate = 0;
int speakerpin = 11;

void setup(){
// declare the LED pins as outputs
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(speakerpin, OUTPUT);

// declare the switch pin as an input
pinMode(2, INPUT);

}

void loop(){
// read the value of the switch
// digitalRead() checks to see if there is voltage
// on the pin or not
switchstate = digitalRead(2);

// check if the Start is pressed.
// if it is, the buttonState is HIGH:
if (switchstate == HIGH) {

// put your main code here, to run repeatedly
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
tone(speakerpin, 700, 100);
delay(100);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
delay(100);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
tone(speakerpin, 700, 100);
delay(100);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
delay(100);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
tone(speakerpin, 700, 100);
delay(100);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
delay(100);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
tone(speakerpin, 700, 100);
delay(100);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
delay(100);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
tone(speakerpin, 700, 100);
delay(100);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
delay(100);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
tone(speakerpin, 700, 100);
delay(100);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
delay(5000);
digitalWrite(3,HIGH);
tone(speakerpin, 500, 500);
delay(1000);
digitalWrite(4,HIGH);
tone(speakerpin, 500, 500);
delay(1000);
digitalWrite(5,HIGH);
tone(speakerpin, 500, 500);
delay(1000);
digitalWrite(6,HIGH);
tone(speakerpin, 500, 500);
delay(1000);
digitalWrite(7,HIGH);
tone(speakerpin, 500, 500);
delay(random(2500, 7000));
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
tone(speakerpin, 900, 1000);
delay(1000);

}
else {
// turn LED off:
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
}
}

Could it be this?

delay(random(2500, 7000));

Oh, try using the code tag button “</>” for posing your code so it looks like above.