// put your setup code here, to run once:
const int joystickInput = A0;
const int depthProxAPin = 12;
const int depthProxBPin = 11;
const int adrSwitch = 5;
const int adrControl = A1;
const int rotarySwitch = 4;
const int rotaryControl = A2;
const int rotaryProxPin = 10;
const int pump1Switch = 3;
const int pump1Control = A3;
const int pump1ProxPin = 9;
const int pump2Switch = 2;
const int pump2Control = A4;
const int pump2ProxPin = 8;
int adrButtonState = LOW, adrPrevButtonState;
int rotaryButtonState = LOW, rotaryPrevButtonState;
int pump1ButtonState = LOW, pump1PrevButtonState;
int pump2ButtonState = LOW, pump2PrevButtonState;
enum { NEUTRAL, UP, DOWN };
enum { ON, OFF};
int dir = NEUTRAL;
int adrSw = OFF;
int rotarySw = OFF;
int pump1Sw = OFF;
int pump2Sw = OFF;
int blockState;
int rotaryState;
int pump1State;
int pump2State;
unsigned long currentDelay;
unsigned long adrSpeedDelay;
unsigned long rotarySpeedDelay;
unsigned long pump1SpeedDelay;
unsigned long pump2SpeedDelay;
unsigned long blockStartTime;
unsigned long rotaryStartTime;
unsigned long pump1StartTime;
unsigned long pump2StartTime;
void setup() {
// designate depthProxAPin and depthProxBPin as an OUTPUT pin for the LED
pinMode (depthProxAPin, OUTPUT);
pinMode (depthProxBPin, OUTPUT);
pinMode (rotaryProxPin, OUTPUT);
pinMode (pump1ProxPin, OUTPUT);
pinMode (pump2ProxPin, OUTPUT);
// designate joystickInput as an Analog INPUT for the potentiometer
pinMode (joystickInput, INPUT);
Serial.begin(9600);
adrPrevButtonState = digitalRead(adrSwitch);
rotaryPrevButtonState = digitalRead(rotarySwitch);
pump1PrevButtonState = digitalRead(pump1Switch);
pump2PrevButtonState = digitalRead(pump2Switch);
}
void loop()
{
adrLoop();
rotaryLoop();
pump1Loop();
pump2Loop();
}
void blocks() {
unsigned long currentTime = millis();
int potValue = analogRead(joystickInput);
int outputValue = map(potValue, 64, 912, 100, -100); //map full up to 100 and full down to -100
Serial.println(outputValue);
if (outputValue > 10) {
//do this when joystick is pressed from the neutral position up
currentDelay = map(outputValue, 10, 100, 500, 1); //map the delay progressivly less as joystick is moved from neutral to full up
if (dir != UP) {
// first time joystick went up from neutral or from down
dir = UP;
blockState = 0;
blockStartTime = currentTime;
digitalWrite(depthProxAPin, HIGH);
}
else {
// joystick has been up so see if time to move to next state
if ( currentTime - blockStartTime >= currentDelay ) {
blockState++;
switch (blockState) {
case 1:
digitalWrite(depthProxBPin, HIGH);
break;
case 2:
digitalWrite(depthProxAPin, LOW);
break;
case 3:
digitalWrite(depthProxBPin, LOW);
break;
case 4:
digitalWrite(depthProxAPin, HIGH );
blockState = 0; // last state so roll over to zero
break;
}
blockStartTime = currentTime;
}
}
}
else if (outputValue < -10) {
//do this when joystick is pressed from the neutral position down
currentDelay = map(outputValue, -10, -100, 500, 1); //map the delay progressivly less as joystick is moved from neutral to full down
if (dir != DOWN) {
// first time joystick went down from neutral or from up
dir = DOWN;
blockState = 0;
blockStartTime = currentTime;
digitalWrite(depthProxBPin, HIGH);
}
else {
// joystick has been up so see if time to move to next state
if ( currentTime - blockStartTime >= currentDelay ) {
blockState++;
switch (blockState) {
case 1:
digitalWrite(depthProxAPin, HIGH);
break;
case 2:
digitalWrite(depthProxBPin, LOW);
break;
case 3:
digitalWrite(depthProxAPin, LOW);
break;
case 4:
digitalWrite(depthProxBPin, HIGH );
blockState = 0; // last state so roll over to zero
break;
}
blockStartTime = currentTime;
}
}
}
else {
//do this when joystick is in neutral position
dir = NEUTRAL;
digitalWrite (depthProxBPin, LOW);
digitalWrite (depthProxAPin, LOW);
}
}
void adr()
{
int adrSpeed = analogRead(adrControl);
unsigned long adrTime = millis();
adrSpeedDelay = map(adrSpeed, 124, 931, 500, 40); //map the delay progressivly less as joystick is moved from neutral to full up
if ( adrTime - blockStartTime >= adrSpeedDelay )
{
blockState++;
switch (blockState)
{
case 1:
digitalWrite(depthProxAPin, HIGH);
break;
case 2:
digitalWrite(depthProxBPin, LOW);
break;
case 3:
digitalWrite(depthProxAPin, LOW);
break;
case 4:
digitalWrite(depthProxBPin, HIGH );
blockState = 0; // last state so roll over to zero
break;
}
blockStartTime = adrTime;
}
}