/*
Copyright 2009-12 Stephan Martin, Dominik Gummel
This file is part of PID-Boost.
PID-Boost is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PID-Boost is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Multidisplay. If not, see .
*/
/*
TODO:
- Ladedrucklimit einstellbar und skalierung entsprechend
- Serielle Ausgabe
- Umrechnung auf Bar
- PID Reglertest
*/
//PID Lib:
#include
//Variables:
int potiValue = 0; // value read from the poti
//PID Lib Stuff, see: http://arduino.cc/playground/Code/PIDLibrary
double Setpoint, Input, Output; //PID Variables
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT); //PID Setup
//Setup:
void setup() {
//Set the Pinmodes:
pinMode(3, OUTPUT); //(Red Led)
pinMode(5, OUTPUT); //(Green Led)
pinMode(7, INPUT); //(Onboard Button)
pinMode(9, OUTPUT); //(N75 Drive)
//Analog Input Overview:
//Analog In 0 = Boost Input (MPXA6400AP)
//Analog In 1 = External Potentiometer
//Analog In 2 = Supply Voltage
//Analog In 3 = External Switch (unused wire currently, possible AnaIn DigIn DigOut)
//lower the PWM Frequency
setPwmFrequency(9, 64);
//Serial On:
Serial.begin(9600);
//Check the Boost Sensor:
//Turn On the Green LED:
digitalWrite(5, HIGH);
//Init the PID Regulator
myPID.SetMode(AUTOMATIC);
myPID.SetSampleTime(200);
}
void loop() {
//Fetch the Current Boost level
Input = analogRead(0);
//Fetch the desired Boost level
Setpoint = analogRead(1);
//Compute the PID Value
myPID.Compute();
//Set the N75 Value
analogWrite(9,Output);
//Debug Output:
analogWrite(3,Output);
//Serial Output:
}
void setPwmFrequency(int pin, int divisor) {
byte mode;
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
switch(divisor) {
case 1:
mode = 0x01;
break;
case 8:
mode = 0x02;
break;
case 64:
mode = 0x03;
break;
case 256:
mode = 0x04;
break;
case 1024:
mode = 0x05;
break;
default:
return;
}
if(pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | mode;
}
else {
TCCR1B = TCCR1B & 0b11111000 | mode;
}
}
else if(pin == 3 || pin == 11) {
switch(divisor) {
case 1:
mode = 0x01;
break;
case 8:
mode = 0x02;
break;
case 32:
mode = 0x03;
break;
case 64:
mode = 0x04;
break;
case 128:
mode = 0x05;
break;
case 256:
mode = 0x06;
break;
case 1024:
mode = 0x7;
break;
default:
return;
}
TCCR2B = TCCR2B & 0b11111000 | mode;
}
}