Go Down

Topic: (HELP!) Using a potentiometer to control a stepper motor. (Read 1 time) previous topic - next topic

ClovenSet121

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.

zoomkat

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

AWOL

#2
Apr 13, 2013, 11:29 pm Last Edit: Apr 13, 2013, 11:35 pm by AWOL Reason: 1
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.
"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.

ClovenSet121

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



ClovenSet121


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.





AWOL

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

ClovenSet121

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

Go Up