Hello,
I am trying to use the following piece of code and it is not behaving the way I need it to.
Open for FULL CODE!! Keeping the post neat.
//BLOCK COMMENT
//https://forum.arduino.cc/t/cyclic-relay2a-2b/889080/10
#define ProjectName "Solenoids"
// CONSTANT DEFINITION
// you need to change these constants to your hardware
const byte Relays[] {3, 7, 8, 4};
const byte Buttons[] {2};
int tt;
// VARIABLE DECLARATION
enum {One, TwoA, TwoB, TwoC};
struct BUTTON
{
byte pin;
int pressedState; // LOW for Active Low
bool wasPressed;
int counter;
unsigned long lastStateChangeTime;
unsigned long debounceInterval;
} button {Buttons[One], 1, 0, 0, 20};
void setup()
{
Serial.begin(9600);
Serial.println(F("."));
Serial.print(F("File : ")), Serial.println(__FILE__);
Serial.print(F("Date : ")), Serial.println(__DATE__);
Serial.print(F("Project: ")), Serial.println(ProjectName);
pinMode (LED_BUILTIN, OUTPUT);
pinMode (A0,INPUT);
for (auto Relay : Relays) pinMode ( Relay, OUTPUT);
for (auto Button : Buttons) pinMode ( Button, INPUT_PULLUP);
}
void loop ()
{
tt = analogRead(A0) /10.23;
Serial.print("Time in Seconds due to Potentiometer :");
Serial.println (tt);
unsigned long currentTime = millis();
digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
bool buttonIsPressed = digitalRead(button.pin) == button.pressedState;
if (buttonIsPressed != button.wasPressed &&
currentTime - button.lastStateChangeTime >= button.debounceInterval)
{
// State Change Detected
button.wasPressed = buttonIsPressed;
button.lastStateChangeTime = currentTime;
{
// Button was just pressed
button.counter = (button.counter + 1) % 6;
static unsigned long TimeOfLastButtonPress = 0;
unsigned long timeBetweenButtonPresses = currentTime - TimeOfLastButtonPress;
TimeOfLastButtonPress = currentTime;
for (auto Relay : Relays)
digitalWrite ( Relay, LOW);
if (timeBetweenButtonPresses > 200) // longer than a second and a half
SlowPressCycle(button.counter);
else if (timeBetweenButtonPresses > 100) // Longer than half a second
MediumPressCycle(button.counter);
else
StandardCycle(button.counter);
}
}
}
// When the Pulse is SLOW (like every 2 Seconds)
// I would like all Relays (TwoA, B & C) same time
void SlowPressCycle(int count)
{
switch (count)
{
case 0:
digitalWrite ( Relays[One] , HIGH);
delay(tt);if (digitalRead(2) == LOW)goto end1;
delay(tt);if (digitalRead(2) == LOW)goto end1;
delay(tt);if (digitalRead(2) == LOW)goto end1;
delay(tt);if (digitalRead(2) == LOW)goto end1;
delay(tt);if (digitalRead(2) == LOW)goto end1;
delay(tt);if (digitalRead(2) == LOW)goto end1;
delay(tt);if (digitalRead(2) == LOW)goto end1;
delay(tt);if (digitalRead(2) == LOW)goto end1;
delay(tt);if (digitalRead(2) == LOW)goto end1;
delay(tt);if (digitalRead(2) == LOW)goto end1;
end1:
digitalWrite ( Relays[One] , LOW);
break;
case 1:
digitalWrite ( Relays[TwoA] , HIGH);
digitalWrite ( Relays[TwoB] , HIGH);
digitalWrite ( Relays[TwoC] , HIGH);
break;
case 2:
digitalWrite ( Relays[One] , HIGH);
delay(tt);if (digitalRead(2) == LOW)goto end2;
delay(tt);if (digitalRead(2) == LOW)goto end2;
delay(tt);if (digitalRead(2) == LOW)goto end2;
delay(tt);if (digitalRead(2) == LOW)goto end2;
delay(tt);if (digitalRead(2) == LOW)goto end2;
delay(tt);if (digitalRead(2) == LOW)goto end2;
delay(tt);if (digitalRead(2) == LOW)goto end2;
delay(tt);if (digitalRead(2) == LOW)goto end2;
delay(tt);if (digitalRead(2) == LOW)goto end2;
delay(tt);if (digitalRead(2) == LOW)goto end2;
end2:
digitalWrite ( Relays[One] , LOW);
break;
case 3:
digitalWrite ( Relays[TwoA] , HIGH);
digitalWrite ( Relays[TwoB] , HIGH);
digitalWrite ( Relays[TwoC] , HIGH);
break;
case 4:
digitalWrite ( Relays[One] , HIGH);
delay(tt);if (digitalRead(2) == LOW)goto end3;
delay(tt);if (digitalRead(2) == LOW)goto end3;
delay(tt);if (digitalRead(2) == LOW)goto end3;
delay(tt);if (digitalRead(2) == LOW)goto end3;
delay(tt);if (digitalRead(2) == LOW)goto end3;
delay(tt);if (digitalRead(2) == LOW)goto end3;
delay(tt);if (digitalRead(2) == LOW)goto end3;
delay(tt);if (digitalRead(2) == LOW)goto end3;
delay(tt);if (digitalRead(2) == LOW)goto end3;
delay(tt);if (digitalRead(2) == LOW)goto end3;
end3:
digitalWrite ( Relays[One] , LOW);
break;
case 5:
digitalWrite ( Relays[TwoA] , HIGH);
digitalWrite ( Relays[TwoB] , HIGH);
digitalWrite ( Relays[TwoC] , HIGH);
break;
}
}
// When the Pulse is faster (every 1 second)
// to have cycle between TwoA,B then TwoB,C
void MediumPressCycle(int count)
{
switch (count)
{
case 0:
digitalWrite ( Relays[One] , HIGH);
delay(tt);if (digitalRead(2), LOW)goto end4;
delay(tt);if (digitalRead(2), LOW)goto end4;
delay(tt);if (digitalRead(2), LOW)goto end4;
delay(tt);if (digitalRead(2), LOW)goto end4;
delay(tt);if (digitalRead(2), LOW)goto end4;
delay(tt);if (digitalRead(2), LOW)goto end4;
delay(tt);if (digitalRead(2), LOW)goto end4;
delay(tt);if (digitalRead(2), LOW)goto end4;
delay(tt);if (digitalRead(2), LOW)goto end4;
delay(tt);if (digitalRead(2), LOW)goto end4;
end4:
digitalWrite ( Relays[One] , LOW);
break;
case 1:
digitalWrite ( Relays[TwoA] , HIGH);
digitalWrite ( Relays[TwoB] , HIGH);
break;
case 2:
digitalWrite ( Relays[One] , HIGH);
delay(tt);if (digitalRead(2), LOW)goto end5;
delay(tt);if (digitalRead(2), LOW)goto end5;
delay(tt);if (digitalRead(2), LOW)goto end5;
delay(tt);if (digitalRead(2), LOW)goto end5;
delay(tt);if (digitalRead(2), LOW)goto end5;
delay(tt);if (digitalRead(2), LOW)goto end5;
delay(tt);if (digitalRead(2), LOW)goto end5;
delay(tt);if (digitalRead(2), LOW)goto end5;
delay(tt);if (digitalRead(2), LOW)goto end5;
delay(tt);if (digitalRead(2), LOW)goto end5;
end5:
digitalWrite ( Relays[One] , LOW);
break;
case 3:
digitalWrite ( Relays[TwoB] , HIGH);
digitalWrite ( Relays[TwoC] , HIGH);
break;
case 4:
digitalWrite ( Relays[One] , HIGH);
delay(tt);if (digitalRead(2), LOW)goto end6;
delay(tt);if (digitalRead(2), LOW)goto end6;
delay(tt);if (digitalRead(2), LOW)goto end6;
delay(tt);if (digitalRead(2), LOW)goto end6;
delay(tt);if (digitalRead(2), LOW)goto end6;
delay(tt);if (digitalRead(2), LOW)goto end6;
delay(tt);if (digitalRead(2), LOW)goto end6;
delay(tt);if (digitalRead(2), LOW)goto end6;
delay(tt);if (digitalRead(2), LOW)goto end6;
delay(tt);if (digitalRead(2), LOW)goto end6;
end6:
digitalWrite ( Relays[One] , LOW);
break;
case 5:
digitalWrite ( Relays[TwoC] , HIGH);
digitalWrite ( Relays[TwoA] , HIGH);
break;
}
}
// When the Pulse is Fastest (faster than 1 second)
// Standard Cycle TwoA, then TwoB, then TwoC.
void StandardCycle(int count)
{
switch (count)
{
case 0: digitalWrite ( Relays[One] , HIGH);
digitalWrite ( Relays[One] , HIGH);
delay(tt);if (digitalRead(2), LOW)goto end7;
delay(tt);if (digitalRead(2), LOW)goto end7;
delay(tt);if (digitalRead(2), LOW)goto end7;
delay(tt);if (digitalRead(2), LOW)goto end7;
delay(tt);if (digitalRead(2), LOW)goto end7;
delay(tt);if (digitalRead(2), LOW)goto end7;
delay(tt);if (digitalRead(2), LOW)goto end7;
delay(tt);if (digitalRead(2), LOW)goto end7;
delay(tt);if (digitalRead(2), LOW)goto end7;
delay(tt);if (digitalRead(2), LOW)goto end7;
end7:
digitalWrite ( Relays[One] , LOW);break;
case 1: digitalWrite ( Relays[TwoA] , HIGH); break;
case 2: digitalWrite ( Relays[One] , HIGH);
digitalWrite ( Relays[One] , HIGH);
delay(tt);if (digitalRead(2), LOW)goto end8;
delay(tt);if (digitalRead(2), LOW)goto end8;
delay(tt);if (digitalRead(2), LOW)goto end8;
delay(tt);if (digitalRead(2), LOW)goto end8;
delay(tt);if (digitalRead(2), LOW)goto end8;
delay(tt);if (digitalRead(2), LOW)goto end8;
delay(tt);if (digitalRead(2), LOW)goto end8;
delay(tt);if (digitalRead(2), LOW)goto end8;
delay(tt);if (digitalRead(2), LOW)goto end8;
delay(tt);if (digitalRead(2), LOW)goto end8;
end8:
digitalWrite ( Relays[One] , LOW);break;
case 3: digitalWrite ( Relays[TwoB] , HIGH); break;
case 4: digitalWrite ( Relays[One] , HIGH);
digitalWrite ( Relays[One] , HIGH);
delay(tt);if (digitalRead(2), LOW)goto end9;
delay(tt);if (digitalRead(2), LOW)goto end9;
delay(tt);if (digitalRead(2), LOW)goto end9;
delay(tt);if (digitalRead(2), LOW)goto end9;
delay(tt);if (digitalRead(2), LOW)goto end9;
delay(tt);if (digitalRead(2), LOW)goto end9;
delay(tt);if (digitalRead(2), LOW)goto end9;
delay(tt);if (digitalRead(2), LOW)goto end9;
delay(tt);if (digitalRead(2), LOW)goto end9;
delay(tt);if (digitalRead(2), LOW)goto end9;
end9:
digitalWrite ( Relays[One] , LOW);break;
case 5: digitalWrite ( Relays[TwoC] , HIGH); break;
}
}
Basically, My Arduino when Input goes high it's supposed to turn on Relay 1, then when it goes LOW and turns on Relays 2a/b/c. bit of complex stuff for speed control etc.
I have added a potentiometer to limit how long I want Relay 1 to stay on for (0-5s).
I had an issue and reduced the time to 1s.
I had a problem where the potentiometer would keep relay 1 too long, past when the input is supposed to turn it off.
Then I thought to create a bunch of small delays which detect inputsignal then adds more delay if the input is still HIGH
The problem is the Relay1 is only on for what feels like 100ms, i have stuffed up the following code and would like some help correcting it
Basically the way I understand the following snippet of code is
Start by turning on Relay 1.
Add a delay = what the potentiometer is.. (100ms) MAX
now if inputsignal (pin2) has gone LOW move onto END1.
If the input is still high it should add another 100ms of delay
and so on.
What do I need to do to ensure that it will stay on for multiple passes of those if statements?
case 0:
digitalWrite ( Relays[One] , HIGH);
delay(tt);if (digitalRead(2) == LOW)goto end1;
delay(tt);if (digitalRead(2) == LOW)goto end1;
delay(tt);if (digitalRead(2) == LOW)goto end1;
delay(tt);if (digitalRead(2) == LOW)goto end1;
delay(tt);if (digitalRead(2) == LOW)goto end1;
delay(tt);if (digitalRead(2) == LOW)goto end1;
delay(tt);if (digitalRead(2) == LOW)goto end1;
delay(tt);if (digitalRead(2) == LOW)goto end1;
delay(tt);if (digitalRead(2) == LOW)goto end1;
delay(tt);if (digitalRead(2) == LOW)goto end1;
end1:
digitalWrite ( Relays[One] , LOW);
break;
case 1:
digitalWrite ( Relays[TwoA] , HIGH);
digitalWrite ( Relays[TwoB] , HIGH);
digitalWrite ( Relays[TwoC] , HIGH);
break;```