I have a code that I'm trying to make something like a selection menu with a potentiometer and LED's and when the potetiometer goes from one another the Buzzer makes a short sound and stops. At the moment with my code it is making a short sound but then goes on to make another sound and it goes on in a loop. Any help?? ( I've only done the first LED/ selection with the sound)
const int POT_PIN = 1;
const int LED_1 = 2;
const int LED_2 = 3;
const int LED_3 = 4;
const int LED_4 = 5;
const int SWITCH_PIN = 6;
const int BUZZER_PIN = 7;
int s;
int sound;
void setup() {
Serial.begin(9600);
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(LED_3, OUTPUT);
pinMode(LED_4, OUTPUT);
pinMode(SWITCH_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
}
int pot;
void loop() {
pot = analogRead(POT_PIN);
sound = analogRead(BUZZER_PIN);
s = digitalRead(SWITCH_PIN);
Serial.println(pot);
Serial.println(s);
if (pot <= 700 && pot >= 600 && s == 1) {
digitalWrite(LED_1, HIGH);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, LOW);
digitalWrite(LED_4, LOW);
tone(BUZZER_PIN, 1000);
delay(500);
noTone(BUZZER_PIN);
delay(500);
} else if (pot <= 700 && pot >= 600 && s == 0) {
for (int i = 1; i <= 10; i++) {
digitalWrite(LED_1, HIGH);
delay(500);
digitalWrite(LED_1, LOW);
delay(500);
}
} else if (pot <= 599 && pot >= 450 && s == 1) {
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, HIGH);
digitalWrite(LED_3, LOW);
digitalWrite(LED_4, LOW);
} else if (pot <= 599 && pot >= 450 && s == 0) {
for (int i = 1; i <= 6; i++) {
digitalWrite(LED_2, HIGH);
delay(500);
digitalWrite(LED_2, LOW);
delay(500);
}
} else if (pot <= 449 && pot >= 300 && s == 1) {
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, HIGH);
digitalWrite(LED_4, LOW);
} else if (pot <= 449 && pot >= 300 && s == 0) {
for (int i = 1; i <= 4; i++) {
digitalWrite(LED_3, HIGH);
delay(500);
digitalWrite(LED_3, LOW);
delay(500);
}
} else {
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, LOW);
digitalWrite(LED_4, HIGH);
}
}
// Led 1 = Chicken
// Led 2 = Vegetables
// Led 3 = Coffee/Soup
// Led 4 = No input
works but it beeps twice not once. How do I fix that? And when it beeps I can't just use the potentiometer to go from one another because it pauses then beeps and then I can switch again
Ok. And so with this code can I just copy and paste into all the others to make the same sound into the others? Even in the for loops but I can maybe change the frequency? To make a different sound when selected?
Your code has a lot of delay(), and it may be that at the moment you press the switch the processing is in some delay() and you don't feel that the switch has been pressed.
Perhaps using interrupt, you can resolve the error.
But the best thing would be, instead of using delay(), to use milliils().
I know that using millis() is complicated for some people, but it is a much "healthier" solution than using delay().
I have a code that I'm trying to make something like a selection menu with a potentiometer and LED's and when the potentiometer goes from one another the Buzzer makes a short sound and stops. At the moment with my code it is making a short sound which is how I want it but then with the same values on the potentiometer when the pull up switch is clicked I don't get any sound which I need. (I've only done the first LED/ selection with the sound)
const int POT_PIN = 1;
const int LED_1 = 2;
const int LED_2 = 3;
const int LED_3 = 4;
const int LED_4 = 5;
const int SWITCH_PIN = 6;
const int BUZZER_PIN = 7;
int s;
int sound;
void setup() {
Serial.begin(9600);
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(LED_3, OUTPUT);
pinMode(LED_4, OUTPUT);
pinMode(SWITCH_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
}
int pot;
int count = 0;
void loop() {
pot = analogRead(POT_PIN);
sound = analogRead(BUZZER_PIN);
s = digitalRead(SWITCH_PIN);
Serial.println(pot);
Serial.println(s);
if (pot < 1000) count = 0;
if (pot <= 700 && pot >= 600 && s == 1) {
digitalWrite(LED_1, HIGH);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, LOW);
digitalWrite(LED_4, LOW);
if (count < 1) {
tone(BUZZER_PIN, 1000);
delay(500);
noTone(BUZZER_PIN);
delay(500);
count++;
}
} else if (pot <= 700 && pot >= 600 && s == 0) {
if (count < 1) {
tone(BUZZER_PIN,2000);
delay(500);
noTone(BUZZER_PIN);
delay(500);
count++;
}
for (int i = 1; i <= 10; i++) {
digitalWrite(LED_1, HIGH);
delay(500);
digitalWrite(LED_1, LOW);
delay(500);
}
} else if (pot <= 599 && pot >= 450 && s == 1) {
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, HIGH);
digitalWrite(LED_3, LOW);
digitalWrite(LED_4, LOW);
} else if (pot <= 599 && pot >= 450 && s == 0) {
for (int i = 1; i <= 6; i++) {
digitalWrite(LED_2, HIGH);
delay(500);
digitalWrite(LED_2, LOW);
delay(500);
}
} else if (pot <= 449 && pot >= 300 && s == 1) {
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, HIGH);
digitalWrite(LED_4, LOW);
} else if (pot <= 449 && pot >= 300 && s == 0) {
for (int i = 1; i <= 4; i++) {
digitalWrite(LED_3, HIGH);
delay(500);
digitalWrite(LED_3, LOW);
delay(500);
}
} else {
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, LOW);
digitalWrite(LED_4, HIGH);
}
}
// Led 1 = Chicken
// Led 2 = Vegetables
// Led 3 = Coffee/Soup
// Led 4 = No input
Right now with this code it's working up to a point that when I use the potentiometer to go to the ranges of LED_1 it makes a sound but it keeps on repeating till I either change the potentiometer range or I press the button. But after I press the button on the pullup switch it goes back to making the sound repetitively. I only need it to make the repetitive sound once when from another potentiometer range goes to it to make a sound that indicates that you switched from on another. Any help???
Yes I realised. But I can't make it like that. Take into for instance you have a knob on a microwave and you turn the potentiometer (knob) onto the selected cooking option (The 3 LEDs) and you click start. Depending on what cooking selection you picked it blinks an amount of times to show that it's cooking. My problem is that I have another feature of the sound where when you switch from one cooking option to another it gives out a sound and when you press the button on a selected cooking option it gives out another type of sound. This is where I'm not able to do it. I have been working on this for quite some time now and I'm getting kind of agitated with it. If you know how to help please do so.