Pot controlled DC Motor

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
cottimonster
 
Posts: 2
Joined: Mon Mar 31, 2014 8:17 pm

Pot controlled DC Motor

Post by cottimonster »

Im trying (very unsuccessfully) to control a DC motor with a pot and a motorshield. I have very little (none before last month) knowledge in programming and am looking for some help. This is the code i have so far'

Code: Select all

#include <AFMotor.h>


AF_DCMotor motor(2);

const int PotPin = A0; // Change this if pot is connected to another pin

int direction = FORWARD;   // Where we are going
int speed = 0;            // How fast
int potVal = 506;         // Current potvalue

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
 
  motor.setSpeed(255);
 
  motor.run(RELEASE);
}

void loop() {
  // read the input on the pot:
  potVal = analogRead(PotPin);
  if (potVal < 506)   
  { // Smaller than 506 so we're going forward
    direction = FORWARD;
    // Map value from pot-range to motorspeed-range
    speed = map(potVal, 0, 506, 255, 0);
  }
  else
  { // Otherwise, we're going backward
    direction = BACKWARD;
    // Map value from pot-range to motorspeed-range
    speed = map(potVal, 507, 1024, 0, 255);
  }  

  // print out the variables:
  Serial.print("Read: ");
  Serial.print(potVal);
  Serial.print(" direction: ");
  if (direction==FORWARD)
    Serial.print("Forward");
  else
    Serial.print("Backward");
  Serial.print(" Speed: ");
  Serial.println(speed);   // End of line, so also make it go a line

 
  delay(1);        // delay in between reads for stability
}

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Pot controlled DC Motor

Post by adafruit_support_bill »

Post a photo of the front and back of your shield and we'll see if we can spot any problems.

cottimonster
 
Posts: 2
Joined: Mon Mar 31, 2014 8:17 pm

Re: Pot controlled DC Motor

Post by cottimonster »

I ran the adafruit motor test and everything ran perfect and the serial monitor reads correctly so i think the error is in the mapping the pot value code(which i have no idea if im doing it correctly)

thanks for the help
Attachments
adafruit front.jpg
adafruit front.jpg (650.87 KiB) Viewed 149 times

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Pot controlled DC Motor

Post by adafruit_support_bill »

Code: Select all

void loop() {
  // read the input on the pot:
  potVal = analogRead(PotPin);
You read the pot correctly.

Code: Select all

  if (potVal < 506)   
  { // Smaller than 506 so we're going forward
    direction = FORWARD;
    // Map value from pot-range to motorspeed-range
    speed = map(potVal, 0, 506, 255, 0);
  }
  else
  { // Otherwise, we're going backward
    direction = BACKWARD;
    // Map value from pot-range to motorspeed-range
    speed = map(potVal, 507, 1024, 0, 255);
  }  
You map the speed and direction correctly.

Code: Select all

  // print out the variables:
  Serial.print("Read: ");
  Serial.print(potVal);
  Serial.print(" direction: ");
  if (direction==FORWARD)
    Serial.print("Forward");
  else
    Serial.print("Backward");
  Serial.print(" Speed: ");
  Serial.println(speed);   // End of line, so also make it go a line

 
  delay(1);        // delay in between reads for stability
}
You print out the speed and direction. But you never set the speed and direction on the motor.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”