Hello there! I’m new to the ardunio, I have to create a way to drive a stepper motor with a potentiometer, ie for each angle of the potentiometer is moved an angle equal to the stepper motor which will have a second pot to Feedback. Using a dedicated driver Polulu that I generate pulses on a pin and another pin I generate a pulse direction, which goes to a HIGH and LOW direction goes to another.
As follows in the drawing down there.
Thank you if you can help me.
Ai down a code that did this to test the analog inputs and some variables, then not progressed more.
Thanks
I have posted in Arduino Forum too…I think that’s no problem ?
MY FIRST TEST CODE
int PotMan = A0;
int PotMot = A1;
int StepPin = 2;
int DirPin = 3;
int ValorManete = 0;
int ValorMotor = 0;
int v1=0;
void setup() {
pinMode(StepPin, OUTPUT);
pinMode(DirPin, OUTPUT);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
}
void loop()
{
ValorManete = PotMan;
ValorManete = analogRead(PotMan);
{
ValorManete = analogRead(PotMan);
ValorManete = map(ValorManete, 0, 1023, 0, 630);
}
{
digitalWrite(StepPin, LOW);
if ( ValorManete <= 1 )
{
digitalWrite(StepPin, HIGH);
}
{
digitalWrite(StepPin, LOW);
{v1 = constrain(v1, 300, 500);}
if ( ValorManete == v1 )
{
digitalWrite(StepPin, HIGH);
}
}
{
digitalWrite(StepPin, LOW);
if ( ValorManete >= 629)
{
digitalWrite(StepPin, HIGH);
}
}
{
ValorMotor = PotMot;
ValorMotor = analogRead(PotMot);
{
ValorMotor = analogRead(PotMot);
ValorMotor= map(ValorMotor, 0, 1023, 0, 315);
}
{
digitalWrite(DirPin, LOW);
if ( ValorMotor <= 1 )
{
digitalWrite(DirPin, HIGH);
}
}
{
digitalWrite(DirPin, LOW);
if ( ValorMotor >= 315 )
{
digitalWrite(DirPin, HIGH);
}
}
}
}
}
This code I think can help us.
I found on Forum autor doublec4
arduino.cc/cgi-bin/yabb2/YaB … 590264/all
//Easydriver pins
int dirPin = 3;
int stepperPin = 12;
//Accelerometer pins (analog)
const int pinx = 1 ;
const int piny = 2 ;
const int pinz = 3 ;
const int SLP = 9;
//variables to store accelerometer output and keep track of stepper position
int valx = 0;
int valy = 0;
int valz = 0;
int last = 0;
int current = 0;
int difference = 0;
//setup
void setup() {
Serial.begin(9600);
pinMode(SLP, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(stepperPin, OUTPUT);
}
//Function for instructing the Easydriver
void step(boolean dir,int steps){
digitalWrite(dirPin,dir); //give Easydriver direction for stepper to move
delay(50);
for(int i=0;i<steps;i++){ //loop through the number of steps that the stepper motor must complete
digitalWrite(stepperPin, LOW);
digitalWrite(stepperPin, HIGH);
delayMicroseconds(1000);
}
}
//Main code body
void loop(){
digitalWrite(SLP, HIGH); //Make sure the accelerometer is "awake"
//Read the values from the accelerometer and assign them to the variables. For this simple program, only the y value will be used for now.
valx = analogRead(pinx);
valy = analogRead(piny);
valz = analogRead(pinz);
if (last == 0) { //For the first time through the loop, set the "last" value to y input from the accelerometer (multiplied by 5 so stepper motor moves more)
last = valy*5;
}
current = valy*5; //Set the "current" value to the y value (multiplied by 5 so stepper motor moves more)
difference = last - current; //Calculate the difference between the last known acceleration and the current acceleration
if (difference > 0) { //If the difference is positive, then it will tell the Easydrive to step the motor counter-clockwise
step(false, difference);
}
if (difference < 0) { //If the difference is a negative number, then it will tell the Easydriver to step the motor clockwise
difference = abs(difference);
step(true, difference);
}
last = current; //Set the last known to acceleration to the current one so it can be compared to the next reading
delay(50);
}