0

This question already has an answer here:

I am trying to have my sketch run a short series of tones (melody) through a Piezo buzzer after a selection is made by the user. I am getting a compilation error with my use of the tone function in the code I've written. I would kindly ask if someone in the "know" could have a look and provide me with a clear, step by step explanation of what I need to change to get it to work. I thank you in advance, your help is much needed and appreciated. -Steve

Here is my sketch:

#include <Stepper.h>
#include <Servo.h>
#include <Tone.h>

/*ARDUINO MOTOR SHIELD
 * The pin breakdown is as follows:
 * Function          Channel A   Channel B
 * Direction         Digital 12  Digital 13
 * Speed (PWM)       Digital 3   Digital 11
 * Brake             Digital 9   Digital 8
 * Current Sensing   Analog 0  Analog 1
 */

const int stepsPerRevolution = 200;  // 1.8 deg. step angle(200 steps/360 deg. rev)
const int dirChA = 12;     // assigning name "dirChA" to pin 12
const int dirChB = 13;     // assigning name "dirChB" to pin 13 
const int pwmChA = 3;      // assigning name "pwmChA" to pin 3 
const int pwmChB = 11;     // assigning name "pwmChB" to pin 11 
const int brkChA = 9;      // assigning name "brkChA" to pin 9
const int brkChB = 8;      // assigning name "brkChB" to pin 8

const int srvoMtr = 10;    // assigning name "srvoMtr" to pin 10

const int pButton1 = 4;    // assigning name "pButton1" to pin 4 
const int pButton2 = 5;    // assigning name "pButton2" to pin 5
const int pButton3 = 6;    // assigning name "pButton3" to pin 6
const int pButton4 = 7;    // assigning name "pButton4" to pin 7

const int ledLight = 2;    // assigning name "ledLight" to pin 2

const int tonePin = 14;     // assigning name "tonePin" to pin 14

Stepper myStepper(stepsPerRevolution, dirChA, dirChB);  // initialize the stepper library on pins 12 & 13
Servo myServo;  // initialize the servo library to logical name myServo

int pButton1State = 0;     // variable for reading the pButton1 state as LOW or HIGH
int pButton2State = 0;     // variable for reading the pButton2 state as LOW or HIGH
int pButton3State = 0;     // variable for reading the pButton3 state as LOW or HIGH
int pButton4State = 0;   // variable for reading the pButton4 state as LOW or HIGH

int pButtonFlag = 0;     // variable for when button 1 has been pressed and released

int myServoPos = 0;    // variable for when servo has reached it's position
int myServoFlag = 0;   // variable for servo when any of the 4 pButton are pressed

void setup() {

 // 1 Stepper Motor
 pinMode(dirChA, OUTPUT);   // initialize pin 12 as an OUTPUT
 digitalWrite(dirChA, LOW);   //initialize pin 12 as LOW (pin voltage is 0 V)

 pinMode(dirChB, OUTPUT);   // initialize pin 13 as an OUTPUT
 digitalWrite(dirChB, LOW);   //intialize pin 13 as LOW (pin voltage is 0 V)

 pinMode(pwmChA, OUTPUT);   // initialize pin 3 as an OUTPUT
 digitalWrite(pwmChA, LOW);   //intialize pin 3 as LOW (pin voltage is 0 V)

 pinMode(pwmChB, OUTPUT);   // initialize pin 11 as an OUTPUT
 digitalWrite(pwmChB, LOW);   //intialize pin 11 as LOW (pin voltage is 0 V)

 pinMode(brkChA, OUTPUT);   // initialize pin 9 as an OUTPUT
 digitalWrite(brkChA, LOW);   //intialize pin 9 as LOW (pin voltage is 0 V)

 pinMode(brkChB, OUTPUT);   // initialize pin 8 as an OUTPUT
 digitalWrite(brkChB, LOW);   //intialize pin 8 as LOW (pin voltage is 0 V)

 myStepper.setSpeed(60);   // set the speed of the stepper motor to 60 RPM

 // 4 Push Buttons
 pinMode(pButton1, INPUT);   // initialize pin 4 as an INPUT
 digitalWrite(pButton1, LOW);   // initialize pin 4 as LOW (pin voltage less than 1.5V)

 pinMode(pButton2, INPUT);   // initialize pin 5 as an INPUT
 digitalWrite(pButton2, LOW);   // initialize pin 5 as LOW (pin voltage less than 1.5V)

 pinMode(pButton3, INPUT);   // initialize pin 6 as an INPUT
 digitalWrite(pButton3, LOW);   // initialize pin 6 as LOW (pin voltage less than 1.5V)

 pinMode(pButton4, INPUT);   // initialize pin 7 as an INPUT
 digitalWrite(pButton4, LOW);   // initialize pin 7 as LOW (pin voltage less than 1.5V)

 // 1 Led Light
 pinMode(ledLight, OUTPUT);   // initialize pin 2 as an OUTPUT
 digitalWrite(ledLight, LOW);   // initialize pin 2 as LOW (pin voltage is 0 V)

// 1 Piezo Speaker
 tone();
 pinMode(tonePin, OUTPUT);   // initialize pin 1 as an OUTPUT
 digitalWrite(tonePin, LOW);   // initialize pin 1 as LOW (pin voltage is 0 V)
}

void loop() {
  //Read button state (pressed or not pressed?)
  pButton1State = digitalRead(pButton1);   // read the state of pButton1 as LOW or HIGH
  pButton2State = digitalRead(pButton2);   // read the state of pButton2 as LOW or HIGH
  pButton3State = digitalRead(pButton3);   // read the state of pButton3 as LOW or HIGH
  pButton4State = digitalRead(pButton4);   // read the state of pButton4 as LOW or HIGH

  // check if the pushbutton is pressed. If it is, the pbuttonState is HIGH:
  if (pButton1State == HIGH) {
     pButtonFlag=1;  // change pButton1Flag variable
  }
     if (pButtonFlag == 1){
        digitalWrite(pwmChA, HIGH);   // enable power to stepper
        digitalWrite(pwmChB, HIGH);   // enable power to stepper
        myStepper.step(40);          // step forward 40 steps to position 1
        digitalWrite(pwmChA, LOW);    // disable power to stepper
        digitalWrite(pwmChB, LOW);    //disable power to stepper

        myServoFlag=1;             // change ServoFlag variable to dispense chocolate

        if (myServoFlag == 1){
           myServo.attach(srvoMtr);   // attaches the servo library to pin 10
           for (myServoPos = 0; myServoPos <= 100; myServoPos += 1) { // goes from 0 degrees to 100 degrees in steps of 1 degree
               myServo.write(myServoPos);    // tell servo to go to position in variable 'myServoPos'
               delay(15);                    // waits 15 milliseconds for the servo to reach the position
           }
           myServo.detach();   // detaches the servo library from pin 10   
           delay(2000); // hold position OPEN for 2 sec

           myServo.attach(srvoMtr);   // attaches the servo library to pin 10
           for (myServoPos = 100; myServoPos >= 0; myServoPos -= 1) { // goes from 100 degrees to 0 degrees in steps of 1 degree
               myServo.write(myServoPos);    // tell servo to go to position in variable 'pos
               delay(15);
           }
           myServo.detach();   // detaches the servo library from pin 10
        }    

        myServoFlag=0;         // changes myServoFlag value back to zero (0)     
        digitalWrite(ledLight, HIGH);   // turn on light to indicate chocolate is dispensed

        if (ledLight == HIGH){
           digitalWrite(tonePin, HIGH); // turn on Piezo speaker 

           // Play Melody
           void tone();{
           tone(tonePin, 233, 203.992851562);
           delay(226.658723958);
           delay(4.82252604167);
           tone(tonePin, 184, 412.325976562);
           delay(458.139973958);
           delay(4.82252604167);
           tone(tonePin, 184, 99.8262890625);
           delay(110.918098958);
           delay(4.82252604167);
           tone(tonePin, 155, 99.8262890625);
           delay(110.918098958);
           delay(4.82252604167);
           tone(tonePin, 184, 203.992851562);
           delay(226.658723958);
           delay(4.82252604167);
           tone(tonePin, 184, 203.992851562);
           delay(226.658723958);
           delay(236.303776042);
           tone(tonePin, 184, 99.8262890625);
           delay(110.918098958);
           delay(4.82252604167);
           tone(tonePin, 155, 99.8262890625);
           delay(110.918098958);
           delay(4.82252604167);
           tone(tonePin, 184, 203.992851562);
           delay(226.658723958);
           delay(4.82252604167);
           tone(tonePin, 184, 203.992851562);
           delay(226.658723958);
           delay(236.303776042);
           tone(tonePin, 184, 203.992851562);
           delay(226.658723958);
           delay(236.303776042);
           tone(tonePin, 233, 555.555);
           }
        }    
        digitalWrite(tonePin, LOW);    // turns off Piezo speaker

        digitalWrite(pwmChA, HIGH);   // enable power to stepper
        digitalWrite(pwmChB, HIGH);   // enable power to stepper
        myStepper.step(-40);          // step backward 40 steps to position 0
        digitalWrite(pwmChA, LOW);    // disable power to stepper
        digitalWrite(pwmChB, LOW);    // disable power to stepper

        digitalWrite(pButton1, LOW);   // initialize pin 4 as LOW (pin voltage less than 1.5V)
        digitalWrite(ledLight, LOW);   // turns off light to indicate the sequence is completed

        pButtonFlag=0;                // changes pButtonFlat value back to zero (0)
        delay(2000); 
     }

  // position 2
  if (pButton2State == HIGH) {
     pButtonFlag=1;  // change pButtonFlag variable
  }
     if (pButtonFlag == 1){
        digitalWrite(pwmChA, HIGH);   // enable power to stepper
        digitalWrite(pwmChB, HIGH);   // enable power to stepper
        myStepper.step(80);          // step forward 80 steps to position 2
        digitalWrite(pwmChA, LOW);    // disable power to stepper
        digitalWrite(pwmChB, LOW);    //disable power to stepper
        delay(2000);

        myServoFlag=1;    // change ServoFlag variable to dispense chocolate

        if (myServoFlag == 1){
           myServo.attach(srvoMtr);   // attaches the servo library to pin 10
           for (myServoPos = 0; myServoPos <= 100; myServoPos += 1) { // goes from 0 degrees to 100 degrees in steps of 5 degrees
               myServo.write(myServoPos);    // tell servo to go to position in variable 'myServoPos'
               delay(15);                    // waits 15 milliseconds for the servo to reach the position
           }
           myServo.detach();   // detaches the servo library from pin 10   
           delay(2000);

           myServo.attach(srvoMtr);   // attaches the servo library to pin 10
           for (myServoPos = 100; myServoPos >= 0; myServoPos -= 1) { // goes from 100 degrees to 0 degrees in steps of 1 degree
               myServo.write(myServoPos);    // tell servo to go to position in variable 'pos
               delay(15);
           }
           myServo.detach();   // detaches the servo library from pin 10   
        }    
        myServoFlag=0;                // changes myServoFlag value back to zero (0)     
        digitalWrite(ledLight, HIGH);    // turn on light to indicate chocolate is dispensed

        if (ledLight == HIGH){
           digitalWrite(tonePin, HIGH); // turn on Piezo speaker 

           // Play Melody
           tone(tonePin, 233, 203.992851562);
           delay(226.658723958);
           delay(4.82252604167);
           tone(tonePin, 184, 412.325976562);
           delay(458.139973958);
           delay(4.82252604167);
           tone(tonePin, 184, 99.8262890625);
           delay(110.918098958);
           delay(4.82252604167);
           tone(tonePin, 155, 99.8262890625);
           delay(110.918098958);
           delay(4.82252604167);
           tone(tonePin, 184, 203.992851562);
           delay(226.658723958);
           delay(4.82252604167);
           tone(tonePin, 184, 203.992851562);
           delay(226.658723958);
           delay(236.303776042);
           tone(tonePin, 184, 99.8262890625);
           delay(110.918098958);
           delay(4.82252604167);
           tone(tonePin, 155, 99.8262890625);
           delay(110.918098958);
           delay(4.82252604167);
           tone(tonePin, 184, 203.992851562);
           delay(226.658723958);
           delay(4.82252604167);
           tone(tonePin, 184, 203.992851562);
           delay(226.658723958);
           delay(236.303776042);
           tone(tonePin, 184, 203.992851562);
           delay(226.658723958);
           delay(236.303776042);
           tone(tonePin, 233, 555.555);
        }    
        digitalWrite(tonePin, LOW);    // turns off Piezo speaker  

        digitalWrite(pwmChA, HIGH);   // enable power to stepper
        digitalWrite(pwmChB, HIGH);   // enable power to stepper
        myStepper.step(-80);          // step backward 80 steps to position 0
        digitalWrite(pwmChA, LOW);    // disable power to stepper
        digitalWrite(pwmChB, LOW);    //disable power to stepper

        digitalWrite(pButton2, LOW);   // initialize pin 5 as LOW (pin voltage less than 1.5V)
        digitalWrite(ledLight, LOW);   // turns off light to indicate the sequence is completed

        pButtonFlag=0;   // changes pButton2Flag value back to zero (0)
        delay(2000); 
    }

    // position 3
    if (pButton3State == HIGH) {
       pButtonFlag=1;   // change pButtonFlag variable
    }
       if (pButtonFlag == 1){
          digitalWrite(pwmChA, HIGH);   // enable power to stepper
          digitalWrite(pwmChB, HIGH);   // enable power to stepper
          myStepper.step(120);          // step forward 120 steps to position 3
          digitalWrite(pwmChA, LOW);    // disable power to stepper
          digitalWrite(pwmChB, LOW);    //disable power to stepper
          delay(2000);

          myServoFlag=1;   // change ServoFlag variable to dispense chocolate

          if (myServoFlag == 1){
             myServo.attach(srvoMtr);   // attaches the servo library to pin 10
             for (myServoPos = 0; myServoPos <= 100; myServoPos += 1) { // goes from 0 degrees to 100 degrees in steps of 5 degrees
                 myServo.write(myServoPos);    // tell servo to go to position in variable 'myServoPos'
                 delay(15);                    // waits 3 sec for the servo to reach the position
             }
             myServo.detach();   // detaches the servo library from pin 10   
             delay(2000);

             myServo.attach(srvoMtr);   // attaches the servo library to pin 10
             for (myServoPos = 100; myServoPos >= 0; myServoPos -= 1) { // goes from 100 degrees to 0 degrees in steps of 1 degree
               myServo.write(myServoPos);    // tell servo to go to position in variable 'pos
               delay(15);
             }
             myServo.detach();   // detaches the servo library from pin 10   
          }    
          myServoFlag=0;                // changes myServoFlag value back to zero (0)     
          digitalWrite(ledLight, HIGH);   // turn on light to indicate chocolate is dispensed

          if (ledLight == HIGH){
             digitalWrite(tonePin, HIGH); // turn on Piezo speaker 

             // Play Melody
             tone(tonePin, 233, 203.992851562);
             delay(226.658723958);
             delay(4.82252604167);
             tone(tonePin, 184, 412.325976562);
             delay(458.139973958);
             delay(4.82252604167);
             tone(tonePin, 184, 99.8262890625);
             delay(110.918098958);
             delay(4.82252604167);
             tone(tonePin, 155, 99.8262890625);
             delay(110.918098958);
             delay(4.82252604167);
             tone(tonePin, 184, 203.992851562);
             delay(226.658723958);
             delay(4.82252604167);
             tone(tonePin, 184, 203.992851562);
             delay(226.658723958);
             delay(236.303776042);
             tone(tonePin, 184, 99.8262890625);
             delay(110.918098958);
             delay(4.82252604167);
             tone(tonePin, 155, 99.8262890625);
             delay(110.918098958);
             delay(4.82252604167);
             tone(tonePin, 184, 203.992851562);
             delay(226.658723958);
             delay(4.82252604167);
             tone(tonePin, 184, 203.992851562);
             delay(226.658723958);
             delay(236.303776042);
             tone(tonePin, 184, 203.992851562);
             delay(226.658723958);
             delay(236.303776042);
             tone(tonePin, 233, 555.555);
          }    
          digitalWrite(tonePin, LOW);    // turns off Piezo speaker

          digitalWrite(pwmChA, HIGH);   // enable power to stepper
          digitalWrite(pwmChB, HIGH);   // enable power to stepper
          myStepper.step(-120);         // step backward 120 steps to position 0
          digitalWrite(pwmChA, LOW);    // disable power to stepper
          digitalWrite(pwmChB, LOW);    // disable power to stepper

          digitalWrite(pButton3, LOW);   // initialize pin 6 as LOW (pin voltage less than 1.5V)
          digitalWrite(ledLight, LOW);   // turns off light to indicate the sequence is completed

          pButtonFlag=0;   // changes pButton3Flag value back to zero (0)
          delay(2000); 
     }
    // position 4
    if (pButton4State == HIGH) {
       pButtonFlag=1;   // change pButtonFlag variable
    }
       if (pButtonFlag == 1){
          digitalWrite(pwmChA, HIGH);   // enable power to stepper
          digitalWrite(pwmChB, HIGH);   // enable power to stepper
          myStepper.step(160);          // step forward 160 steps to position 3
          digitalWrite(pwmChA, LOW);    // disable power to stepper
          digitalWrite(pwmChB, LOW);    //disable power to stepper
          delay(2000);

          myServoFlag=1;   // change ServoFlag variable to dispense chocolate
          Serial.println("\nServo Flag = 1 - Dispense Chocolate");

          if (myServoFlag == 1){
             myServo.attach(srvoMtr);   // attaches the servo library to pin 10
             for (myServoPos = 0; myServoPos <= 100; myServoPos += 1) { // goes from 0 degrees to 100 degrees in steps of 5 degrees
                 myServo.write(myServoPos);    // tell servo to go to position in variable 'myServoPos'
                 delay(15);                    // waits 3 sec for the servo to reach the position
             }
             myServo.detach();   // detaches the servo library from pin 10   
             delay(2000);

             myServo.attach(srvoMtr);   // attaches the servo library to pin 10
             for (myServoPos = 100; myServoPos >= 0; myServoPos -= 1) { // goes from 100 degrees to 0 degrees in steps of 1 degree
               myServo.write(myServoPos);    // tell servo to go to position in variable 'pos
               delay(15);
             }
             myServo.detach();   // detaches the servo library from pin 10   
          }    
          myServoFlag=0;   // changes myServoFlag value back to zero (0)     
          digitalWrite(ledLight, HIGH);   // turn on light to indicate chocolate is dispensed

          if (ledLight == HIGH){
             digitalWrite(tonePin, HIGH); // turn on Piezo speaker 

             // Play Melody
             tone(tonePin, 233, 203.992851562);
             delay(226.658723958);
             delay(4.82252604167);
             tone(tonePin, 184, 412.325976562);
             delay(458.139973958);
             delay(4.82252604167);
             tone(tonePin, 184, 99.8262890625);
             delay(110.918098958);
             delay(4.82252604167);
             tone(tonePin, 155, 99.8262890625);
             delay(110.918098958);
             delay(4.82252604167);
             tone(tonePin, 184, 203.992851562);
             delay(226.658723958);
             delay(4.82252604167);
             tone(tonePin, 184, 203.992851562);
             delay(226.658723958);
             delay(236.303776042);
             tone(tonePin, 184, 99.8262890625);
             delay(110.918098958);
             delay(4.82252604167);
             tone(tonePin, 155, 99.8262890625);
             delay(110.918098958);
             delay(4.82252604167);
             tone(tonePin, 184, 203.992851562);
             delay(226.658723958);
             delay(4.82252604167);
             tone(tonePin, 184, 203.992851562);
             delay(226.658723958);
             delay(236.303776042);
             tone(tonePin, 184, 203.992851562);
             delay(226.658723958);
             delay(236.303776042);
             tone(tonePin, 233, 555.555);
          }    
          digitalWrite(tonePin, LOW);    // turns off Piezo speaker

          digitalWrite(pwmChA, HIGH);   // enable power to stepper
          digitalWrite(pwmChB, HIGH);   // enable power to stepper
          myStepper.step(-160);         // step backward 160 steps to position 0
          digitalWrite(pwmChA, LOW);    // disable power to stepper
          digitalWrite(pwmChB, LOW);    // disable power to stepper

          digitalWrite(pButton3, LOW);   // initialize pin 6 as LOW (pin voltage less than 1.5V)
          digitalWrite(ledLight, LOW);   // turns off light to indicate the sequence is completed

          pButtonFlag=0;   // changes pButton3Flag value back to zero (0)
          delay(2000);          
       }
}  

marked as duplicate by Juraj, sempaiscuba, VE7JRO, MatsK, gre_gor Feb 15 at 6:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Cut-and-Paste programming? – Mikael Patel Feb 11 at 21:28
  • i do not think that anyone will waste their time trying to figure out which line causes the compile error – jsotola Feb 11 at 21:31
  • 3
    your code is overly complicated for no reason ..... you have 4 almost identical code blocks .... the only difference between them is a number of steps ..... you could easily use a function block that accepts the number of steps as an argument ...... that way you have the code block only once and it will work for any number of buttons – jsotola Feb 11 at 21:54
  • 2
    one other thing ..... you write code without thinking about what the code does ..... for instance you have a command myServoFlag=1; which is fine, but the next command is if (myServoFlag == 1){ ............. that if statement is always true, so there is no reason to test the value of myServoFlag ...................... it is like saying paint the box black, if the box is black then draw a white circle ...... instead it should be paint the box black, draw a white circle – jsotola Feb 11 at 21:59
  • void tone();{ is just wrong; this should be a/your compilation error. consider refactoring your code. – esoterik Feb 12 at 0:07
0

First off

Even if compiled your code will not work; in the tone function reference you can read:

Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega).

You seem to want to use both pins 3 & 11 for PWM output; you cannot also use the Tone library while using these pins. Although you never actually called analogWrite(...) on these pins so it probably won't do what you want anyway.

Second

the line:

void tone();{

is absolutely wrong; one cannot declare a function within another function in C++ or C. And this isn't even a valid definition either. This is probably your compilation error, your compiler should tell you what line number this is on!

Third

It seems you don't quite understand what the code you are attempting to use even does. I would assume that you haven't read, or understood, the documentation.

digitalWrite(tonePin, HIGH); // turn on Piezo speaker

The tone library doesn't work like this; at best this does nothing, at worst it disrupts the tone library

myServo.attach(srvoMtr);   // attaches the servo library to pin 10
for (myServoPos = 100; myServoPos >= 0; myServoPos -= 1) { // goes from 100 degrees to 0 degrees in steps of 1 degree
  myServo.write(myServoPos);    // tell servo to go to position in variable 'pos
  delay(15);
}
myServo.detach(); 

This is not how you use the servo library! .attach() is generally used in setup, .detach() is almost never used.

    digitalWrite(pwmChA, HIGH);   // enable power to stepper
    digitalWrite(pwmChB, HIGH);   // enable power to stepper
    myStepper.step(40);          // step forward 40 steps to position 1
    digitalWrite(pwmChA, LOW);    // disable power to stepper
    digitalWrite(pwmChB, LOW);    //disable power to stepper

This is not how you use the stepper library! Steppers are generally powered 100% of the time. When un-powered they cannot generate torque or hold a position! Also those pins aren't associated in any way with the stepper, you assigned dirChA and dirChB to the stepper. The stepper library assumes it is talking to a stepper driver, not driving the stepper itself.

Finally

Your code repeats itself too much, and is too verbose, and does many nonsensical things or things that result in nothing or something very different that the comments imply was intended.

You need to structure you code better, refactor into re-useable functions and remove code that doesn't do anything

  • Thank you, Esoterik, for taking the time to respond. I'm new to programming, but eager to learn. Any positive instruction I can receive in this forum will only contribute to my learning. That said, I've made a number of changes and the code now compiles. However, the results are unstable. Likely to the inherent conflict with pins 3 & 11 and the use of the tone function. What if I changed to midi()? I've submitted a new question "Code causing Servo motor not to work" with my revised code. I hope you can help. – Stevie_D Feb 12 at 23:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.