I'm currently working on a vacuum pressure controller for another project I have in the works. Basically I'm using a 12v vacuum pump connected to a motorshield with a vacuum pressure sensor that output an analog signal between 0 and 1023. The sensor itself is rated for a vacuum pressure range between 0 and 16 in/hg. I know I could probably optimize the code and get the controller to work a bit more smoothly, but I'm still pretty new to all this, so all things considered, I'm surprised I've come this far. What the controller is supposed to do is vary the vacuum pressure of the pump based on predefined pressures stored in an array. I need it to modify the pump pressure based on the predefined pressures at predefined intervals. So far, that works. What I'm having trouble with is that I can't get it to repeat the entire sketch more than twice. My suspicion is that the arduino is running out of memory, or that I'm using the wrong types of variable. My suspicion is that I may need to implement a timer library, which I did try at one point, with little to no success. After it cycles through all the variables a second time, it will either hold the last pressure in the array, or it will randomly kick the pump on and off. My hope is that maybe someone can take a look at my code and tell me where I went wrong, or if I do need to add a timer library of some kind, possibly recommend a simple one, and possibly get me started in the right direction on how to implement it. Thanks in advance guys!
const int analogInPin = A0; //pressure sensor pin assignment
const int pwmPin = 3; //pump speed control pin assignment
const int dirPin = 12; //controls pump direction
//sets the vaccuum pressure for each pump cycle
const int pumpPressure[] = {
5, 10, 5, 10, 5, 10, 5, 10 };
//sets the duration of each pump cycle (in seconds)
long unsigned interval[] = {
10, 5, 10, 5, 10, 5, 10, 5 };
unsigned long startTime = millis();
unsigned long currentInterval = 0;
int lastValue = 0;
int sensorValue = 0;
int outputValue = 0;
int pumpSpeed = 100;
int cycleCount = 0;
int sensorMap = 0;
int totalCycleCount = 1;
void setup() {
pinMode(dirPin, OUTPUT);
Serial.begin(9600);
digitalWrite(dirPin, HIGH);
sensorMap = analogRead(analogInPin); //calibrates the pressure sensor
analogWrite(pwmPin, pumpSpeed); //starts the pump at the predetermined speed
}
void loop() {
for(int a = 0; a < 8; a++) {
cycleCount = a;
currentInterval = (currentInterval + (interval[a] * 1000));
while(millis() - startTime < currentInterval)
{
pressureSensor();
pressureRegulate();
pressureSerial();
if(a == 8) {
a = 0;
}
}
}
}
void pressureSensor() { //reads the pressure sensor and then maps it to a value between 0 and 16 in/hg
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, sensorMap, 0, 0, 16);
outputValue = constrain(outputValue, 0, 16);
lastValue = outputValue;
}
void pressureRegulate() { //adjusts the pump speed based on the desired pressure for that cycle
if(lastValue == pumpPressure[cycleCount]) {
analogWrite(pwmPin, pumpSpeed);
}
if(lastValue > pumpPressure[cycleCount]) {
pumpSpeed--;
pumpSpeed = constrain (pumpSpeed, 0, 255);
analogWrite(pwmPin, pumpSpeed);
}
if(lastValue < pumpPressure[cycleCount]) {
pumpSpeed++;
pumpSpeed = constrain (pumpSpeed, 0, 255);
analogWrite(pwmPin, pumpSpeed);
}
}
void pressureSerial() { //outputs the desired pressure and actual pressure to serial monitor
Serial.print("set pressure = " );
Serial.print(pumpPressure[cycleCount]);
Serial.print("\t pressure = ");
Serial.println(output value);
}