sounds activated with button using Arduino

Thread Starter

Killerbee65

Joined May 15, 2017
256
Hello all! I recently came across an idea that I want to improve on into the future.

The first iteration of the design includes a 8w speaker, Arduino uno, 10k resistor, breadboard, and button.

The basics of it is, I want to activate the sounds in this program using a button.

Code:
#include "Volume3.h"

#define speakerPin 9

void setup() {
  // put your setup code here, to run once:
  wolfWhistle();
  delay(1000);
  R2D2();
  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:

}

void gameboy(){
  vol.tone(speakerPin,1025,1023); // pa
  delay(70);
  uint16_t v = 1000;
  while(v > 0){
    vol.tone(speakerPin, 2090, v); // ting!
    delay(10);
    v-=10;
  }
}

void wolfWhistle() {
  int f = 122; // starting frequency
  int v = 0;   // starting volume
  while (f < 4000) {  // slide up to 4000Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00);
    f += 25;
    delay(1);
  }
  vol.noTone();
  delay(100); // wait a moment
  f = 122; // starting frequency
  v = 0;   // starting volume
  while (f < 3000) { // slide up to 3000Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00); 
    f += 25;
    delay(2);
  }
  while (f > 125) { // slide down to 125Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00);
    f -= 25;
    delay(2);
  }
  vol.noTone(); // end tone production
}

void R2D2() {
  int beeps[] = {1933, 2156, 1863, 1505, 1816, 1933, 1729, 2291};
  int buzzVols[] = {144, 180, 216, 252, 252, 252, 252, 216, 180, 144};

  int i = 9;
  while (i >= 0) {
    vol.tone(speakerPin, 1050, buzzVols[i]*4);
    delayMicroseconds(20*64);
    vol.tone(speakerPin, 1050, buzzVols[i] / 8*4);
    delayMicroseconds(40*64);
    i--;
  }

  delay(35);

  i = 0;
  while (i < 8) {
    int v = 0;
    while (v < 250) { // 12.5 mS fade up time
      vol.tone(speakerPin, beeps[i], v*4);
      v += 10;
      delayMicroseconds(2*64);
    }
    delay(20);
    v = 250;
    while (v > 0) { // 12.5 mS fade down time
      vol.tone(speakerPin, beeps[i], v*4);
      v -= 10;
      delayMicroseconds(5*64);
    }
    vol.noTone();
    delay(25);
    i++;
  }

  int f = 2466;
  while (f < 2825) {
    vol.tone(speakerPin, f, 1023);
    f += 3;
    delay(1);
  }  
  f = 2825;
  int v = 255;
  while (f > 2000) {
    vol.tone(speakerPin, f, v*4);
    f -= 6;
    v -= 1;
    delay(1);
  }
  vol.noTone();
  delay(35);

  i = 10;
  while (i > 0) {
    vol.tone(speakerPin, 1050, buzzVols[i]*4);
    delayMicroseconds(20*64);
    vol.tone(speakerPin, 1050, buzzVols[i] / 8*4);
    delayMicroseconds(40*64);
    i--;
  }
  vol.noTone();
}
note: I am not the creator of this program, but it does include the r2d2 sound I need for a project without the use of a mp3 shield.

this is the first step in my final project, the final project uses a ps4 controller's button instead of the 4 pin one I am currently using. I mention this for later development of the code, just taking it one step at a time.
 

dl324

Joined Mar 30, 2015
16,845
Just connect the switch to an input and have the Arduino check it's state.

You'll also need to move the sounds from the setup() procedure and put them in one controlled by the switch.

Arduino won't drive a speaker directly, so you'll need a transistor.
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
I know how to program it if I am writing the code or if it was already in the code, but like I said, this isnt my code and I dont quite understand it logically. I have tried to program the button so that if it is pressed itll play the selected sound like you mentioned above, but nothing.
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
Show your code for monitoring the switch.


big note here, this code is 3 different codes mashed together so my apologies if it looks like a mess :)

Code:
// Include the required Wire library for I2C<br>
#include <Wire.h>//i2c slave
#include "Volume3.h"//music
#include <Stepper.h>//head spin
#define speakerPin 9//speaker pin
#define STEPPER_PIN_1 10
#define STEPPER_PIN_2 11
#define STEPPER_PIN_3 12
#define STEPPER_PIN_4 13
#define motorSteps 400
int buttonPin = 7;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status
int step_number = 0;
int LED = 8;
int x = 0;
void setup() {

pinMode(buttonPin, INPUT);
//head turn
pinMode(STEPPER_PIN_1, OUTPUT);
pinMode(STEPPER_PIN_2, OUTPUT);
pinMode(STEPPER_PIN_3, OUTPUT);
pinMode(STEPPER_PIN_4, OUTPUT);
  
  // Define the LED pin as Output
  pinMode (LED, OUTPUT);
  // Start the I2C Bus as Slave on address 9
  Wire.begin(9); 
  // Attach a function to trigger when something is received.
  Wire.onReceive(receiveEvent);
  
//music
   wolfWhistle();
  delay(1000);
  R2D2();
  delay(1000);
}
void receiveEvent(int bytes) {
  x = Wire.read();    // read one character from the I2C
}
void loop() {
  //read the input pin
int buttonState = digitalRead(buttonPin);

//if the button is pressed
if (buttonState == 1){
   wolfWhistle();
  delay(1000);
  R2D2();
  delay(1000);
}
}

  //degrees for head turn
  for(int a = 0; a < 14000; a++){
  OneStep(false);
  delay(2);
 }
 for(int a = 0; a < 14000; a++){
  OneStep(true);
  delay(2);
 }
  //If value received is 0 blink LED for 200 ms: slave
  if (x == 0) {
    digitalWrite(LED, HIGH);
    delay(200);
    digitalWrite(LED, LOW);
    delay(200);
  }
  //If value received is 3 blink LED for 400 ms
  if (x == 3) {
    digitalWrite(LED, HIGH);
    delay(400);
    digitalWrite(LED, LOW);
    delay(400);
  }
}
void OneStep(bool dir){
    if(dir){
switch(step_number){
  case 0:
  digitalWrite(STEPPER_PIN_1, HIGH);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 1:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, HIGH);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 2:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, HIGH);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 3:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, HIGH);
  break;
} 
  }else{
    switch(step_number){
  case 0:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, HIGH);
  break;
  case 1:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, HIGH);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 2:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, HIGH);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 3:
  digitalWrite(STEPPER_PIN_1, HIGH);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
 
  
} 
  }
step_number++;
  if(step_number > 3){
    step_number = 0;
  }
}
//music code starts
void gameboy(){
  vol.tone(speakerPin,1025,1023); // pa
  delay(70);
  uint16_t v = 1000;
  while(v > 0){
    vol.tone(speakerPin, 2090, v); // ting!
    delay(10);
    v-=10;
  }
}

void wolfWhistle() {
  int f = 122; // starting frequency
  int v = 0;   // starting volume
  while (f < 4000) {  // slide up to 4000Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00);
    f += 25;
    delay(1);
  }
  vol.noTone();
  delay(100); // wait a moment
  f = 122; // starting frequency
  v = 0;   // starting volume
  while (f < 3000) { // slide up to 3000Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00); 
    f += 25;
    delay(2);
  }
  while (f > 125) { // slide down to 125Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00);
    f -= 25;
    delay(2);
  }
  vol.noTone(); // end tone production
}

void R2D2() {
  int beeps[] = {1933, 2156, 1863, 1505, 1816, 1933, 1729, 2291};
  int buzzVols[] = {144, 180, 216, 252, 252, 252, 252, 216, 180, 144};

  int i = 9;
  while (i >= 0) {
    vol.tone(speakerPin, 1050, buzzVols[i]*4);
    delayMicroseconds(20*64);
    vol.tone(speakerPin, 1050, buzzVols[i] / 8*4);
    delayMicroseconds(40*64);
    i--;
  }

  delay(35);

  i = 0;
  while (i < 8) {
    int v = 0;
    while (v < 250) { // 12.5 mS fade up time
      vol.tone(speakerPin, beeps[i], v*4);
      v += 10;
      delayMicroseconds(2*64);
    }
    delay(20);
    v = 250;
    while (v > 0) { // 12.5 mS fade down time
      vol.tone(speakerPin, beeps[i], v*4);
      v -= 10;
      delayMicroseconds(5*64);
    }
    vol.noTone();
    delay(25);
    i++;
  }

  int f = 2466;
  while (f < 2825) {
    vol.tone(speakerPin, f, 1023);
    f += 3;
    delay(1);
  }  
  f = 2825;
  int v = 255;
  while (f > 2000) {
    vol.tone(speakerPin, f, v*4);
    f -= 6;
    v -= 1;
    delay(1);
  }
  vol.noTone();
  delay(35);

  i = 10;
  while (i > 0) {
    vol.tone(speakerPin, 1050, buzzVols[i]*4);
    delayMicroseconds(20*64);
    vol.tone(speakerPin, 1050, buzzVols[i] / 8*4);
    delayMicroseconds(40*64);
    i--;
  }
  vol.noTone();
}
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
Please separate out the code you're having problems with.

I commented out the code that isn't of concern. I hope this helps.

Code:
// Include the required Wire library for I2C<br>
//#include <Wire.h>//i2c slave
#include "Volume3.h"//music
//#include <Stepper.h>//head spin
#define speakerPin 9//speaker pin
/*#define STEPPER_PIN_1 10
#define STEPPER_PIN_2 11
#define STEPPER_PIN_3 12
#define STEPPER_PIN_4 13
#define motorSteps 400*/
int buttonPin = 7;   // choose the input pin (for a pushbutton)
/*int step_number = 0;
int LED = 8;
int x = 0;*/
void setup() {

pinMode(buttonPin, INPUT);
/*//head turn
pinMode(STEPPER_PIN_1, OUTPUT);
pinMode(STEPPER_PIN_2, OUTPUT);
pinMode(STEPPER_PIN_3, OUTPUT);
pinMode(STEPPER_PIN_4, OUTPUT);
  
  // Define the LED pin as Output
  pinMode (LED, OUTPUT);
  // Start the I2C Bus as Slave on address 9
  Wire.begin(9); 
  // Attach a function to trigger when something is received.
  Wire.onReceive(receiveEvent);*/
  
//music
   wolfWhistle();
  delay(1000);
  R2D2();
  delay(1000);
}
/*void receiveEvent(int bytes) {
  x = Wire.read();    // read one character from the I2C
}*/
void loop() {
  //read the input pin
int buttonState = digitalRead(buttonPin);

//if the button is pressed
if (buttonState == 1){
   wolfWhistle();
  delay(1000);
  R2D2();
  delay(1000);
}
}

 /* //degrees for head turn
  for(int a = 0; a < 14000; a++){
  OneStep(false);
  delay(2);
 }
 for(int a = 0; a < 14000; a++){
  OneStep(true);
  delay(2);
 }
  //If value received is 0 blink LED for 200 ms: slave
  if (x == 0) {
    digitalWrite(LED, HIGH);
    delay(200);
    digitalWrite(LED, LOW);
    delay(200);
  }
  //If value received is 3 blink LED for 400 ms
  if (x == 3) {
    digitalWrite(LED, HIGH);
    delay(400);
    digitalWrite(LED, LOW);
    delay(400);
  }
}
void OneStep(bool dir){
    if(dir){
switch(step_number){
  case 0:
  digitalWrite(STEPPER_PIN_1, HIGH);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 1:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, HIGH);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 2:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, HIGH);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 3:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, HIGH);
  break;
} 
  }else{
    switch(step_number){
  case 0:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, HIGH);
  break;
  case 1:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, HIGH);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 2:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, HIGH);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 3:
  digitalWrite(STEPPER_PIN_1, HIGH);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
 
  
} 
  }
step_number++;
  if(step_number > 3){
    step_number = 0;
  }
}
//music code starts
void gameboy(){
  vol.tone(speakerPin,1025,1023); // pa
  delay(70);
  uint16_t v = 1000;
  while(v > 0){
    vol.tone(speakerPin, 2090, v); // ting!
    delay(10);
    v-=10;
  }
}
*/
void wolfWhistle() {
  int f = 122; // starting frequency
  int v = 0;   // starting volume
  while (f < 4000) {  // slide up to 4000Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00);
    f += 25;
    delay(1);
  }
  vol.noTone();
  delay(100); // wait a moment
  f = 122; // starting frequency
  v = 0;   // starting volume
  while (f < 3000) { // slide up to 3000Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00); 
    f += 25;
    delay(2);
  }
  while (f > 125) { // slide down to 125Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00);
    f -= 25;
    delay(2);
  }
  vol.noTone(); // end tone production
}

void R2D2() {
  int beeps[] = {1933, 2156, 1863, 1505, 1816, 1933, 1729, 2291};
  int buzzVols[] = {144, 180, 216, 252, 252, 252, 252, 216, 180, 144};

  int i = 9;
  while (i >= 0) {
    vol.tone(speakerPin, 1050, buzzVols[i]*4);
    delayMicroseconds(20*64);
    vol.tone(speakerPin, 1050, buzzVols[i] / 8*4);
    delayMicroseconds(40*64);
    i--;
  }

  delay(35);

  i = 0;
  while (i < 8) {
    int v = 0;
    while (v < 250) { // 12.5 mS fade up time
      vol.tone(speakerPin, beeps[i], v*4);
      v += 10;
      delayMicroseconds(2*64);
    }
    delay(20);
    v = 250;
    while (v > 0) { // 12.5 mS fade down time
      vol.tone(speakerPin, beeps[i], v*4);
      v -= 10;
      delayMicroseconds(5*64);
    }
    vol.noTone();
    delay(25);
    i++;
  }

  int f = 2466;
  while (f < 2825) {
    vol.tone(speakerPin, f, 1023);
    f += 3;
    delay(1);
  }  
  f = 2825;
  int v = 255;
  while (f > 2000) {
    vol.tone(speakerPin, f, v*4);
    f -= 6;
    v -= 1;
    delay(1);
  }
  vol.noTone();
  delay(35);

  i = 10;
  while (i > 0) {
    vol.tone(speakerPin, 1050, buzzVols[i]*4);
    delayMicroseconds(20*64);
    vol.tone(speakerPin, 1050, buzzVols[i] / 8*4);
    delayMicroseconds(40*64);
    i--;
  }
  vol.noTone();
}
 

MrSoftware

Joined Oct 29, 2013
2,188
Remove all of the code that has nothing to do with playing sound, get the sound part working all by itself first. Maybe even start a whole new program just for sound. THEN worry about adding the other functionality. Trying to do it all at once is confusing you. Make the sound program such that there is one function call, PlaySound(), that triggers the sound. That will make it very easy to add other parts later.
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
Code:
#include "Volume3.h"

#define speakerPin 9

void setup() {
  // put your setup code here, to run once:
  wolfWhistle();
  delay(1000);
  R2D2();
  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:

}

void gameboy(){
  vol.tone(speakerPin,1025,1023); // pa
  delay(70);
  uint16_t v = 1000;
  while(v > 0){
    vol.tone(speakerPin, 2090, v); // ting!
    delay(10);
    v-=10;
  }
}

void wolfWhistle() {
  int f = 122; // starting frequency
  int v = 0;   // starting volume
  while (f < 4000) {  // slide up to 4000Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00);
    f += 25;
    delay(1);
  }
  vol.noTone();
  delay(100); // wait a moment
  f = 122; // starting frequency
  v = 0;   // starting volume
  while (f < 3000) { // slide up to 3000Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00); 
    f += 25;
    delay(2);
  }
  while (f > 125) { // slide down to 125Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00);
    f -= 25;
    delay(2);
  }
  vol.noTone(); // end tone production
}

void R2D2() {
  int beeps[] = {1933, 2156, 1863, 1505, 1816, 1933, 1729, 2291};
  int buzzVols[] = {144, 180, 216, 252, 252, 252, 252, 216, 180, 144};

  int i = 9;
  while (i >= 0) {
    vol.tone(speakerPin, 1050, buzzVols[i]*4);
    delayMicroseconds(20*64);
    vol.tone(speakerPin, 1050, buzzVols[i] / 8*4);
    delayMicroseconds(40*64);
    i--;
  }

  delay(35);

  i = 0;
  while (i < 8) {
    int v = 0;
    while (v < 250) { // 12.5 mS fade up time
      vol.tone(speakerPin, beeps[i], v*4);
      v += 10;
      delayMicroseconds(2*64);
    }
    delay(20);
    v = 250;
    while (v > 0) { // 12.5 mS fade down time
      vol.tone(speakerPin, beeps[i], v*4);
      v -= 10;
      delayMicroseconds(5*64);
    }
    vol.noTone();
    delay(25);
    i++;
  }

  int f = 2466;
  while (f < 2825) {
    vol.tone(speakerPin, f, 1023);
    f += 3;
    delay(1);
  }  
  f = 2825;
  int v = 255;
  while (f > 2000) {
    vol.tone(speakerPin, f, v*4);
    f -= 6;
    v -= 1;
    delay(1);
  }
  vol.noTone();
  delay(35);

  i = 10;
  while (i > 0) {
    vol.tone(speakerPin, 1050, buzzVols[i]*4);
    delayMicroseconds(20*64);
    vol.tone(speakerPin, 1050, buzzVols[i] / 8*4);
    delayMicroseconds(40*64);
    i--;
  }
  vol.noTone();
}
Here's the original code. You mentioned using something like playsound( ), looking at void setup( ), doesn't the names of each sound already do that? I have played around with it and do understand this be the case.
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
Code:
#include "Volume3.h"

#define speakerPin 9

void setup() {
  // put your setup code here, to run once:
  wolfWhistle();
  delay(1000);
  R2D2();
  delay(1000);
  pinMode(2, INPUT);
}

int switchOne = 0;  

void loop() {
  // put your main code here, to run repeatedly:
switchOne = digitalRead(2);
if (switchOne == HIGH) {
     R2D2();
  }
}

void gameboy(){
  vol.tone(speakerPin,1025,1023); // pa
  delay(70);
  uint16_t v = 1000;
  while(v > 0){
    vol.tone(speakerPin, 2090, v); // ting!
    delay(10);
    v-=10;
  }
}

void wolfWhistle() {
  int f = 122; // starting frequency
  int v = 0;   // starting volume
  while (f < 4000) {  // slide up to 4000Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00);
    f += 25;
    delay(1);
  }
  vol.noTone();
  delay(100); // wait a moment
  f = 122; // starting frequency
  v = 0;   // starting volume
  while (f < 3000) { // slide up to 3000Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00); 
    f += 25;
    delay(2);
  }
  while (f > 125) { // slide down to 125Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00);
    f -= 25;
    delay(2);
  }
  vol.noTone(); // end tone production
}

void R2D2() {
  int beeps[] = {1933, 2156, 1863, 1505, 1816, 1933, 1729, 2291};
  int buzzVols[] = {144, 180, 216, 252, 252, 252, 252, 216, 180, 144};

  int i = 9;
  while (i >= 0) {
    vol.tone(speakerPin, 1050, buzzVols[i]*4);
    delayMicroseconds(20*64);
    vol.tone(speakerPin, 1050, buzzVols[i] / 8*4);
    delayMicroseconds(40*64);
    i--;
  }

  delay(35);

  i = 0;
  while (i < 8) {
    int v = 0;
    while (v < 250) { // 12.5 mS fade up time
      vol.tone(speakerPin, beeps[i], v*4);
      v += 10;
      delayMicroseconds(2*64);
    }
    delay(20);
    v = 250;
    while (v > 0) { // 12.5 mS fade down time
      vol.tone(speakerPin, beeps[i], v*4);
      v -= 10;
      delayMicroseconds(5*64);
    }
    vol.noTone();
    delay(25);
    i++;
  }

  int f = 2466;
  while (f < 2825) {
    vol.tone(speakerPin, f, 1023);
    f += 3;
    delay(1);
  }  
  f = 2825;
  int v = 255;
  while (f > 2000) {
    vol.tone(speakerPin, f, v*4);
    f -= 6;
    v -= 1;
    delay(1);
  }
  vol.noTone();
  delay(35);

  i = 10;
  while (i > 0) {
    vol.tone(speakerPin, 1050, buzzVols[i]*4);
    delayMicroseconds(20*64);
    vol.tone(speakerPin, 1050, buzzVols[i] / 8*4);
    delayMicroseconds(40*64);
    i--;
  }
  vol.noTone();
}
here's the code with the button code, not sure if the code doesn't work or if the button I am using is a little of.
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
Are you pulling your pin down with the internal or an external pull-down, then pulling it up with the button?
I don't exactly know what you mean but from I know I can say my resistor is a pull down one, not sure if that's how you say it.
I have it set up like the image below.

1590022237704.png
 

Deleted member 115935

Joined Dec 31, 1969
0
i see your still on this , in a new thread.
Did you get very far with the digital input example we posted in the other thread ?

As you know , as you say your a programmer, the art of programming is to divide the problem up.

So first up, make some code that reads a button and toggles a led ( there is one on the board for you )

Once you have that , you will be a big step forward,
show us what you can do.

good luck.
 

MrSoftware

Joined Oct 29, 2013
2,188
Get your multi meter and make sure the buttons are working how you think they are. If they're rotated 90-degrees, they won't operate the way you're expecting. You can also pull the wire out of your breadboard and touch the wires instead of pushing the button, to take the button out of the equation.
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
i see your still on this , in a new thread.
Did you get very far with the digital input example we posted in the other thread ?

As you know , as you say your a programmer, the art of programming is to divide the problem up.

So first up, make some code that reads a button and toggles a led ( there is one on the board for you )

Once you have that , you will be a big step forward,
show us what you can do.

good luck.
Thanks for the encouragement! Though I'm not quite sure what thread you mean? I have quite a few
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
Get your multi meter and make sure the buttons are working how you think they are. If they're rotated 90-degrees, they won't operate the way you're expecting. You can also pull the wire out of your breadboard and touch the wires instead of pushing the button, to take the button out of the equation.
I'll try it and let you know, but as far as code goes do you see anything worth noting?
 

djsfantasi

Joined Apr 11, 2010
9,156
You can’t reliably read a button with a digitalRead() statement. You need a debounce routine. Or use the BOUNCE2 Arduino library.
 

djsfantasi

Joined Apr 11, 2010
9,156
Code:
#include "Volume3.h"

#define speakerPin 9

void setup() {
  // put your setup code here, to run once:
  wolfWhistle();
  delay(1000);
  R2D2();
  delay(1000);
  pinMode(2, INPUT);
}

int switchOne = 0;

void loop() {
  // put your main code here, to run repeatedly:
switchOne = digitalRead(2);
if (switchOne == HIGH) {
     R2D2();
  }
}

void gameboy(){
  vol.tone(speakerPin,1025,1023); // pa
  delay(70);
  uint16_t v = 1000;
  while(v > 0){
    vol.tone(speakerPin, 2090, v); // ting!
    delay(10);
    v-=10;
  }
}

void wolfWhistle() {
  int f = 122; // starting frequency
  int v = 0;   // starting volume
  while (f < 4000) {  // slide up to 4000Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00);
    f += 25;
    delay(1);
  }
  vol.noTone();
  delay(100); // wait a moment
  f = 122; // starting frequency
  v = 0;   // starting volume
  while (f < 3000) { // slide up to 3000Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00);
    f += 25;
    delay(2);
  }
  while (f > 125) { // slide down to 125Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00);
    f -= 25;
    delay(2);
  }
  vol.noTone(); // end tone production
}

void R2D2() {
  int beeps[] = {1933, 2156, 1863, 1505, 1816, 1933, 1729, 2291};
  int buzzVols[] = {144, 180, 216, 252, 252, 252, 252, 216, 180, 144};

  int i = 9;
  while (i >= 0) {
    vol.tone(speakerPin, 1050, buzzVols[i]*4);
    delayMicroseconds(20*64);
    vol.tone(speakerPin, 1050, buzzVols[i] / 8*4);
    delayMicroseconds(40*64);
    i--;
  }

  delay(35);

  i = 0;
  while (i < 8) {
    int v = 0;
    while (v < 250) { // 12.5 mS fade up time
      vol.tone(speakerPin, beeps[i], v*4);
      v += 10;
      delayMicroseconds(2*64);
    }
    delay(20);
    v = 250;
    while (v > 0) { // 12.5 mS fade down time
      vol.tone(speakerPin, beeps[i], v*4);
      v -= 10;
      delayMicroseconds(5*64);
    }
    vol.noTone();
    delay(25);
    i++;
  }

  int f = 2466;
  while (f < 2825) {
    vol.tone(speakerPin, f, 1023);
    f += 3;
    delay(1);
  }
  f = 2825;
  int v = 255;
  while (f > 2000) {
    vol.tone(speakerPin, f, v*4);
    f -= 6;
    v -= 1;
    delay(1);
  }
  vol.noTone();
  delay(35);

  i = 10;
  while (i > 0) {
    vol.tone(speakerPin, 1050, buzzVols[i]*4);
    delayMicroseconds(20*64);
    vol.tone(speakerPin, 1050, buzzVols[i] / 8*4);
    delayMicroseconds(40*64);
    i--;
  }
  vol.noTone();
}
here's the code with the button code, not sure if the code doesn't work or if the button I am using is a little of.
The Volume3 library requires that you initialize an object before it can be used.

Thus, your vol.tone statements will do nothing because the vol object hasn’t been initialized. At a minimum, you need to define the vol object at the beginning of the sketch with a “Volume vol;” statement. Then this object needs to be initialized with at least a “vol.begin();” statement in the setup() routine.

I’m not familiar with the Volume3 library, but discovered the definition and initialization requirements with a simple Google search for “Arduino Volume3”.

That’s your problem.
 
Top