Hi guys,
So I have this code that activates some stuff (pneunets, or PNs) via an arduino and breadboard circuit.
Right now, I'm trying to adapt the code from using just one set of inputs, maxPress and minPress,
for each of the four pins (and PNs) to an individual value of each one (i.e. maxPress1-4, minPress1-4). I'm at a bit of a loss as to how to do this. Would anybody have any ideas?
Thank you,
Zane
const int numRegs = 4; //the number of regulators total
int maxPress = 75; //the maximum pressure, divisible by 5
//int maxPress2 = 75;
//int maxPress3 = 75;
//int maxPress4 = 75;
int minPress = 0; //the minimum pressure, divisible by 5
//int minPress2 = 0;
//int minPress3 = 0;
//int minPress4 = 0;
int Run = 0;
int phase=0;
int stepTime= 500;
int RampTime = 1000; //inflate and deflate time in ms, divisible by 10
//unsigned long lastChangeTime = 0; //the time of the last time
//const int stepTime = 10; // the step interval time in ms
//const int stepNumber = RampTime/stepTime; // the number of times the pressure value is increased within the activation and deactivation period
//const int stepSize = round(pressRange/stepNumber); //how much the pressure increased with each step
//boolean Starting = true;
//-------------------- ARRAYS -----------------
//these pins correspond to regulators which correspond to the pneunets...
// Pin 3= Reg 4 = PN 3
// Pin 9= Reg 1 = PN 1
// Pin 10= Reg 3 = PN 2
// Pin 11= Reg 2 = PN 4
// Reg 1 (PN1) & Reg 2 (PN4) activate at the same time, Reg 3 (PN2) & Reg 4 (PN3) activate at the same time
int regPin[4] = {9,10,11,3}; // the pins in use in the arduino
int regPress[4] = {maxPress, minPress, maxPress-phrase, minPress+phrase}; // by giving this array differing starting values, I can set phases.
//int regPress[2] = {minPress, maxPress}; // by giving this array starting values, I can set phases.
unsigned long lastChangeTime[4]={0,0,0,0};
boolean Switch[4]={false, true,false,true}; //determines whether to use the deflation code (if True) or the inflation code (if False)
//boolean Switch[2]={true, false}; //determines whether to use the deflation code (if True) or the inflation code (if False)
int pressRange = maxPress - minPress;
//unsigned long stepTime = RampTime/pressRange;
//unsigned long stepNumber = RampTime/stepTime;
int stepNumber= round(RampTime/stepTime);
int stepSize=round(pressRange/stepNumber);
//------------------------------------------------------------------
//------------------------------- SETUP ----------------------------
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
setPwmFrequency(9, 1);
setPwmFrequency(3, 1);
setPwmFrequency(10, 1);
setPwmFrequency(11,1);
Serial.print("pressRange : ");
Serial.println(pressRange);
Serial.print("stepSize : ");
Serial.println(stepSize);
Serial.print("stepNumber : ");
Serial.println(stepNumber);
// pressRange = maxPress - minPress;
// stepTime = RampTime/pressRange;
// stepNumber = RampTime/stepTime;p
}
//------------------------------------------------------------------
//------------------------------- LOOP -----------------------------
void loop() {
// put your main code here, to run repeatedly:
if (maxPress >= 120){
Run = 0;
}
if (Run == 1) {
for (int n; n < numRegs; n++) { //cycle through the regulators
if (Switch[n] == false) { //check to see if it's time to start inflating for regulator n, if so...
if (millis() - lastChangeTime[n] >= stepTime) {//time to inflate a step?
lastChangeTime[n] += stepTime; //set next inflation step time
regPress[n] += stepSize; //increase the pressure by stepSize
if (regPress[n] > maxPress) { //if it's above the maxPressure, reset to maxPressure
regPress[n] = maxPress;
Switch[n] = true; //tell the code that on the next cycle, it should start deflating
// delay(100);
}
analogWrite(regPin[n], regPress[n]); //tell the code to inflate by the pressure value enclosed in the array
Serial.println(regPin[n]);
Serial.println(regPress[n]);
// delay(00);
}
}
else { //if for regulator n, switch is true, it is time to deflate
// Serial.println("Loop True works");
if (millis() - lastChangeTime[n] >= stepTime) {//check to see if it's time to go through a deflation step
lastChangeTime[n] += stepTime; //set next deflation step time
regPress[n] -= stepSize; //decrease the pressure by stepSize
if (regPress[n] < minPress) { //if it's below the minPressure, reset to minPressure
regPress[n] = minPress;
Switch[n]=false; //tell the code that on the next cycle, it should start inflating
// delay(10000);
}
analogWrite(regPin[n], regPress[n]); //tell the code to inflate by the pressure value enclosed in the array
// delay(200);
Serial.println(regPin[n]);
Serial.println(regPress[n]);
}
}
}
}
}
//------------------------------------------------------------------
//------------------------------- PWM ------------------------------
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 = 0x07; break;
default: return;
}
TCCR2B = TCCR2B & 0b11111000 | mode;
}
}