Awesome Throttle PID

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 ?
:bulb:
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);

}


For direction I win… About the rest …¬¬

int Pot = A0; 
int Stepp = 2;
int Dir = 3;
int ValorMan = 0; 
int last = 0;
int current = 0;
int difference = 0;


void setup() 
{
  pinMode(Stepp, OUTPUT); 
  pinMode(Dir, OUTPUT); 
  pinMode(A0, INPUT);
}


void loop() 
{
  ValorMan = analogRead(Pot);
    {
       ValorMan = analogRead(Pot);
  ValorMan = map(ValorMan, 0, 1023, 0, 360);
    }
    
    {
    last = analogRead(Pot); 
    delay(100);
    current = analogRead(Pot);
    delay(100) ;
    }
        
    {
      difference = last - current;         //Calculate the difference between the last known acceleration and the current acceleration 
    }
    
    if (difference > 10)
    {          
  digitalWrite(Stepp, LOW);
    }
    if (difference < -10) 
    {      
  digitalWrite(Stepp, HIGH);
    }
    
}

Hello.

I don’t see any questions or description of problem in your posts. I suggest you simplify your code to the smallest bit that does not work and then tell us what you want it to do and what it actually does. Also, it looks like you have not modified your schematic based on my comments in this thread.

- Ryan

Hello RyanTM ,
Thanks for your reply!
I have not made ​​changes because they knew it, was not a mistake :slight_smile: but thank you for guiding me.
After burning many neurons could make the PID software and it worked perfectly. Oh that some photos of the project working. Now I have to create routines to improve the scales and adjustments to the degrees needed. If you have suggestions for improving the program. Thank you very much.

Down there in this program…

//============================================================================================================
//                                                     PROGRAMA DO LEO                                       =
//============================================================================================================
//
//============================================================================================================
//                                                       VARIAVEIS                                           =
//============================================================================================================

int Pot = A0; 
int PotM = A1;
int Stepp = 2;
int Dir = 3;
int ValorMan = 0; 
int ValorMot = 0;
int val3=10;
int last = 0;
int current = 0;
int difference = 0;
//int y = 0;
//int b = 0;
int c =0; 

//============================================================================================================
//                                                      SETUP                                                =
//============================================================================================================
void setup() 
{
  pinMode(Stepp, OUTPUT); 
  pinMode(Dir, OUTPUT); 
  
}

//============================================================================================================
//                                                      SETUP                                                =
//============================================================================================================
void loop()

{
LEITURA: 
  
    ValorMan = analogRead(Pot); //Read POT MOTOR
    ValorMot = analogRead(PotM); //Read POT Control THROTTLE
    ValorMan = map(ValorMan, 0, 1023, 0, 360); // Degrees 1 turn
    ValorMot = map(ValorMot, 0, 1023, 0, 360); // Degrees 1 turn 
    
    last = ValorMan; //Read POT Control THROTTLE
    current = ValorMot;  //Read POT Motor
     
    difference = last - current;  //Difference between two POTs
    

 c = abs (difference); // positive value always 
  
  
  // LEFT or RIGHT 
  
   if (difference > 1/100 ) 
    {          
  digitalWrite(Dir, LOW); 
    }
   if (difference < 1/100) 
    {      
  digitalWrite(Dir, HIGH); 
    }
                                                                                                                                                                                                                                              

                                                                                                                       
      if (c >= 3) // Pulses to the driver . If <= 3 Stop pulses , means motor in position of the throttle. 
      
      
      {
         digitalWrite(Stepp, HIGH);
         delay(1/10000);
         digitalWrite(Stepp, LOW);
         delay(1/10000);
       }
goto LEITURA;
    

}

IMAGES ----->






People typically do not use GOTO when programming in C++ unless they really have to. I would suggest changing it to be an infinite while loop like:

while(1) 
{
  your code here;
}

Nice, pictures. Your throttle control looks very nice. What are you making?

- Ryan