Pages: [1]   Go Down
Author Topic: Have i done something bad?  (Read 258 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

#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.
« Last Edit: July 24, 2013, 09:46:01 am by MrBear » Logged

Recife Brasil
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 51
Posts: 2276
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: