Go Down

Topic: Have i done something bad? (Read 310 times) previous topic - next topic

MrBear

Jul 24, 2013, 04:42 pm Last Edit: Jul 24, 2013, 04:46 pm by MrBear Reason: 1
Hi
I am using the PID library to create a servomotor (DC motor with a POT) but had some problems making my motor turn in both directions. What i did was to change the Input and Setpoint pin hereby making the setpointpin the inputpin. ...... it seems to work but..?
Code: [Select]


#include <PID_v1.h>

double error;

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,20,0,0, DIRECT);

void setup()
{

TCCR2B = 0x01;   // Timer 2: PWM 3 & 11 @ 32 kHz
Serial.begin(9600);

 //initialize the variables we're linked to
 Input = analogRead(A3);
 Setpoint = analogRead(A1);

 //turn the PID on
 myPID.SetMode(AUTOMATIC);
}

void loop()
{
 double HJULraw = analogRead(A1); //reading the 2 pots to calculate an error
 double RATraw = analogRead(A3);
 error = (RATraw-HJULraw);
   
 if(error >= 2) {
   TurnR();
 }   else if (error <= -2) {
   TurnL();
 } else {
   digitalWrite(11, LOW);
    digitalWrite(3, LOW);
 }
 }

 
void TurnR() {
 Setpoint = analogRead(A3);  
 Input = analogRead(A1);
 myPID.Compute();
 Output = map(Output, 0, 255, 0, 150);
 analogWrite(3, 0);
 analogWrite(11,Output);
  Cprint();
 }
 
 
void TurnL() {
 Setpoint = analogRead(A1); // THIS IS WHERE THE TWO PINS ARE CHANGED COMPARED TO Void TurnR
 Input = (analogRead(A3));
 myPID.Compute();
 Output = map(Output, 0, 255, 0, 150);
 analogWrite(11, 0);
 analogWrite(3, Output);
 Cprint();
 }

void Cprint() {
Serial.write("error: ");
Serial.print(error);
 Serial.write("  potRAT: ");
Serial.print(analogRead(A3));
 Serial.write("  potHJUL: ");
Serial.print(analogRead(A1));
 Serial.write("  Output: ");
Serial.println(Output);

delay(100);
}



I dont know if this is the way to do it?

Thanks for your help.

pajaraca

I'm not a expert but I think your code is out void loop ().

holmes4

Auto format your code and then look at it again! ( and repost it) the layouts so poor I can't make head or tail of it.

Mark

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy