that works! thank you
this is what the code looks like now
/*
Created by Alan Sanchez 2010
*/
#include <PID_Beta6.h>
double Setpoint, Input, Output;
PID pid(&Input, &Output, &Setpoint,3,30,5);
//VARIABLES//
int motorpwm = 5;
int direc = 4;
int button = 2;
int pos;
int photo;
void setup() {
Serial.begin(9600);
pinMode(motorpwm, OUTPUT);
pinMode(direc, OUTPUT);
pinMode(button, INPUT); //declare button pin as input
Input = analogRead(A0);
Output = 0;
pid.SetOutputLimits(-400,400);
pid.SetMode(AUTO); //turn on PID
Setpoint = 313;
}
void loop() {
//digitalWrite(direc, HIGH);
if (digitalRead(button) == HIGH){ //if button is pressed go to end()
Serial.println("button has been pressed");
while(1) {
//pos = analogRead(A0); //read pot in analog pin A0
//photo = analogRead(A1); //read photosensor in analog pin A1
Input = analogRead(A0);
pid.Compute();
if (Output < 0) {
digitalWrite(direc, LOW);
}
else if (Output>0) {
digitalWrite(direc, HIGH);
}
else {
Output=0;
}
int drive = Output/4;
analogWrite(motorpwm, drive);
Serial.print("Pot Value = "); //print pot value
Serial.print(Input, DEC);
Serial.print(" ");
Serial.print("Output = "); //print photo value
Serial.println(drive, DEC);
}
}
}
all thats left to do is fine tune the PID. Again thanks for the library, it saves soooo much time.