Hey guys, this post is a repost. I asked about this problem before but my previous post was very vague to many people. I edited the post eventually, but i think its better if I repost it. Let me know if there are any more questions and I’ll try my best to clear things up!
The code I provided IS function besides at the button part which is why I asked for help, no matter what I try I can't make it work the way I want.
Okay so I wanna make little device that makes a noice depending on how on object is away from the supersonic sensor, which you could probably see from the code, I want the noise to change depending on which button you pressed last. So for example if I pressed knop1 it would change the pitch it plays for all distances.
If there are any more questions let me know in the comments and i'll try helping out sooner next time!
// Ultrasonic pins
const int trigPin = 9;
const int echoPin = 10;
// LED pins
const int led1 = 5;
const int led2 = 4;
const int led3 = 2;
const int led4 = 6;
// Speaker pin
const int speakerPin = 3;
// Buttons
const int button1 = 7;
const int button2 = 8;
const int button3 = 12;
bool b1 = false;
bool b2 = false;
bool b3 = false;
#include "pitches.h"
long duration;
int distance;
int selectedNote = NOTE_C5;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
if (distance <= 5) {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
if (digitalRead(button1)) {
b1 = true;
}
if (digitalRead(button2)) {
b2 = true;
selectedNote = NOTE_D4;
Serial.println("knop2");
}
if (digitalRead(button3)) {
b3 = true;
selectedNote = NOTE_E4;
Serial.println("knop3");
}
else{
selectedNote = NOTE_B4;
}
if(b1 == true){
selectedNote = NOTE_C4;
Serial.println("knop1");
}
tone(speakerPin, selectedNote, 100);
}
else if (distance <= 10) {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
if (digitalRead(button1)) {
selectedNote = NOTE_C5;
Serial.println("knop1");
}
if (digitalRead(button2)) {
selectedNote = NOTE_D5;
Serial.println("knop2");
}
if (digitalRead(button3)) {
selectedNote = NOTE_E5;
Serial.println("knop3");
}
else{
selectedNote = NOTE_B5;
}
tone(speakerPin, selectedNote, 100);
}
else if (distance <= 15) {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
if (digitalRead(button1)) {
selectedNote = NOTE_C6;
Serial.println("knop1");
}
if (digitalRead(button2)) {
selectedNote = NOTE_D6;
Serial.println("knop2");
}
if (digitalRead(button3)) {
selectedNote = NOTE_E6;
Serial.println("knop3");
}
else{
selectedNote = NOTE_B6;
}
tone(speakerPin, selectedNote, 100);
}
else if (distance <= 20) {
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
if (digitalRead(button1)) {
selectedNote = NOTE_C7;
Serial.println("knop1");
}
if (digitalRead(button2)) {
selectedNote = NOTE_D7;
Serial.println("knop2");
}
if (digitalRead(button3)) {
selectedNote = NOTE_E7;
Serial.println("knop3");
}
else{
selectedNote = NOTE_B7;
}
tone(speakerPin, selectedNote, 100);
}
else {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
noTone(speakerPin);
}
delay(100);
}
I added the entire code this time because thats what most people told me to do