DC motor

Hi all !

I’ve bought my Adruino uno board yesterday because i finally wanted to learn some code writing. So i tried to make a stepper controller using the L298 chip and I made it. I went on and now I want to make a DC motor spin in both ways using a potentiometer. the way it should work is that in the middle the motor is standing still and than goes faster in each direction.
Thats the code I made Please comment it so I can make it Better.

int mot1a=4;
int mot1b=5;
int potValue=A0;
int val;
void setup() {

pinMode(mot1a,OUTPUT);
pinMode(mot1b,OUTPUT);

}

void loop() {
  {
  val = analogRead(potValue);
  val = map(val, 0, 1023, 1, 5);
  }
  
  for(int potValue = 0 ; potValue <= 500; potValue +=1) {
  digitalWrite(mot1a, potValue);
  digitalWrite(mot1b, LOW);
  }
   for(int potValue = 500 ; potValue <= 523; potValue +=1) {
  digitalWrite(mot1a, LOW);
  digitalWrite(mot1b, LOW);
  }
  for(int potValue = 523 ; potValue >= 1023; potValue -=1) {
  digitalWrite(mot1b, potValue);
  digitalWrite(mot1a, LOW);
  }}

Thanks in advance !

Having "potValue" as both a pin number (constant-ish) and a variable is sort-of confusing, don't you think?

The braces around the analogue read /map are unnecessary, and you don't appear to anything with "val"

  for(int potValue = 523 ; potValue >= 1023; potValue -=1) {

Starting with potValue equal 523, execute the body of the loop while potValue is greater than or equal to 1023, and decrement potValue by 1 after each iteration.

How many times will the body of the loop be executed?

  digitalWrite(mot1a, potValue);
  digitalWrite(mot1b, LOW);

The digitalWrite function expects the second argument to be HIGH or LOW, not 27 or 157 or 500.

   for(int potValue = 500 ; potValue <= 523; potValue +=1) {
  digitalWrite(mot1a, LOW);
  digitalWrite(mot1b, LOW);
  }

Now this is useful. Turn both pins off 24 times, while doing nothing else.

OK I see. I've made everything wrong, so what would then be the right code ?

I'd start here:

int potPin=A0;

what would then be the right code ?

That rather depends on what you want to do.

it's nothing specific im only experimenting. As I already said is that I would like to make a motor stand still when the potentiometer is in the middle and than start spinning faster the way I rotate the potentiometer.

So I found a code on this side:http://arduino.cc/en/Tutorial/AnalogInOutSerial And modified it so it looks like this:

/*
  Analog input, analog output, serial output

 Reads an analog input pin, maps the result to a range from 0 to 255
 and uses the result to set the pulsewidth modulation (PWM) of an output pin.
 Also prints the results to the serial monitor.

 The circuit:
 * potentiometer connected to analog pin 0.
   Center pin of the potentiometer goes to the analog pin.
   side pins of the potentiometer go to +5V and ground
 * LED connected from digital pin 9 to ground

 created 29 Dec. 2008
 Modified 4 Sep 2010
 by Tom Igoe

 This example code is in the public domain.

 */

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 5; // Analog output pin that the LED is attached to
const int analogOutPin1 = 4;
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);            
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 1024, 512, 0, 255);  
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);

// read the analog in value:
  sensorValue = analogRead(analogInPin);            
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 512, 1024, 0, 255);  
  // change the analog out value:
  analogWrite(analogOut, outputValue);    



  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);      
  Serial.print("\t output = ");      
  Serial.println(outputValue);   

  // wait 10 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(10);                     
}

Now the only problem is that the motor spins fast on each side (thats OK) but when approaching the middle motor gets to zero too fast so the cycle begins again and then it goes the other way. what did i do wrong ?

  // map it to the range of the analog out:
  outputValue = map(sensorValue, 1024, 512, 0, 255);

The range of values from analogRead is from 0 to 1023. What value do you get in outputValue when the potentiometer reading is 135? Not what you expect, I'd guess.

  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 512, 1024, 0, 255);

You read the value again, and map it using a completely different (also incorrect) range.

You should be mapping the output from (0, 1023) to (0, 512). Then, if the value is less then 256, set the direction pin LOW and analogWrite the other value to a PWM pin.

If the value is greater than 256, set the direction pin HIGH, and analogWrite 512 minus the value to a PWM pin.

This way, at either extreme of the pot, the motor is going its fastest, and near the middle it is going slow. At precisely the middle, the direction changes.

so first when the sensor reads 135 the output is at -190. My rage goes from: sensor 0, output -255 to sensor 1023, output 254.

now how do i set the direction pin LOW, and HIGH at the right values?

Using less than or greater than zero.

how do i write that as a code ?

Use an 'if' statement. Maybe even an 'else' for good measure.

what did I do wrong ?

 if (sensorValue < 512)
  {
    analogWrite(analogOutPin, HIGH);
    analogWrite(analogOut, LOW);
  }
  else if (sensorValue > 512)
  {
    analogWrite(analogOut, HIGH);
    analogWrite(analogOutPin, LOW);
  }

what did I do wrong ?

You mean besides only pasting a little bit of your code, and not telling us what the problem is?

so that part is right ?:smiley:

/*
  Analog input, analog output, serial output
 
 Reads an analog input pin, maps the result to a range from 0 to 255
 and uses the result to set the pulsewidth modulation (PWM) of an output pin.
 Also prints the results to the serial monitor.
 
 The circuit:
 * potentiometer connected to analog pin 0.
   Center pin of the potentiometer goes to the analog pin.
   side pins of the potentiometer go to +5V and ground
 * LED connected from digital pin 9 to ground
 
 created 29 Dec. 2008
 Modified 4 Sep 2010
 by Tom Igoe
 
 This example code is in the public domain.
 
 */

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 5; // Analog output pin that the LED is attached to
const int analogOut = 4;
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);            
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1024, 0, 255);  
  // change the analog out value:
  if (sensorValue < 512)
  {
    analogWrite(analogOutPin, HIGH);
    analogWrite(analogOut, LOW);
  }
  else if (sensorValue > 512)
  {
    analogWrite(analogOut, HIGH);
    analogWrite(analogOutPin, LOW);
  }

  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);      
  Serial.print("\t output = ");      
  Serial.println(outputValue);   

  // wait 10 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(10);                     
}

Here is the whole thing :stuck_out_tongue: the problem is that nothing happens

const int analogInPin = A0;

A0 is the value to use when using an analog pin (pin 0) as a digital pin. Stupid ass addition to WProgram.h, if you ask me.

You want to use 0, not A0.

    analogWrite(analogOut, LOW);

Pin 4 is not a PWM pin, so you can't use analogWrite to write to that pin.

What happens when sensor value IS 512?

OK Thanks !! that made it work but now how do I get it to run faster each way?

that made it work but now how do I get it to run faster each way?

Make what run faster? The code? Or the motor?

I mean the motor I would like to make it stand still when the pot. is in the middle and then spin faster each way when I turn the potentiometer.

Try re-reading Reply #7. You were on the right track.