Glass Blowing Machine Help

Hi all,

First time poster, hopefully I comprehended the posting instructions correctly.

I am using an Arduino MEGA 2560 R3 for a semi automated glass blowing machine.

It should be fairly simple; the bulk of what I need is to trigger relays on an off at various times, but not in consistent intervals. There is one sensor and a hope to add buttons that can adjust the times, but I'm not even close to getting to that part of code or what it would look like. I've attached a simple diagram of what needs to be turned on and turned off and when this occurs.

I am using an 8 relay module as all solenoids use 120v.

This program is a shot in the dark, but its what I've been able to cobble together over the past few days from tutorials and youtube videos. I can get the individual relays to turn on at different times(!), but I'm stumped how to get the to turn off at different times and how to structure it so it can accomplish what I need.

I'm sure there are some gapping holes in my explanation, but this is the best I can provide right now. Please ask questions! Let me know if this is enough to work with or if my explanation and diagram needs to be more thorough.

Thanks in advance!

const unsigned long event_1 = 2000;
const unsigned long event_2 = 4000;
const unsigned long event_3 = 6000;

const int relay1 = 2;
const int relay2 = 3;
const int relay3 = 4;

unsigned long previousTime_1;

void setup() {


  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);


void loop() {

  unsigned long currentTime = millis();

  if (millis() < event_1 ) {
    digitalWrite(relay1, HIGH);
  else {
    digitalWrite(relay1, LOW);

  if (millis() < event_2 ) {
    digitalWrite(relay2, HIGH);
  else {
    digitalWrite(relay2, LOW);

  if (millis() < event_3 ) {
    digitalWrite(relay3, HIGH);
  else {
    digitalWrite(relay3, LOW);



Can you provide actual times on that "state diagram"?

For example:

  • how long is INITIAL_HEAT?
  • how long is SECOND_HEAT?
  • how long is FINAL_HEAT?
  • how long is AIR_CYLINDER1 and 5V_DC_MOTOR?
  • how long is AIR_CYLINDER2?
  • how long is AIR_CYLINDER3?
  • what is the time between the end of AIR_CYLINDER1/5V_DC_MOTOR until the start of AIR_CYLINDER3?

Your code attempt shows 3 relays and 3 "event" time values but I can't link those to your drawing.


Demonstration code for several things at the same time

My tutorial on Multi-tasking in Arduino covers doing multiple things 'at the same' time.
For multiple timers and delays see my tutorial on Writing Timers and Delays in Arduino.
It has lots of examples of using timers, and the millisDelay timer class allows for easy starting and stopping timers.

Sounds like a good case for a state machine.

A very good case for using an ESP32 and freeRTOS.

if (millis() < event_1 ) {
    digitalWrite(relay1, HIGH);
  else {
    digitalWrite(relay1, LOW);

This is not a correctly formed millis timer. millis is a moving target and event_1 is fixed. It needs to be:
if(millis() - referencePointOne < event_1)…

This state machine is totally timer driven and, I believe, could serve your needs with some modifications.

My tutorial on Multi-tasking in Arduino has an ESP32 example that avoids the complexities of RTOS, unless of course you are keen to learning RTOS.

Very grateful for all the replies! I am humbled by the efficiency and usefulness of this forum!

I have a lot to digest here from the tutorials to state machines to RTOS. It'll take me a little while to figure out which method will be the best for my situation and lack of coding experience. Will post more questions when they come up!


This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.