arduino doorbell for blind person

i am trying to code the button so that press button once and tune 1 plays, press button twice play tune 2 and press button 3 times play tune 3 here is what i have:

int buzzerPin = 3;
int buttonPin = 7;
int ButtonPressed = 0;
int buttonState = 0;

int buttonpressedonce() {
if (ButtonPressed = HIGH) {
tone(800, 30);
}
}
int buttonpressedtwice() {
if (ButtonPressed = HIGH) {
tone(700, 30);
}
}
int buttonpressed3times() {
if (ButtonPressed = HIGH) {
tone(600, 30);
}

}

void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {
ButtonPressed = digitalRead(buttonPin);
if (buttonState = HIGH) {
ButtonPressed= HIGH;
buttonpressedonce();
} else {
if (buttonState = HIGH) {
ButtonPressed= HIGH;
buttonpressedtwice();
} else {
if (buttonState = HIGH) {
ButtonPressed= HIGH;
buttonpressed3times();
} else {
noTone;
}
}
}
}

it just keep playing the tunes

Look at loop again. That if... else will never work. That else asks for the same as the if.

Count button presses with the state change detection tutorial method. Use a switch case or if, else if, else structure to select the tune based on the count.

Read the how to use this forum-please read sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

= is not the same as ==

= Assigns a value to a variable.

== is the comparison operator.

if (buttonState = HIGH) <———<<< there are a whole bunch to fix.

if (buttonState == HIGH)

groundfungus

i have gone to the site and copy and paste along with some editing and i can't get it to be button pressed once, button pressed twice and button pressed 3 times

all it does is play the same tune over and over again this is what i have

const int buzzerPin = 3;
const int buttonPin = 7;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

void setup() {
  pinMode(buzzerPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  buttonPushCounter = digitalRead(buttonPin);
   if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      Serial.println("off");
    }
    delay(50);
  }
  lastButtonState = buttonState;
  if (buttonPushCounter % 1 == 0) {
    delay(150);
    tone (buzzerPin, 800, 30);
  }
  else if (buttonPushCounter % 2 == 0) {
    delay(150);
    tone (buzzerPin, 700, 30);
  }
  else if (buttonPushCounter % 3 == 0) {
    delay(150);
    tone (buzzerPin, 600, 30);
  }
  else{
    noTone;
  }
  }
  buttonPushCounter = digitalRead(buttonPin);

Shouldn't that be:

 buttonState = digitalRead(buttonPin);
 if (buttonPushCounter % 1 == 0) {

To:

 if (buttonPushCounter  == 1) {

And so on.

How is the switch wired? Is there a pulldown resistor?

Switches are better wired to ground and a digital input set to pinMode(INPUT_PULLUP). You will need to adjust the code because the input will read HIGH when not pressed and LOW when pressed.

Wire the switch as shown:

And try this code:

const int buzzerPin = 3;
const int buttonPin = 7;
int buttonPushCounter = 0;
int buttonState = HIGH;
int lastButtonState = HIGH;

void setup()
{
   pinMode(buzzerPin, OUTPUT);
   pinMode(buttonPin, INPUT_PULLUP);
   Serial.begin(9600);
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 50;
   if (millis() - timer >= interval)
   {
      timer = millis();
      buttonState = digitalRead(buttonPin);
      if (buttonState != lastButtonState)
      {

         if (buttonState == LOW)
         {
            buttonPushCounter++;
            Serial.println("on");
            Serial.print("number of button pushes: ");
            Serial.println(buttonPushCounter);
         }
         else
         {
            Serial.println("off");
         }
         delay(50);
         lastButtonState = buttonState;

         if (buttonPushCounter == 1)
         {
            delay(150);
            tone (buzzerPin, 800, 2000);
            Serial.println(800);
         }

         else if (buttonPushCounter == 2 )
         {
            delay(150);
            tone (buzzerPin, 700, 2000);
            Serial.println(700);
         }
         else if (buttonPushCounter == 3)
         {
            delay(150);
            tone (buzzerPin, 600, 2000);
            Serial.println(600);
         }
         else if (buttonPushCounter == 4)
         {
            buttonPushCounter = 0;
         }
         else
         {
            noTone;
         }
      }
   }
}

Is this close to what you want?

hey thanks it works but i need to wait to see if the button will be pushed more than once and then reset after the sound is made 'i am just using the Arduino thinker-shield'

this is what i have p.s i have added the theme songs of Game Of Thrones, Mario, and Star Wars in place of the beeps not sure if they are working through

const int buzzerPin = 3;
const int buttonPin = 7;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;




int starwars (){
  const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;
 
const int buzzerPin = 8;
const int ledPin1 = 12;
const int ledPin2 = 13;
 
int counter = 0;

void loop(){
 
  //Play first section
  firstSection();
 
  //Play second section
  secondSection();
 
  //Variant 1
  beep(f, 250);  
  beep(gS, 500);  
  beep(f, 350);  
  beep(a, 125);
  beep(cH, 500);
  beep(a, 375);  
  beep(cH, 125);
  beep(eH, 650);
 
  delay(500);
 
  //Repeat second section
  secondSection();
 
  //Variant 2
  beep(f, 250);  
  beep(gS, 500);  
  beep(f, 375);  
  beep(cH, 125);
  beep(a, 500);  
  beep(f, 375);  
  beep(cH, 125);
  beep(a, 650);  
 
  delay(650);
}
 
void{ beep(int note, int duration)
  //Play tone on buzzerPin
  tone(buzzerPin, note, duration);
 
  //Play different LED depending on value of 'counter'
  if(counter % 2 == 0)
  {
    digitalWrite(ledPin1, HIGH);
    delay(duration);
    digitalWrite(ledPin1, LOW);
  }else
  {
    digitalWrite(ledPin2, HIGH);
    delay(duration);
    digitalWrite(ledPin2, LOW);
  }
 
  //Stop tone on buzzerPin
  noTone(buzzerPin);
 
  delay(50);
 
  //Increment counter
  counter++;
}
 
void{ firstSection()
  beep(a, 500);
  beep(a, 500);    
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);  
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);
 
  delay(500);
 
  beep(eH, 500);
  beep(eH, 500);
  beep(eH, 500);  
  beep(fH, 350);
  beep(cH, 150);
  beep(gS, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);
 
  delay(500);
}
 
void{ secondSection()
  beep(aH, 500);
  beep(a, 300);
  beep(a, 150);
  beep(aH, 500);
  beep(gSH, 325);
  beep(gH, 175);
  beep(fSH, 125);
  beep(fH, 125);    
  beep(fSH, 250);
 
  delay(325);
 
  beep(aS, 250);
  beep(dSH, 500);
  beep(dH, 325);  
  beep(cSH, 175);  
  beep(cH, 125);  
  beep(b, 125);  
  beep(cH, 250);  
 
  delay(350);
}
}


void setup() {
  pinMode(buzzerPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  buttonState = digitalRead(buttonPin);
   if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      Serial.println("off");
    }
    delay(50);
  }
  lastButtonState = buttonState;
  if (buttonPushCounter  == 1) {
    delay(150);
    
  }
  else if (buttonPushCounter  == 2) {
    delay(150);
    
  }
  else if (buttonPushCounter  == 3) {
    delay(150);
    tone (buzzerPin, 600, 30);
  }
  else{
    noTone;
  }
  }

This example reads switch presses. It will count presses for 1 second after the first press and choose a tone by the count. Replace the tone calls with functions to play your tunes.

// counts button presses in tuneInterval milliseconds
// play tune based on count

const int buzzerPin = 3;
const int buttonPin = 7;
int buttonPushCounter = 0;
int buttonState = HIGH;
int lastButtonState = HIGH;

unsigned long tuneInterval = 1000;  // time for button count before playing
unsigned long tuneTimer = 0;

const byte numberOfTunes = 4;

void setup()
{
   pinMode(buzzerPin, OUTPUT);
   pinMode(buttonPin, INPUT_PULLUP);
   Serial.begin(9600);
}

void loop()
{
   static unsigned long buttonTimer = 0;
   unsigned long interval = 50;
   if (millis() - buttonTimer >= interval)
   {
      buttonTimer = millis();
      buttonState = digitalRead(buttonPin);
      if (buttonState != lastButtonState)
      {

         if (buttonState == LOW)
         {
            buttonPushCounter++;
            tuneTimer = millis();  // reset clock
            Serial.print("PRESSED  ");
            Serial.print("number of button pushes: ");
            Serial.println(buttonPushCounter);
            
         }
         if (buttonPushCounter > numberOfTunes)
         {
            buttonPushCounter = 0;
         }
         lastButtonState = buttonState;
      }
   }
   // has it been tuneInterval since the last button press?
   if (millis() - tuneTimer >= tuneInterval)
      // yes, play tune by buttonPushCounter
   {
      if (buttonPushCounter == 1)
      {
         //delay(150);
         tone (buzzerPin, 800, 2000);
         Serial.println(800);
      }

      else if (buttonPushCounter == 2 )
      {
         //delay(150);
         tone (buzzerPin, 700, 2000);
         Serial.println(700);
      }
      else if (buttonPushCounter == 3)
      {
         //delay(150);
         tone (buzzerPin, 600, 2000);
         Serial.println(600);
      }
      buttonPushCounter = 0;
   }
}

groundfungus

thank you this is what i needed and it works how i wanted it to :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:

Great, glad to help.