Hi! I have been having some trouble with a code I copied off the internet. it has been slightly modified to fit my project, any suggestions?


#include <AFMotor.h>
#include "AnimateBilly.h"

#define NUM_MOTORS 2
AF_DCMotor head(1, MOTOR12_1KHZ); // create motor #2, 64KHz pwm
AF_DCMotor mouth(2, MOTOR12_1KHZ);
int sensorPin = A0;    // select the input pin for the audio signal
int ledPin = 13;


void runMotorOnOff(AF_DCMotor motor, int aniDelay = 500) {
  motor.run(FORWARD);
  delay(aniDelay);
  motor.run(BACKWARD);
  delay(5);
  motor.run(RELEASE);
  delay(aniDelay);
}

void runTwoMotorsOnOff(AF_DCMotor motor1, AF_DCMotor motor2, int aniDelay = 500) {
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  delay(aniDelay);
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  delay(5);
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  delay(aniDelay);
}

bool randomDecisionToTurnOn(int oddsOn) {
  int score = random(1, 100);
  return score < oddsOn;
}

void releaseMotor(AF_DCMotor motor) {
  motor.run(BACKWARD);
  delay(15);
  motor.run(RELEASE);
}


void dance(int duration, int times = 2) {
  int elapsedTime = 0;
  int iterDuration = duration / times;
  for (int i = 0; i < times; i++) {
    if (randomDecisionToTurnOn(60))
    }
  }
}

void talk(int duration, int wordCount) {
  int iterDuration = duration / wordCount;
  head.run(FORWARD);
  for (int i = 0; i < wordCount; i++) {
    runMotorOnOff(mouth, iterDuration);
    int sampleSensor(int pin, int samples = 20, int duration = 250) {
      float sensorValue = 0;
      int iterDelay = duration / samples;
      for (int i = 0; i < samples; i++) {
        sensorValue += analogRead(pin);
        delay(iterDelay);
      }
      digitalWrite(ledPin, LOW);
      return (int)(sensorValue / samples);
    }

    AF_DCMotor motors[NUM_MOTORS] = {head, mouth};
    void setupFishMotors() {
      Serial.begin(9600);           // set up Serial library at 9600 bps
      Serial.println("Motor test!");
      for (int i = 0; i < NUM_MOTORS; i++) {
        motors[i].setSpeed(255);
        int sampleAverages(int pin, int samples = 3, int duration = 100) {
          int iterDelay = duration / samples;
          int avgValue = 0;
          for (int i = 0; i < samples; i++) {
            avgValue += sampleSensor(pin, samples, iterDelay);
            delay(iterDelay);
          }
        }

        void billySleep() {
          head.run(RELEASE);
          tail.run(RELEASE);
         
        }

        void moveBilly(int movementCount, int duration) {
          if (movementCount > 3) {
            billySleep();
            dance(duration, random(1, 4));
            return (int)(avgValue / samples);
          }

          void setup() {
            setupFishMotors();
            // declare the ledPin as an OUTPUT:
            pinMode(ledPin, OUTPUT);
            Serial.begin(9600);      // open the serial port at 9600 bps:
          }

          int stillPlaying = 0;
          int aniDuration = 500;
          int previousValue = 0;
          int quietThreshold = 2;

          void loop() {
            // read the value from the sensor:
            int sensorValue = sampleSensor(sensorPin);
            float smoothedSensorValue = (sensorValue + previousValue) / 2 ;
            if (smoothedSensorValue > quietThreshold) {
              digitalWrite(ledPin, HIGH);
              stillPlaying = min(stillPlaying++, 4);
              moveBilly(stillPlaying, aniDuration);
            } else if (stillPlaying == 0) {
              billySleep();
              digitalWrite(ledPin, LOW);
              stillPlaying = 0;
            } else {
              talk(duration, random(2, 6));
              stillPlaying--;
            }
            previousVPreformatted textalue = sensorValue;
          }
my error messages were:
Arduino: 1.8.19 (Windows 10), Board: "Arduino Uno"





















C:\Users\johns\OneDrive\Documents\Arduino\trout\trout.ino:4:0: warning: "NUM_MOTORS" redefined

 #define NUM_MOTORS 2

 

In file included from C:\Users\johns\OneDrive\Documents\Arduino\trout\trout.ino:2:0:

C:\Users\johns\OneDrive\Documents\Arduino\libraries\billy/AnimateBilly.h:3:0: note: this is the location of the previous definition

 #define NUM_MOTORS 3

 

trout:5:16: error: redefinition of 'AF_DCMotor head'

 AF_DCMotor head(1, MOTOR12_1KHZ); // create motor #2, 64KHz pwm

                ^

In file included from C:\Users\johns\OneDrive\Documents\Arduino\trout\trout.ino:2:0:

C:\Users\johns\OneDrive\Documents\Arduino\libraries\billy/AnimateBilly.h:4:12: note: 'AF_DCMotor head' previously declared here

 AF_DCMotor head(1, MOTOR12_1KHZ); // create motor #2, 64KHz pwm

            ^~~~

trout:6:17: error: redefinition of 'AF_DCMotor mouth'

 AF_DCMotor mouth(2, MOTOR12_1KHZ);

                 ^

In file included from C:\Users\johns\OneDrive\Documents\Arduino\trout\trout.ino:2:0:

C:\Users\johns\OneDrive\Documents\Arduino\libraries\billy/AnimateBilly.h:5:12: note: 'AF_DCMotor mouth' previously declared here

 AF_DCMotor mouth(2, MOTOR12_1KHZ);

            ^~~~~

C:\Users\johns\OneDrive\Documents\Arduino\trout\trout.ino: In function 'void runMotorOnOff(AF_DCMotor, int)':

trout:11:6: error: redefinition of 'void runMotorOnOff(AF_DCMotor, int)'

 void runMotorOnOff(AF_DCMotor motor, int aniDelay = 500) {

      ^~~~~~~~~~~~~

In file included from C:\Users\johns\OneDrive\Documents\Arduino\trout\trout.ino:2:0:

C:\Users\johns\OneDrive\Documents\Arduino\libraries\billy/AnimateBilly.h:9:6: note: 'void runMotorOnOff(AF_DCMotor, int)' previously defined here

 void runMotorOnOff(AF_DCMotor motor, int aniDelay = 500){

      ^~~~~~~~~~~~~

C:\Users\johns\OneDrive\Documents\Arduino\trout\trout.ino: In function 'void runTwoMotorsOnOff(AF_DCMotor, AF_DCMotor, int)':

trout:20:6: error: redefinition of 'void runTwoMotorsOnOff(AF_DCMotor, AF_DCMotor, int)'

 void runTwoMotorsOnOff(AF_DCMotor motor1, AF_DCMotor motor2, int aniDelay = 500) {

      ^~~~~~~~~~~~~~~~~

In file included from C:\Users\johns\OneDrive\Documents\Arduino\trout\trout.ino:2:0:

C:\Users\johns\OneDrive\Documents\Arduino\libraries\billy/AnimateBilly.h:18:6: note: 'void runTwoMotorsOnOff(AF_DCMotor, AF_DCMotor, int)' previously defined here

 void runTwoMotorsOnOff(AF_DCMotor motor1, AF_DCMotor motor2, int aniDelay = 500){

      ^~~~~~~~~~~~~~~~~

C:\Users\johns\OneDrive\Documents\Arduino\trout\trout.ino: In function 'bool randomDecisionToTurnOn(int)':

trout:32:6: error: redefinition of 'bool randomDecisionToTurnOn(int)'

 bool randomDecisionToTurnOn(int oddsOn) {

      ^~~~~~~~~~~~~~~~~~~~~~

In file included from C:\Users\johns\OneDrive\Documents\Arduino\trout\trout.ino:2:0:

C:\Users\johns\OneDrive\Documents\Arduino\libraries\billy/AnimateBilly.h:30:6: note: 'bool randomDecisionToTurnOn(int)' previously defined here

 bool randomDecisionToTurnOn(int oddsOn) {

      ^~~~~~~~~~~~~~~~~~~~~~

C:\Users\johns\OneDrive\Documents\Arduino\trout\trout.ino: In function 'void releaseMotor(AF_DCMotor)':

trout:37:6: error: redefinition of 'void releaseMotor(AF_DCMotor)'

 void releaseMotor(AF_DCMotor motor) {

      ^~~~~~~~~~~~

In file included from C:\Users\johns\OneDrive\Documents\Arduino\trout\trout.ino:2:0:

C:\Users\johns\OneDrive\Documents\Arduino\libraries\billy/AnimateBilly.h:35:6: note: 'void releaseMotor(AF_DCMotor)' previously defined here

 void releaseMotor(AF_DCMotor motor) {

      ^~~~~~~~~~~~

C:\Users\johns\OneDrive\Documents\Arduino\trout\trout.ino: In function 'void dance(int, int)':

trout:44:6: error: redefinition of 'void dance(int, int)'

 void dance(int duration, int times = 2) {

      ^~~~~

In file included from C:\Users\johns\OneDrive\Documents\Arduino\trout\trout.ino:2:0:

C:\Users\johns\OneDrive\Documents\Arduino\libraries\billy/AnimateBilly.h:47:6: note: 'void dance(int, int)' previously defined here

 void dance(int duration, int times = 1){

      ^~~~~

trout:49:5: error: expected primary-expression before '}' token

     }

     ^

C:\Users\johns\OneDrive\Documents\Arduino\trout\trout.ino: At global scope:

trout:51:1: error: expected declaration before '}' token

 }

 ^

exit status 1

redefinition of 'AF_DCMotor head'



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

type or paste code here

Did it compile and work before you modified it? Go back to the original and do your mods one at a time.

It did not compile when I first tried it which led me to modify.

And then it did compile correctly?

At the least you have a lot of problems with the curly brackets in the code. There are several missing/misplaced. Look at the code after formatting with the autoformat function (ctrl-t).

Where do you get the AnimateBilly.h file.

I thought Billy had three motors and apparently, his library does too.

You could give your num motors a new name and find and replace throughout your program. Should get you closer to compiling at any rate.

It looks like a lot of functions and variables that are defined in AnimateBilly.h are also defined in your sketch. Does the AnimateBilly library come with an example you can start with? Look for it in:
File->Examples->AnimateBilly->exaplename

If you don't find an example included in the library, you can find one here: