Arduino Forum

Using Arduino => Motors, Mechanics, Power and CNC => Topic started by: ClovenSet121 on Apr 13, 2013, 11:16 pm

Title: (HELP!) Using a potentiometer to control a stepper motor.
Post by: ClovenSet121 on Apr 13, 2013, 11:16 pm
Hi everyone.
I am working on a final project for a control systems class in college. I've had some programming experience, but am fairly new to arduino. For this part of my project, I am using a potentiometer to control a stepper motor. The analog values from the pot are converted to a value between 0 and 1023.

What I want:
-To keep track of the pot's last location by writing the value to EEPROM at the end of my code.
-When the program loops to the beginning, i read the pot's value and subtract it's last location from the value I stored in EEPROM.
-Take the absolute value of the difference, and use the difference in a while loop to control the number of steps.
(This makes sense to me, but if my logic is off, please let me know!)

MY PROBLEM:
-My program compiles fine, however, when I test it with the hardware...nothing happens!!

Any and all help is appreciated!!

MY CODE

int setpoint = A0;                         // Set point potentiometer.
int setpointValue = 0;                    //  Variable for set point potenetiometer value.

int Step = 8;                               //Pin 8 is step
int diff;                                     //The difference between the set point and value in memory.

#include <EEPROM.h>
int storedValue = 0;                     
int addr = 0;

void setup() {                           
pinMode(Step, OUTPUT);                 
}

void loop() {
  setpointValue = analogRead(setpoint);    // Read values from setpoint potentiometer. 
  storedValue = EEPROM.read(addr);         // Read addr in EEPROM and put value in storedValue.
  storedValue *4;                                  // Multiply by 4
 
  diff = setpointValue - storedValue;      // Take difference.
  abs(diff);                               // Take absoulute value of the difference.
 
if (diff > 0) {
   while(diff>0);                         
   {
    digitalWrite(Step, HIGH);
    delay(500);
    digitalWrite(Step, LOW);
    delay(500);
    diff--;
  }
}
else;
{                                         //Do nothing if difference = 0.
}

storedValue = analogRead(setpoint) / 4;   //Read the setpoint potentiometer 
EEPROM.write(addr, storedValue);          //Store this value in EEPROM.
Title: Re: (HELP!) Using a potentiometer to control a stepper motor.
Post by: zoomkat on Apr 13, 2013, 11:24 pm
#7 below:

http://arduino.cc/forum/index.php/topic,148850.0.html
Title: Re: (HELP!) Using a potentiometer to control a stepper motor.
Post by: AWOL on Apr 13, 2013, 11:29 pm
Code: [Select]
storedValue *4;  
It may be that the forum has mangled this code because it doesn't have any CODE TAGS, but what did you intend that bit of code to do?
Code: [Select]
abs(diff);    And that?

Also, watch those limited erase/write cycles for the EPROM.
Title: Re: (HELP!) Using a potentiometer to control a stepper motor.
Post by: ClovenSet121 on Apr 14, 2013, 11:21 pm
Opps. This is my code with the Code tags.
Code: [Select]

int setpoint = A0;                        // Setpoint potentiometer.
int setpointValue = 0;                    //  Variable for setpoint potenetiometer value.

int Step = 8;                             //Pin 8 is step
int diff;                                 //The difference between the setpoint and value in memory.

#include <EEPROM.h>
int storedValue = 0;                     
int addr = 0;

void setup() {                           
pinMode(Step, OUTPUT);                 
}

void loop() {
  setpointValue = analogRead(setpoint);    // Read values from setpoint potentiometer. 
  storedValue = EEPROM.read(addr);         // Read addr in EEPROM and put value in storedValue.
  storedValue *4;                          // Multiply by 4
 
  diff = setpointValue - storedValue;      // Take difference.
  abs(diff);                               // Take absoulute value of the difference.
 
if (diff > 0) {
   while(diff>0);                         
   {
    digitalWrite(Step, HIGH);
    delay(500);
    digitalWrite(Step, LOW);
    delay(500);
    diff--;
  }
}
else;
{                                         //Do nothing if difference = 0.
}

storedValue = analogRead(setpoint) / 4;   //Read the setpoint potentiometer 
EEPROM.write(addr, storedValue);          //Store this value in EEPROM.
}
 


Title: Re: (HELP!) Using a potentiometer to control a stepper motor.
Post by: ClovenSet121 on Apr 14, 2013, 11:32 pm

Code: [Select]
storedValue *4;  
It may be that the forum has mangled this code because it doesn't have any CODE TAGS, but what did you intend that bit of code to do?
Code: [Select]
abs(diff);    And that?

Also, watch those limited erase/write cycles for the EPROM.


From what I've read, a spot in EEPROM holds up to 8 bits, so up to 255. The analog to digital converter uses 10 bits, so 1024.
Code: [Select]
storedValue *4;  
Since the number of combinations is 255 I multiply by 4 to get the value back up in the 1024 range. Does that make sense?

Code: [Select]
abs(diff);   
My goal is to take the difference of the previous location( before the value changed) with the current position value.
So i take the difference of my storedValue*4 - setpointValue = diff.
If my setpoint value is larger than my storedValue then there is a negative number.
Taking the absolute value should eliminate this possibility.




Title: Re: (HELP!) Using a potentiometer to control a stepper motor.
Post by: AWOL on Apr 15, 2013, 08:47 am
Quote
Since the number of combinations is 255 I multiply by 4 to get the value back up in the 1024 range. Does that make sense?

Sort of, but you need to store the result of the multiplication.

Quote
Taking the absolute value should eliminate this possibility.

Only if you actually do something with the result of taking the absolute value, like assigning it to a variable.
Title: Re: (HELP!) Using a potentiometer to control a stepper motor.
Post by: ClovenSet121 on Apr 15, 2013, 11:53 pm
Thanks for your help. I found a easier way to accomplish my goal, codewise and it works!