0

When I try to compile the following sketch, the compiler returns the error "exit status 1 - expected primary-expression before '(' token", and highlights "midi();" in my code.

Here is my sketch:

#include <Stepper.h>
#include <Servo.h>
#include <MIDI.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 = 1;     // assigning name "tonePin" to pin 1

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() {
//Serial.begin(31250);   // initialize the serial port for messaging & troubleshooting
 //Serial.println("Running UNO Setup");

 // 1 Stepper Motor
// Serial.println("\nStepper Motor");
 pinMode(dirChA, OUTPUT);   // initialize pin 12 as an OUTPUT
 digitalWrite(dirChA, LOW);   //initialize pin 12 as LOW (pin voltage is 0 V)
// Serial.println("Channel A Dir - PIN 12 - set as Output & Low");
 pinMode(dirChB, OUTPUT);   // initialize pin 13 as an OUTPUT
 digitalWrite(dirChB, LOW);   //intialize pin 13 as LOW (pin voltage is 0 V)
// Serial.println("Channel B Dir - PIN 13 - set as Output & Low");
 pinMode(pwmChA, OUTPUT);   // initialize pin 3 as an OUTPUT
 digitalWrite(pwmChA, LOW);   //intialize pin 3 as LOW (pin voltage is 0 V)
// Serial.println("Channel A PWM - PIN 3 - set as Output & Low");
 pinMode(pwmChB, OUTPUT);   // initialize pin 11 as an OUTPUT
 digitalWrite(pwmChB, LOW);   //intialize pin 11 as LOW (pin voltage is 0 V)
// Serial.println("Channel B PWM - PIN 11 - set as Output & Low");
 pinMode(brkChA, OUTPUT);   // initialize pin 9 as an OUTPUT
 digitalWrite(brkChA, LOW);   //intialize pin 9 as LOW (pin voltage is 0 V)
// Serial.println("Channel A Brake - PIN 9 - set as Output & Low");
 pinMode(brkChB, OUTPUT);   // initialize pin 8 as an OUTPUT
 digitalWrite(brkChB, LOW);   //intialize pin 8 as LOW (pin voltage is 0 V)
// Serial.println("Channel B Brake - PIN 8 - set as Output & Low");

 myStepper.setSpeed(60);   // set the speed of the stepper motor to 60 RPM
// Serial.println("\nSet Stepper Speed to 60 RPM");

 // 4 Push Buttons
// Serial.println("\n4 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)
// Serial.println("Push Button 1 - PIN 4 - set a Input & Low"); 
 pinMode(pButton2, INPUT);   // initialize pin 5 as an INPUT
 digitalWrite(pButton2, LOW);   // initialize pin 5 as LOW (pin voltage less than 1.5V)
// Serial.println("Push Button 2 - PIN 5 - set a Input & Low");
 pinMode(pButton3, INPUT);   // initialize pin 6 as an INPUT
 digitalWrite(pButton3, LOW);   // initialize pin 6 as LOW (pin voltage less than 1.5V)
// Serial.println("Push Button 3 - PIN 6 - set a Input & Low");
 pinMode(pButton4, INPUT);   // initialize pin 7 as an INPUT
 digitalWrite(pButton4, LOW);   // initialize pin 7 as LOW (pin voltage less than 1.5V)
// Serial.println("Push Button 4 - PIN 7 - set a Input & Low");  

 // 1 Led Light
// Serial.println("\nLED Light");
 pinMode(ledLight, OUTPUT);   // initialize pin 2 as an OUTPUT
 digitalWrite(ledLight, LOW);   // initialize pin 2 as LOW (pin voltage is 0 V)
// Serial.println("LED Light - PIN 2 - set to Output & Low");

 // 1 Piezo Speaker
midi();
// Serial.println("Piezo Speaker Inialized");
}

void loop() {
  //Read button state (pressed or not pressed?)
//  Serial.println("\nReading button 1 thru 4 states - high or low");
  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) {
 //    Serial.println("\nPush Button 1 Pressed");
     pButtonFlag=1;  // change pButton1Flag variable
 //    Serial.println("Button 1 flag = 1");
  }
     if (pButtonFlag == 1){
 //       Serial.println("Stepper Start - Forward 40 steps");
        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
 //       Serial.println("Stepper Stop");
        delay(2000);

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

        if (myServoFlag == 1){
 //          Serial.println("Servo Start");
           myServo.attach(srvoMtr);   // attaches the servo library to pin 10
 //          Serial.println("Attach 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   
 //          Serial.println("\nDetach Servo Library to PIN 10");
 //          Serial.println("Servo Stop");
 //          Serial.println("Hold position OPEN for 3 sec");
           delay(2000);

           myServo.attach(srvoMtr);   // attaches the servo library to pin 10
 //          Serial.println("Attach 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   
 //          Serial.println("\nDetach Servo Library to PIN 10");
 //          Serial.println("Servo Stop - Awaiting next selection");
        }    
        myServoFlag=0;                // changes myServoFlag value back to zero (0)     
 //       Serial.println("Servo Flag = 0");

        digitalWrite(ledLight, HIGH);   // turn on light to indicate chocolate is dispensed
 //       Serial.println("\nLed Light ON");
 //       Serial.println("\nPlay Tones");

        void midi() {
                 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);
                 }

        delay(3000);

//        Serial.println("Stepper Start - Return to position zero");    
        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
//        Serial.println("Stepper Stop");

        digitalWrite(pButton1, LOW);   // initialize pin 4 as LOW (pin voltage less than 1.5V)
//        Serial.println("\nReset Button 1 - PIN 4 - Low");
        digitalWrite(ledLight, LOW);   //turns off light to indicate the sequence is completed
//        Serial.println("\nLed Light OFF");

        pButtonFlag=0;                // changes pButtonFlat value back to zero (0)
//        Serial.println("\Button 1 Flag = 0");
        delay(2000); 
     }

  // position 2
  if (pButton2State == HIGH) {
//     Serial.println("\nPush Button 2 Pressed");
     pButtonFlag=1;  // change pButtonFlag variable
//     Serial.println("Button 2 flag = 1");
  }
     if (pButtonFlag == 1){
//        Serial.println("Stepper Start - Forward 80 steps");
        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
//        Serial.println("Stepper Stop");
        delay(2000);

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

        if (myServoFlag == 1){
//           Serial.println("Servo Start");
           myServo.attach(srvoMtr);   // attaches the servo library to pin 10
//           Serial.println("Attach 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   
//           Serial.println("\nDetach Servo Library to PIN 10");
//           Serial.println("Servo Stop");
//           Serial.println("Hold position OPEN for 2 sec");
           delay(2000);

           myServo.attach(srvoMtr);   // attaches the servo library to pin 10
//           Serial.println("Attach 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   
//           Serial.println("\nDetach Servo Library to PIN 10");
//           Serial.println("Servo Stop - Awaiting next selection");
        }    
        myServoFlag=0;                // changes myServoFlag value back to zero (0)     
//        Serial.println("Servo Flag = 0");

        digitalWrite(ledLight, HIGH);    // turn on light to indicate chocolate is dispensed
//        Serial.println("\nLed Light ON");
        delay(3000);

//        Serial.println("Stepper Start - Return to position zero");    
        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
//        Serial.println("Stepper Stop");

        digitalWrite(pButton2, LOW);   // initialize pin 5 as LOW (pin voltage less than 1.5V)
//        Serial.println("\nReset Button 2 - PIN 5 - Low");
        digitalWrite(ledLight, LOW);   // turns off light to indicate the sequence is completed
//        Serial.println("\nLed Light OFF");

        pButtonFlag=0;   // changes pButton2Flag value back to zero (0)
//        Serial.println("\Button 2 Flag = 0");
        delay(2000); 
    }

    // position 3
    if (pButton3State == HIGH) {
//       Serial.println("\nPush Button 3 Pressed");
       pButtonFlag=1;   // change pButtonFlag variable
//       Serial.println("Button 3 flag = 1");
    }
       if (pButtonFlag == 1){
//          Serial.println("Stepper Start - Forward 120 steps");
          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
//          Serial.println("Stepper Stop");
          delay(2000);

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

          if (myServoFlag == 1){
//            Serial.println("Servo Start");
             myServo.attach(srvoMtr);   // attaches the servo library to pin 10
//             Serial.println("Attach 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   
//             Serial.println("\nDetach Servo Library to PIN 10");
//             Serial.println("Servo Stop");
//             Serial.println("Hold position OPEN for 2 sec");
             delay(2000);

             myServo.attach(srvoMtr);   // attaches the servo library to pin 10
//             Serial.println("Attach 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   
//             Serial.println("\nDetach Servo Library to PIN 10");
//             Serial.println("Servo Stop - Awaiting next selection");
          }    
          myServoFlag=0;                // changes myServoFlag value back to zero (0)     
//          Serial.println("Servo Flag = 0");

          digitalWrite(ledLight, HIGH);   // turn on light to indicate chocolate is dispensed
//          Serial.println("\nLed Light ON");
          delay(3000);

//          Serial.println("Stepper Start - Return to position zero");    
          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
//          Serial.println("Stepper Stop");

          digitalWrite(pButton3, LOW);   // initialize pin 6 as LOW (pin voltage less than 1.5V)
//          Serial.println("\nReset Button 3 - PIN 6 - Low");
          digitalWrite(ledLight, LOW);   // turns off light to indicate the sequence is completed
//          Serial.println("\nLed Light OFF");

          pButtonFlag=0;   // changes pButton3Flag value back to zero (0)
//          Serial.println("\Button 3 Flag = 0");
          delay(2000); 
     }
    // position 4
    if (pButton4State == HIGH) {
//       Serial.println("\nPush Button 4 Pressed");
       pButtonFlag=1;   // change pButtonFlag variable
//       Serial.println("Button 3 flag = 1");
    }
       if (pButtonFlag == 1){
//          Serial.println("Stepper Start - Forward 160 steps");
          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
//          Serial.println("Stepper Stop");
          delay(2000);

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

          if (myServoFlag == 1){
//             Serial.println("Servo Start");
             myServo.attach(srvoMtr);   // attaches the servo library to pin 10
//             Serial.println("Attach 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   
//             Serial.println("\nDetach Servo Library to PIN 10");
//             Serial.println("Servo Stop");
//             Serial.println("Hold position OPEN for 2 sec");
             delay(2000);

             myServo.attach(srvoMtr);   // attaches the servo library to pin 10
//             Serial.println("Attach 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   
//             Serial.println("\nDetach Servo Library to PIN 10");
//             Serial.println("Servo Stop - Awaiting next selection");
          }    
          myServoFlag=0;   // changes myServoFlag value back to zero (0)     
//          Serial.println("Servo Flag = 0");

          digitalWrite(ledLight, HIGH);   // turn on light to indicate chocolate is dispensed
//          Serial.println("\nLed Light ON");
          delay(3000);

//          Serial.println("Stepper Start - Return to position zero");    
          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
//          Serial.println("Stepper Stop");

          digitalWrite(pButton3, LOW);   // initialize pin 6 as LOW (pin voltage less than 1.5V)
//          Serial.println("\nReset Button 3 - PIN 6 - Low");
          digitalWrite(ledLight, LOW);   // turns off light to indicate the sequence is completed
//          Serial.println("\nLed Light OFF");

          pButtonFlag=0;   // changes pButton3Flag value back to zero (0)
//          Serial.println("\Button 4 Flag = 0");
          delay(2000);          
       }
}  

I'm including the "midi();" function to play a short melody through an attached Piezo speaker (buzzer) once the led is activated.

When I compile the midi code on its own in a separate sketch it works (see below).

int tonePin = 1;
void setup() {
 midi ();
}

void midi() {
    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);

   }

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

}

I've searched the Internet for an answer, but have found none that relate directly with what I'm attempting to make work. Can you help me, please?

3

You have tried to define void midi() {...} inside the loop() function. But you cannot nest function definitions. Your definition (implementation) of midi() should be outside any other function.

Like this:

#include <Stepper.h>
#include <Servo.h>
#include <MIDI.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 = 1;     // assigning name "tonePin" to pin 1

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 midiplay() {
  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);
}


void setup() {
  //Serial.begin(31250);   // initialize the serial port for messaging & troubleshooting
  //Serial.println("Running UNO Setup");

  // 1 Stepper Motor
  // Serial.println("\nStepper Motor");
  pinMode(dirChA, OUTPUT);   // initialize pin 12 as an OUTPUT
  digitalWrite(dirChA, LOW);   //initialize pin 12 as LOW (pin voltage is 0 V)
  // Serial.println("Channel A Dir - PIN 12 - set as Output & Low");
  pinMode(dirChB, OUTPUT);   // initialize pin 13 as an OUTPUT
  digitalWrite(dirChB, LOW);   //intialize pin 13 as LOW (pin voltage is 0 V)
  // Serial.println("Channel B Dir - PIN 13 - set as Output & Low");
  pinMode(pwmChA, OUTPUT);   // initialize pin 3 as an OUTPUT
  digitalWrite(pwmChA, LOW);   //intialize pin 3 as LOW (pin voltage is 0 V)
  // Serial.println("Channel A PWM - PIN 3 - set as Output & Low");
  pinMode(pwmChB, OUTPUT);   // initialize pin 11 as an OUTPUT
  digitalWrite(pwmChB, LOW);   //intialize pin 11 as LOW (pin voltage is 0 V)
  // Serial.println("Channel B PWM - PIN 11 - set as Output & Low");
  pinMode(brkChA, OUTPUT);   // initialize pin 9 as an OUTPUT
  digitalWrite(brkChA, LOW);   //intialize pin 9 as LOW (pin voltage is 0 V)
  // Serial.println("Channel A Brake - PIN 9 - set as Output & Low");
  pinMode(brkChB, OUTPUT);   // initialize pin 8 as an OUTPUT
  digitalWrite(brkChB, LOW);   //intialize pin 8 as LOW (pin voltage is 0 V)
  // Serial.println("Channel B Brake - PIN 8 - set as Output & Low");

  myStepper.setSpeed(60);   // set the speed of the stepper motor to 60 RPM
  // Serial.println("\nSet Stepper Speed to 60 RPM");

  // 4 Push Buttons
  // Serial.println("\n4 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)
  // Serial.println("Push Button 1 - PIN 4 - set a Input & Low");
  pinMode(pButton2, INPUT);   // initialize pin 5 as an INPUT
  digitalWrite(pButton2, LOW);   // initialize pin 5 as LOW (pin voltage less than 1.5V)
  // Serial.println("Push Button 2 - PIN 5 - set a Input & Low");
  pinMode(pButton3, INPUT);   // initialize pin 6 as an INPUT
  digitalWrite(pButton3, LOW);   // initialize pin 6 as LOW (pin voltage less than 1.5V)
  // Serial.println("Push Button 3 - PIN 6 - set a Input & Low");
  pinMode(pButton4, INPUT);   // initialize pin 7 as an INPUT
  digitalWrite(pButton4, LOW);   // initialize pin 7 as LOW (pin voltage less than 1.5V)
  // Serial.println("Push Button 4 - PIN 7 - set a Input & Low");

  // 1 Led Light
  // Serial.println("\nLED Light");
  pinMode(ledLight, OUTPUT);   // initialize pin 2 as an OUTPUT
  digitalWrite(ledLight, LOW);   // initialize pin 2 as LOW (pin voltage is 0 V)
  // Serial.println("LED Light - PIN 2 - set to Output & Low");

  // 1 Piezo Speaker
  midiplay();
  // Serial.println("Piezo Speaker Inialized");
}

void loop() {
  //Read button state (pressed or not pressed?)
  //  Serial.println("\nReading button 1 thru 4 states - high or low");
  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) {
    //    Serial.println("\nPush Button 1 Pressed");
    pButtonFlag = 1; // change pButton1Flag variable
    //    Serial.println("Button 1 flag = 1");
  }
  if (pButtonFlag == 1) {
    //       Serial.println("Stepper Start - Forward 40 steps");
    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
    //       Serial.println("Stepper Stop");
    delay(2000);

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

    if (myServoFlag == 1) {
      //          Serial.println("Servo Start");
      myServo.attach(srvoMtr);   // attaches the servo library to pin 10
      //          Serial.println("Attach 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
      //          Serial.println("\nDetach Servo Library to PIN 10");
      //          Serial.println("Servo Stop");
      //          Serial.println("Hold position OPEN for 3 sec");
      delay(2000);

      myServo.attach(srvoMtr);   // attaches the servo library to pin 10
      //          Serial.println("Attach 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
      //          Serial.println("\nDetach Servo Library to PIN 10");
      //          Serial.println("Servo Stop - Awaiting next selection");
    }
    myServoFlag = 0;              // changes myServoFlag value back to zero (0)
    //       Serial.println("Servo Flag = 0");

    digitalWrite(ledLight, HIGH);   // turn on light to indicate chocolate is dispensed
    //       Serial.println("\nLed Light ON");
    //       Serial.println("\nPlay Tones");

    midiplay();
    delay(3000);

    //        Serial.println("Stepper Start - Return to position zero");
    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
    //        Serial.println("Stepper Stop");

    digitalWrite(pButton1, LOW);   // initialize pin 4 as LOW (pin voltage less than 1.5V)
    //        Serial.println("\nReset Button 1 - PIN 4 - Low");
    digitalWrite(ledLight, LOW);   //turns off light to indicate the sequence is completed
    //        Serial.println("\nLed Light OFF");

    pButtonFlag = 0;              // changes pButtonFlat value back to zero (0)
    //        Serial.println("\Button 1 Flag = 0");
    delay(2000);
  }

  // position 2
  if (pButton2State == HIGH) {
    //     Serial.println("\nPush Button 2 Pressed");
    pButtonFlag = 1; // change pButtonFlag variable
    //     Serial.println("Button 2 flag = 1");
  }
  if (pButtonFlag == 1) {
    //        Serial.println("Stepper Start - Forward 80 steps");
    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
    //        Serial.println("Stepper Stop");
    delay(2000);

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

    if (myServoFlag == 1) {
      //           Serial.println("Servo Start");
      myServo.attach(srvoMtr);   // attaches the servo library to pin 10
      //           Serial.println("Attach 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
      //           Serial.println("\nDetach Servo Library to PIN 10");
      //           Serial.println("Servo Stop");
      //           Serial.println("Hold position OPEN for 2 sec");
      delay(2000);

      myServo.attach(srvoMtr);   // attaches the servo library to pin 10
      //           Serial.println("Attach 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
      //           Serial.println("\nDetach Servo Library to PIN 10");
      //           Serial.println("Servo Stop - Awaiting next selection");
    }
    myServoFlag = 0;              // changes myServoFlag value back to zero (0)
    //        Serial.println("Servo Flag = 0");

    digitalWrite(ledLight, HIGH);    // turn on light to indicate chocolate is dispensed
    //        Serial.println("\nLed Light ON");
    delay(3000);

    //        Serial.println("Stepper Start - Return to position zero");
    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
    //        Serial.println("Stepper Stop");

    digitalWrite(pButton2, LOW);   // initialize pin 5 as LOW (pin voltage less than 1.5V)
    //        Serial.println("\nReset Button 2 - PIN 5 - Low");
    digitalWrite(ledLight, LOW);   // turns off light to indicate the sequence is completed
    //        Serial.println("\nLed Light OFF");

    pButtonFlag = 0; // changes pButton2Flag value back to zero (0)
    //        Serial.println("\Button 2 Flag = 0");
    delay(2000);
  }

  // position 3
  if (pButton3State == HIGH) {
    //       Serial.println("\nPush Button 3 Pressed");
    pButtonFlag = 1; // change pButtonFlag variable
    //       Serial.println("Button 3 flag = 1");
  }
  if (pButtonFlag == 1) {
    //          Serial.println("Stepper Start - Forward 120 steps");
    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
    //          Serial.println("Stepper Stop");
    delay(2000);

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

    if (myServoFlag == 1) {
      //            Serial.println("Servo Start");
      myServo.attach(srvoMtr);   // attaches the servo library to pin 10
      //             Serial.println("Attach 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
      //             Serial.println("\nDetach Servo Library to PIN 10");
      //             Serial.println("Servo Stop");
      //             Serial.println("Hold position OPEN for 2 sec");
      delay(2000);

      myServo.attach(srvoMtr);   // attaches the servo library to pin 10
      //             Serial.println("Attach 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
      //             Serial.println("\nDetach Servo Library to PIN 10");
      //             Serial.println("Servo Stop - Awaiting next selection");
    }
    myServoFlag = 0;              // changes myServoFlag value back to zero (0)
    //          Serial.println("Servo Flag = 0");

    digitalWrite(ledLight, HIGH);   // turn on light to indicate chocolate is dispensed
    //          Serial.println("\nLed Light ON");
    delay(3000);

    //          Serial.println("Stepper Start - Return to position zero");
    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
    //          Serial.println("Stepper Stop");

    digitalWrite(pButton3, LOW);   // initialize pin 6 as LOW (pin voltage less than 1.5V)
    //          Serial.println("\nReset Button 3 - PIN 6 - Low");
    digitalWrite(ledLight, LOW);   // turns off light to indicate the sequence is completed
    //          Serial.println("\nLed Light OFF");

    pButtonFlag = 0; // changes pButton3Flag value back to zero (0)
    //          Serial.println("\Button 3 Flag = 0");
    delay(2000);
  }
  // position 4
  if (pButton4State == HIGH) {
    //       Serial.println("\nPush Button 4 Pressed");
    pButtonFlag = 1; // change pButtonFlag variable
    //       Serial.println("Button 3 flag = 1");
  }
  if (pButtonFlag == 1) {
    //          Serial.println("Stepper Start - Forward 160 steps");
    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
    //          Serial.println("Stepper Stop");
    delay(2000);

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

    if (myServoFlag == 1) {
      //             Serial.println("Servo Start");
      myServo.attach(srvoMtr);   // attaches the servo library to pin 10
      //             Serial.println("Attach 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
      //             Serial.println("\nDetach Servo Library to PIN 10");
      //             Serial.println("Servo Stop");
      //             Serial.println("Hold position OPEN for 2 sec");
      delay(2000);

      myServo.attach(srvoMtr);   // attaches the servo library to pin 10
      //             Serial.println("Attach 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
      //             Serial.println("\nDetach Servo Library to PIN 10");
      //             Serial.println("Servo Stop - Awaiting next selection");
    }
    myServoFlag = 0; // changes myServoFlag value back to zero (0)
    //          Serial.println("Servo Flag = 0");

    digitalWrite(ledLight, HIGH);   // turn on light to indicate chocolate is dispensed
    //          Serial.println("\nLed Light ON");
    delay(3000);

    //          Serial.println("Stepper Start - Return to position zero");
    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
    //          Serial.println("Stepper Stop");

    digitalWrite(pButton3, LOW);   // initialize pin 6 as LOW (pin voltage less than 1.5V)
    //          Serial.println("\nReset Button 3 - PIN 6 - Low");
    digitalWrite(ledLight, LOW);   // turns off light to indicate the sequence is completed
    //          Serial.println("\nLed Light OFF");

    pButtonFlag = 0; // changes pButton3Flag value back to zero (0)
    //          Serial.println("\Button 4 Flag = 0");
    delay(2000);
  }
}
|improve this answer|||||
  • Thank you Jose! I appreciate your quick reply. So can I make a call to run the program outside the loop function. If yes, how? I'm not an expert. – Stevie_D Feb 6 '19 at 22:06
  • There is a difference between defining a function and calling a function. You must define it outside any other function. But you must call it from within setup() or loop() (or some other function that is called by those, etc.) To define the function, is where you say void midi() { blah blah code here}, and to call it you just write midi();. – jose can u c Feb 6 '19 at 22:35
  • Hi Jose, I'm a new-be to Arduino. I would need a detailed example in the form of a sketch, or if can, correct the sketch I've provided so that it will compile, please. Ok? – Stevie_D Feb 8 '19 at 19:45
  • You have already done it... In your second code block that "does work" because midi() is alone, you have the block that defines what midi() does and it's not inside setup() or loop(). And you have called midi(); from the setup() function. So, just move the definition of midi() {...} to a place that is not inside any other function. In the place in loop() or setup() where you want the action to take place, just call the function with midi(); – jose can u c Feb 8 '19 at 20:05
  • I'm sorry, Jose. I'm not getting it to work because I'm not following/understanding your explanation. – Stevie_D Feb 11 '19 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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