Controlling of buzzer with LDR and potential meter

Greetings all, I am currently writing a program that combines the 7segment display, potential meter, light dependent resistor, a buzzer and also a servo within in.

I am new to the whole Arduino device and is trying to make it such that on even numbers of my 7segment display, the buzzer sound will be controlled by the potential meter. When odd numbers are reflected on the display, the LDR will then be used to control the buzzer noise.

The below program code when used, allows me to control the buzzer sound with my potential meter at number ‘0’ but when the button is pressed, nothing happens. It does not break from the while loop and I am unable to proceeded on to the odd number loop.

I have tried using the IF loop but the buzzer sound only takes on the initial value of the potential Meter. Any changes made to the potential meter does not change the buzzer sound. The sound only changes when the potential meter is changed before an even number is reached.

Any suggestions or solutions will be greatly appreciated.

#include <Servo.h>

float potenValue = 0;
float ldrValue = 0;
int freq = 0;
int buttonPushCounter = -1;
int buttonState = 0;
int lastButtonstate = 0;
int i = 0;
int even[] = {0, 2, 4, 6, 8};
int odd[] = {1, 3, 5, 7, 9};
Servo servo_3;

void setup() {
 pinMode(11, OUTPUT); // LED
 pinMode(13, INPUT_PULLUP); // PushButton
 pinMode(2, OUTPUT); // 7Segment G
 pinMode(3, OUTPUT); // 7Segment F
 pinMode(4, OUTPUT); // 7Segment A
 pinMode(5, OUTPUT); // 7Segment B
 pinMode(8, OUTPUT); // 7Segment E
 pinMode(9, OUTPUT); // 7Segment D
 pinMode(10, OUTPUT); // 7Segment C
 pinMode(12, OUTPUT); // EletricSpeaker
 pinMode(A0, OUTPUT); //LDR
 pinMode(A4, OUTPUT); //PotentioMeter
 Serial.begin (9600);
 servo_3.attach(7);
}

void potentBuzzer()
{
 potenValue = analogRead(A4);
 Serial.print(potenValue);
 Serial.println("\n");
 freq = map(potenValue, 0, 1023, 100, 5000);
 tone(12, freq);
}

void ldrBuzzer()
{
 ldrValue = analogRead(A0);
 Serial.print(ldrValue);
 Serial.println("\n");
 freq = map(ldrValue, 0, 1023, 100, 5000);
 tone(12, freq);
}


void displayNumber()
{
 if (buttonPushCounter == 0) {
   digitalWrite(2, LOW);
   digitalWrite(3, HIGH);
   digitalWrite(4, HIGH);
   digitalWrite(5, HIGH);
   digitalWrite(8, HIGH);
   digitalWrite(9, HIGH);
   digitalWrite(10, HIGH);
   servo_3.write(15);
 } else if (buttonPushCounter == 1) {
   digitalWrite(2, LOW);
   digitalWrite(3, LOW);
   digitalWrite(4, LOW);
   digitalWrite(5, HIGH);
   digitalWrite(8, LOW);
   digitalWrite(9, LOW);
   digitalWrite(10, HIGH);
   servo_3.write(25);
 } else if (buttonPushCounter == 2) {
   digitalWrite(2, HIGH);
   digitalWrite(3, LOW);
   digitalWrite(4, HIGH);
   digitalWrite(5, HIGH);
   digitalWrite(8, HIGH);
   digitalWrite(9, HIGH);
   digitalWrite(10, LOW);
   servo_3.write(35);
 } else if (buttonPushCounter == 3)
 {
   digitalWrite(2, HIGH);
   digitalWrite(3, LOW);
   digitalWrite(4, HIGH);
   digitalWrite(5, HIGH);
   digitalWrite(8, LOW);
   digitalWrite(9, HIGH);
   digitalWrite(10, HIGH);
   servo_3.write(45);
 } else if (buttonPushCounter == 4)
 {
   digitalWrite(2, HIGH);
   digitalWrite(3, HIGH);
   digitalWrite(4, LOW);
   digitalWrite(5, HIGH);
   digitalWrite(8, LOW);
   digitalWrite(9, LOW);
   digitalWrite(10, HIGH);
   servo_3.write(55);
 } else if (buttonPushCounter == 5)
 {
   digitalWrite(2, HIGH);
   digitalWrite(3, HIGH);
   digitalWrite(4, HIGH);
   digitalWrite(5, LOW);
   digitalWrite(8, LOW);
   digitalWrite(9, HIGH);
   digitalWrite(10, HIGH);
   servo_3.write(65);
 } else if (buttonPushCounter == 6)
 {
   digitalWrite(2, HIGH);
   digitalWrite(3, HIGH);
   digitalWrite(4, HIGH);
   digitalWrite(5, LOW);
   digitalWrite(8, HIGH);
   digitalWrite(9, HIGH);
   digitalWrite(10, HIGH);
   servo_3.write(75);
 } else if (buttonPushCounter == 7)
 {
   digitalWrite(2, LOW);
   digitalWrite(3, LOW);
   digitalWrite(4, HIGH);
   digitalWrite(5, HIGH);
   digitalWrite(8, LOW);
   digitalWrite(9, LOW);
   digitalWrite(10, HIGH);
   servo_3.write(85);
 } else if (buttonPushCounter == 8)
 {
   digitalWrite(2, HIGH);
   digitalWrite(3, HIGH);
   digitalWrite(4, HIGH);
   digitalWrite(5, HIGH);
   digitalWrite(8, HIGH);
   digitalWrite(9, HIGH);
   digitalWrite(10, HIGH);
   servo_3.write(95);
 } else if (buttonPushCounter == 9)
 {
   digitalWrite(2, HIGH);
   digitalWrite(3, HIGH);
   digitalWrite(4, HIGH);
   digitalWrite(5, HIGH);
   digitalWrite(8, LOW);
   digitalWrite(9, HIGH);
   digitalWrite(10, HIGH);
   servo_3.write(105);
 }

}
void loop() {
 buttonState = digitalRead(13);
 if (buttonState != lastButtonstate) {
   if (buttonState == HIGH) {
     buttonPushCounter++;
     displayNumber();
     Serial.println(buttonPushCounter);
     for (i = 0; i < 5;)
     {
       while (buttonPushCounter == even[i])
       {
         potentBuzzer();
         if (buttonState == HIGH) {
           break;
         }
         while (buttonPushCounter == odd[i])
         {
           ldrBuzzer();
           if (buttonState == HIGH) {
             break;
           }
         }
       }
     }
     lastButtonstate = buttonState;
     if (buttonPushCounter >= 9) {
       buttonPushCounter = -1;
     }
   }
 }
}

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

It looks like you're considering a button press to be HIGH. With input pullup, you should be looking for LOW.