Need a code to keep a processing going for set a amount of time

I have this project that I'm doing where I make a system in which I have to connect a power supply and sonicator to a relay so that we can switch between the use of them every 1 second of 90 seconds. The current being sent to the power supply and sonicator will be AC.
We will use a button switch on the arduino to start the process.

So I need a code that turns the volts sent to power supply and sonicator on 1 second then off one second for a duration of 90 seconds, not sync (alternating between power supply and sonicator). The Program will start when a button is pressed and it needs to be able to automatically stop after 90 seconds.

Currently, I have been working on a code for this process but I still need code for:

CRITIQUE MY CODE please let me know if there is anything I could add to it

Preliminary CODE:

//Button is connected to Digital Pin 2
const int switchPin = 2;
//define names for the Digital Pins 6 & 7
//Relay 1 is connected to power supply
//Relay 2 is connected to sonicator
const int RELAY1 = 6; 
const int RELAY2 = 7; 
//Declare Button’s current state is OFF
boolean lastButton = LOW;
boolean currentButton  = LOW;
boolean RELAY1On = false;
boolean RELAY2On = false;
 
void setup()
{
  //Initialize the Arduino data Pins 6 & 7 for OUTPUTs
  pinMode(RELAY1, OUTPUT);       
  pinMode(RELAY2, OUTPUT);
  //Button on Digital Pin 2 is a INPUT
  pinMode(2, INPUT);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if(last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}
void loop()
{
  static unsigned long startTime = 0;
  if (digitalRead(switchPin) == LOW)  // or high
  {
    // set start time of button press if not set already
    if (startTime == 0)
    {
      startTime = millis();
    }
  }
  else
  {
    // if button was previously pressed (indicated by startTime not being zero) and now released
    if (startTime != 0)
    {
      // print the duration that the button was pressed
      Serial.print("duration = 90000"); Serial.println(millis() - startTime);

      // reset start time
      startTime = 0;
    }
  }
//if the Button is pressed the Process will Start
 currentButton = debounce(lastButton);
 if (lastButton == LOW && currentButton == HIGH)
 {
  RELAY1On = !RELAY1On;
  RELAY2On = !RELAY2On;
 }
 lastButton = currentButton;
   //Turn On Relay1
  digitalWrite(RELAY1,LOW);           
  //Wait 1 second
  delay(1000);
  //Turn Off Relay
  digitalWrite(RELAY1,HIGH);
  //Turn On Relay2
  digitalWrite(RELAY2,LOW);           
  //Wait 1 second
  delay(1000);
  //Turn Off Relay
  digitalWrite(RELAY2,HIGH);
}

rojackson13:
CRITIQUE MY CODE for any mistakes please and let me know if there is anything I could add to it

First of all tell us if it does what you want.

If it does not then tell us what it actually does and what you want it to do that is different.

And please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

…R

Do you have a 10k pulldown resistor connected from pin 2 to ground? If not the pin will be "floating" (no defined state) when the button is not pressed.

Int switchState = 0;

Why use a 16 bit "int" to store a 1 bit state (ON / OFF)?
Why not:

bool switchState  = false;

edgemoron:
Do you have a 10k pulldown resistor connected from pin 2 to ground? If not the pin will be "floating" (no defined state) when the button is not pressed.
Why use a 16 bit "int" to store a 1 bit state (ON / OFF)?
Why not:

bool switchState  = false;

Well right now I'm a beginner at coding so using 'int' because it was in the Arduino Uno book.
But could you what you explain what your suggestion does differently

It uses half as much memory.

CRITIQUE MY CODE for any mistakes please and let me know if there is anything I could add to it

This code doesn't even compile. I mean - why ask for a critique from humans when your compiler will give you an initial critique all of its own? You dont - at this stage - need to add stuff to it. You need to fix it so that it compiles.

sketch_jul07a:7: error: 'Int' does not name a type

Int switchState  = 0;
^
/Users/pmurray/Documents/Arduino/sketch_jul07a/sketch_jul07a.ino: In function 'void loop()':
sketch_jul07a:27: error: 'switchState' was not declared in this scope
switchState = digitalRead(2);
^
sketch_jul07a:30: error: expected primary-expression before 'switch'
if (switch state == HIGH);
    ^
sketch_jul07a:30: error: expected ')' before 'switch'
/Users/pmurray/Documents/Arduino/sketch_jul07a/sketch_jul07a.ino:30:26: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
if (switch state == HIGH);
                          ^
exit status 1
'Int' does not name a type

'Int' is not a type. 'int' is.
'switch state' is not an identifier. 'switch_state' is.
Your if has an empty statement after it - a lone semicolon. It's not doing what you think its doing.

Why have a const int switchPin = 2; if you don't use it in pinMode or digitalRead?

Below a simple example how to measure duration; not tested.

void loop()
{
  static unsigned long startTime = 0;

  if (digitalRead(switchPin) == LOW)  // or high
  {
    // set start time of button press if not set already
    if (startTime == 0)
    {
      startTime = millis();
    }
  }
  else
  {
    // if button was previously pressed (indicated by startTime not being zero) and now released
    if (startTime != 0)
    {
      // print the duration that the button was pressed
      Serial.print("duration = "); Serial.println(millis() - startTime);

      // reset start time
      startTime = 0;
    }

  }

  // simple debouce
  delay(50);

}

Here's my new updated Code

//Button is connected to Digital Pin 2
const int switchPin = 2;
//define names for the Digital Pins 6 & 7
//Relay 1 is connected to power supply
//Relay 2 is connected to sonicator
const int RELAY1 = 6; 
const int RELAY2 = 7; 
//Declare Button's current state is OFF
boolean lastButton = LOW;
boolean currentButton  = LOW;
boolean RELAY1On = false;
boolean RELAY2On = false;
 
void setup()
{
  //Initialize the Arduino data Pins 6 & 7 for OUTPUTs
  pinMode(RELAY1, OUTPUT);       
  pinMode(RELAY2, OUTPUT);
  //Button on Digital Pin 2 is a INPUT
  pinMode(2, INPUT);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if(last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}
void loop()
{
  static unsigned long startTime = 0;
  if (digitalRead(switchPin) == LOW)  // or high
  {
    // set start time of button press if not set already
    if (startTime == 0)
    {
      startTime = millis();
    }
  }
  else
  {
    // if button was previously pressed (indicated by startTime not being zero) and now released
    if (startTime != 0)
    {
      // print the duration that the button was pressed
      Serial.print("duration = 90000"); Serial.println(millis() - startTime);

      // reset start time
      startTime = 0;
    }
  }
//if the Button is pressed the Process will Start
 currentButton = debounce(lastButton);
 if (lastButton == LOW && currentButton == HIGH)
 {
  RELAY1On = !RELAY1On;
  RELAY2On = !RELAY2On;
 }
 lastButton = currentButton;
   //Turn On Relay1
  digitalWrite(RELAY1,LOW);           
  //Wait 1 second
  delay(1000);
  //Turn Off Relay
  digitalWrite(RELAY1,HIGH);
  //Turn On Relay2
  digitalWrite(RELAY2,LOW);           
  //Wait 1 second
  delay(1000);
  //Turn Off Relay
  digitalWrite(RELAY2,HIGH);
}

You are reading the switchPin in different places in the program. That will give strange results if the pin state changes in between those readings. Try to only read the inputs once at the top of the loop and then you have a consistent set of variables to make calculations with.

Your code seems to do two totally different things: it counts how long the button is pressed down and it cycles the two relays on and off for one second each when the button is pressed. Due to the use of delays in the second part, the first part can never report a button press shorter than 2 seconds.

Is this what you want it to do? What do you want it to do differently?

rojackson13:
Here's my new updated Code

//Button is connected to Digital Pin 2

const int switchPin = 2;

...
...

void setup()
{
  ...
  ...

pinMode(2, INPUT);
}

You still have a hardcoded pin number in setup :wink: See, the trick is that if you ever need to change to another pin, you only have to change the code in one place; currently you have to change it in two places.

You also don't seem to really use the snippet that I provided as it was intended; already indicated by @MorganS. The code that I provided shows how you can do thing for a given duration

E.g.
in the if block (where the startTime is set to millis()), you can switch the relay on.
in the else block (where the startTime is set to 0), you can switch the relay off.