1

I'm using an Adafruit V1 Motor Shield for coding two 12 volt DC motors. In my code I'm changing the set speed, but the motors still seem to be going the same speed, which is really fast, and I want them to go slow. What do I change to make my motors speed run the way I want them to run?

Here's my code below:

#include <AFMotor.h>

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);

void setup() {
  Serial.begin(9600); // set up Serial library at 9600 bps
  Serial.println("Motor test!");
  // turn on motor
  motor1.setSpeed(3);
  motor2.setSpeed(5);
}

void loop() {
  uint8_t i;

  //second motor
  Serial.print("tick");

  motor2.run(FORWARD);

  for (i=0; i<255; i++) {
    motor2.setSpeed(i);  
    delay(10);
  }

  for (i=255; i!=0; i--) {
    motor2.setSpeed(i);  
    delay(10);
  }
 
  Serial.print("tick");
  
  motor2.run(FORWARD);

  for (i=0; i<255; i++) {
    motor2.setSpeed(i);  
    delay(10);
  }
 
  for (i=255; i!=0; i--) {
    motor2.setSpeed(i);  
    delay(10);
  }

  Serial.print("tick");
  
  motor2.run(FORWARD);

  for (i=0; i<255; i++) {
    motor2.setSpeed(i);  
    delay(10);
  }
 
  for (i=255; i!=0; i--) {
    motor2.setSpeed(i);  
    delay(10);
  }

  Serial.print("tick");
  
  motor2.run(FORWARD);

  for (i=0; i<255; i++) {
    motor2.setSpeed(i);  
    delay(10);
  }
 
  for (i=255; i!=0; i--) {
    motor2.setSpeed(i);  
    delay(10);
  }

  Serial.print("tick");
  
  motor2.run(FORWARD);

  for (i=0; i<255; i++) {
    motor2.setSpeed(i);  
    delay(10);
  }
 
  for (i=255; i!=0; i--) {
    motor2.setSpeed(i);  
    delay(10);
  }

  //1 motor
  Serial.print("tick");
  
  motor1.run(FORWARD);

  for (i=0; i<255; i++) {
    motor1.setSpeed(i);  
    delay(10);
  }
 
  for (i=255; i!=0; i--) {
    motor1.setSpeed(i);  
    delay(10);
  }

  Serial.print("tick");
  
  motor2.run(FORWARD);

  for (i=0; i<255; i++) {
    motor2.setSpeed(i);  
    delay(10);
  }
 
  for (i=255; i!=0; i--) {
    motor2.setSpeed(i);  
    delay(10);
  }
 
  Serial.print("tick");
  
  motor2.run(FORWARD);

  for (i=0; i<255; i++) {
    motor2.setSpeed(i);  
    delay(10);
  }
 
  for (i=255; i!=0; i--) {
    motor2.setSpeed(i);  
    delay(10);
  }
 
  Serial.print("tack");
  
  motor1.run(BACKWARD);

  for (i=0; i<255; i++) {
    motor1.setSpeed(i);  
    delay(10);
  }
 
  for (i=255; i!=0; i--) {
    motor1.setSpeed(i);  
    delay(10);
  }
}
11
  • delete all of the code inside loop() ... what happens then? – jsotola Jan 11 at 2:34
  • What do I change to make my motors speed run the way I want them to run? ... how is anyone supposed to guess how you want the motors to run? – jsotola Jan 11 at 2:36
  • @jsotola I mean by like how do i change the speed? – user71592 Jan 11 at 2:56
  • please read my first comment – jsotola Jan 11 at 2:57
  • @jsotola when I delete all of the code inside void loop(), nothing happens. – user71592 Jan 11 at 3:03

Your Answer

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

Browse other questions tagged or ask your own question.