Pages: [1]   Go Down
Author Topic: (HELP!) Using a potentiometer to control a stepper motor.  (Read 831 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8962
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

#7 below:

http://arduino.cc/forum/index.php/topic,148850.0.html
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24450
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
abs(diff);   
And that?

Also, watch those limited erase/write cycles for the EPROM.
« Last Edit: April 13, 2013, 04:35:48 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Opps. This is my code with the Code tags.
Code:
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.
}
 

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
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:
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:
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.




Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24450
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your help. I found a easier way to accomplish my goal, codewise and it works!
Logged

Pages: [1]   Go Up
Jump to: