Help, Simon Says using a Ultrasonic Sensor?

I’ve recently started using Arduino and I get the basics. One of my projects is to make a Simon Says game using the Ultrasonic Sensor, so far I’ve written down the code. The led light works and the ultrasonic sensor tracks my hand movement perfectly. The thing I left out of the code is the part I’m having a hard time figuring out, that’s the sequence of random patterns you press (in this case your hand movements) to move forward to a different random pattern.

Here’s the Code:

#define trigPin 7
#define echoPin 6
#define led 13
#define led2 12
#define led3 11
#define led4 10
#define led5 9
#define led6 8
#define led7 6
#define led8 5
#define buzzer 3

int sound = 250;


void setup() {
  Serial.begin (9600);

  randomSeed(analogRead(0));
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);
  pinMode(buzzer, OUTPUT);
 
}
const unsigned long measure_distance() {
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);
  pinMode(trigPin, INPUT);
  return pulseIn(trigPin, HIGH);
}

void loop() {
  unsigned long duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = measure_distance();
  distance = (duration/2) / 29.1;
  
  

 Serial.println("Distance: " + distance);

  if (distance <= 40) {
    digitalWrite(led, HIGH);
    sound = 261;
}
  else {
    digitalWrite(led,LOW);
  }
  if (distance < 35) {
      digitalWrite(led2, HIGH);
      sound = 294;
}
  else {
      digitalWrite(led2, LOW);
  }
  if (distance < 30) {
      digitalWrite(led3, HIGH);
      sound = 329;
} 
  else {
    digitalWrite(led3, LOW);
  }
  if (distance < 25) {
    digitalWrite(led4, HIGH);
    sound = 349;
}
  else {
    digitalWrite(led4,LOW);
  }
  if (distance < 20) {
    digitalWrite(led5, HIGH);
    sound = 392;
}
  else {
    digitalWrite(led5,LOW);
  }
  if (distance < 15) {
    digitalWrite(led6, HIGH);
    sound = 440;
}
  else {
    digitalWrite(led6,LOW);
  }

 if (distance < 10) {
    digitalWrite(led7, HIGH);
    sound = 493;
}
  else {
    digitalWrite(led7,LOW);
  }
  if (distance < 5) {
    digitalWrite(led8, HIGH);
    sound = 523;
}
  else {
    digitalWrite(led8,LOW);
  }
  
  if (distance > 40 || distance <= 0){
    Serial.println("Out of range");
    noTone(buzzer);
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
    tone(buzzer, sound);
  }
  delay(300);
}

Capture.PNG

Can anyone help me?

You didn’t use code tags to post your code. It’s important that you use them - if you don’t, the forum software sometimes interprets sequences of characters in the code as directives to format text in some way. It won’t display those sequences, and it will unexpectedly reformat the rest of the text. When that happens, and someone tries to copy your code and paste it into the IDE, it often throws an error, and readers will complain that the code fails to compile. Using code tags also makes it easier to read, and can be copied with a single mouse click.

Unless the sketch is too large, it’s better if you post your code, rather than attach it. When it’s attached, we have to download it, open it in a text editor, then copy it to the IDE. It’s much easier to just view the code in your post.

The code tags make the code look

like this

when posting source code files.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.

When you are finished that, please read this post:

How to use this forum - please read.