Very easy code question - code embedded

ive got my main code but its dependant on audio
thi is a test code, on the original draft user was using a different motor shield
i am using the v2 motorshield

ive added the lines to included the library at the top of the code
but to test my motors, how do i rename it in the rest of the code?

included screenshot with wrror, and code below. just wanted to test the 3 motors in sequence to make sure the motors are done correctly

#include <Adafruit_MotorShield.h>
#define NUM_MOTORS 3

AF_DCMotor head(1, MOTOR12_1KHZ);
AF_DCMotor mouth(2, MOTOR12_1KHZ);
AF_DCMotor tail(3, MOTOR34_1KHZ);

AF_DCMotor motors[NUM_MOTORS] = {head, mouth, tail};
void setup() {
  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);
  }
}

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

void loop() {
  runMotorOnOff(head, 1000);
  runMotorOnOff(mouth, 500);
  runMotorOnOff(tail, 1000);
}

i didnt expect to get so many weird errors back from simple code. curious what to change so i can test the 3 motors, thanks in advance

Where did you see that class in the examples?


#include <Adafruit_MotorShield.h>
#define NUM_MOTORS 3

Adafruit_DCMotor(1, MOTOR12_1KHZ);
Adafruit_DCMotor(2, MOTOR12_1KHZ);
Adafruit_DCMotor(3, MOTOR34_1KHZ);

Adafruit_DCMotor motors[NUM_MOTORS] = {head, mouth, tail};
void setup() {
  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);
  }
}

void runMotorOnOff(AdaFruit_DCMotor motor , int animationDelay = 500) {
  motor.run(FORWARD);
  delay(animationDelay);
  motor.run(BACKWARD);
  delay(5);
  motor.run(RELEASE);
  delay(animationDelay / 2);
}

void loop() {
  runMotorOnOff(head, 1000);
  runMotorOnOff(mouth, 500);
  runMotorOnOff(tail, 1000);
}

ive fixed those for now. as i realized i understood those ones but now errors i dont understand
current code state above with new errors below

If you have a Adafruit Motorshield v2, then this is the page to start: https://www.adafruit.com/product/1438

There is a link on that page to a tutorial. Go through the whole tutorial: https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino.

Start with a single motor and have a look at the examples at Github. For example this one: https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library/blob/master/examples/DCMotorTest/DCMotorTest.ino

If that works, then you can try with three motors.

The difference between a beginner and a advanced Arduino user is that a beginner tries to rush to the end result, and a advanced Arduino user does a project step by step.

1 Like

+1

This applies well beyond arduinos, too.

i already figured it out. whos rushing anything? i ask questions as i go, as anybody whos EAGER to learn will. you were once new too buddy, Communities are there to ask questions and work together.

I'm glad you have it working. Could you share the solution ?

Why did you post a picture of the error messages, and not the error messages themselves?

Sure I was a newcomer too. And as a newcomer beeing un-experienced I wrote programs this way:

write minimum 100 lines of code and then start testing.
The result was: I had 5 bugs in the code which where influencing each other.
So with trying to correct the first bug this took me a lot of time because there were so many lines of code which caused the bug.
It took looong to find the first bug.

Then I tried to do a "quick fix" without fully understanding my own code.
This caused aditional bugs.

All in all it took 50 hours to make the code work

Today (and as each every experienced and / or professional software-developper) I write code this way:

Write a simple test-program which has only a few lines of code and test it until this testcode works reliable. This took 1 hour time.

Then testing the next small thing
Write a simple test-program for the "second" function which has only a few lines of code and test it until this testcode works reliable. This took 1 hour time.

Putting those two functions together and test again
took 0,5 hours time

repeated this with the next three functions and have the code up and running
after 7 hours. Compared to 50 hours with a lot of head-scratching and frustration.

And additionally doing modifications of the code and test them runs down pretty quick.

You can have "luck" that throwing together parts of other peoples code does work.
Seems to be so in your case. And this might lead to a programming-style of writing many lines then start testing.
Experienced users try to help you with saying: in the long run adding one thing at a time then immitiately test will finish your project faster because searching for the bug will be fast as there are only a few lines to search through!

Trying to be fast will turn out to slow you down.

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.