Go Down

Topic: arduino doorbell for blind person (Read 298 times) previous topic - next topic

technomancer1273

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;
      }
    }
  }
}

technomancer1273


Railroader

Look at loop again. That if...  else will never work. That else asks for the same as the if.
Use Your knowledge. If that's not enough, look for education.
Having knowledge, think outside the box to gain more of it. Only trains run like the train, on the rails. The rest run between the rails.

groundFungus

#3
Sep 11, 2020, 05:31 am Last Edit: Sep 11, 2020, 05:34 am by groundFungus
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.  
You will save everyone's time if you read and follow the forum guidelines.  :)          
https://forum.arduino.cc/index.php/topic,148850.0.html
and
https://forum.arduino.cc/index.php?topic=97455.0

larryd

#4
Sep 12, 2020, 04:43 am Last Edit: Sep 12, 2020, 04:44 am by larryd
= 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)




No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

technomancer1273

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

technomancer1273

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


Code: [Select]
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;
  }
  }

groundFungus

#7
Sep 15, 2020, 05:14 am Last Edit: Sep 15, 2020, 05:18 am by groundFungus
Code: [Select]
  buttonPushCounter = digitalRead(buttonPin);
Shouldn't that be:
Code: [Select]
buttonState = digitalRead(buttonPin);

Code: [Select]
if (buttonPushCounter % 1 == 0) {
To:
Code: [Select]
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.
You will save everyone's time if you read and follow the forum guidelines.  :)          
https://forum.arduino.cc/index.php/topic,148850.0.html
and
https://forum.arduino.cc/index.php?topic=97455.0

groundFungus

#8
Sep 15, 2020, 05:27 am Last Edit: Sep 15, 2020, 05:28 am by groundFungus
Wire the switch as shown:


And try this code:
Code: [Select]
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?
You will save everyone's time if you read and follow the forum guidelines.  :)          
https://forum.arduino.cc/index.php/topic,148850.0.html
and
https://forum.arduino.cc/index.php?topic=97455.0

technomancer1273

#9
Sep 17, 2020, 04:05 am Last Edit: Sep 17, 2020, 04:38 am by technomancer1273
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'

technomancer1273

#10
Sep 17, 2020, 04:43 am Last Edit: Sep 17, 2020, 04:49 am by technomancer1273
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


Code: [Select]
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;
  }
  }

groundFungus

#11
Sep 17, 2020, 08:28 pm Last Edit: Sep 18, 2020, 03:39 am by groundFungus
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.

Code: [Select]
// 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;
   }
}
You will save everyone's time if you read and follow the forum guidelines.  :)          
https://forum.arduino.cc/index.php/topic,148850.0.html
and
https://forum.arduino.cc/index.php?topic=97455.0

technomancer1273

groundfungus

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

groundFungus

You will save everyone's time if you read and follow the forum guidelines.  :)          
https://forum.arduino.cc/index.php/topic,148850.0.html
and
https://forum.arduino.cc/index.php?topic=97455.0

Go Up