Clubbing 2 or more programs

Hello,

I am trying to club 2 programs in a single program as shown in below. My requirement is the first program should run as it is but the second program should run based on the return value of the function that is in the loop method. Can I use conditions in the void loop method?

@LarryD Please share your thoughts on this.

//////////////////////////////////////////////// Program-1/////////////////////////////////

// Simple Blink Program for Arduino Nano

// Define the pin for the LED
const int ledPin1 = 13;

void setup() {
  // Set the LED pin as an output
  pinMode(ledPin1, OUTPUT);
}

void loop() {
  // Turn the LED on (HIGH)
  digitalWrite(ledPin1, HIGH);
  delay(1000); // Wait for 1 second

  // Turn the LED off (LOW)
  digitalWrite(ledPin1, LOW);
  delay(1000); // Wait for 1 second
}


/////////////////////////////////////// Program-2 /////////////////////////////////

// Simple Blink Program for Arduino Nano

// Define the pin for the LED
const int ledPin2 = 6;

void setup() {
  // Set the LED pin as an output
  pinMode(ledPin2, OUTPUT);
}

void loop() {
  // Turn the LED on (HIGH)
  digitalWrite(ledPin2, HIGH);
  delay(1000); // Wait for 1 second

  // Turn the LED off (LOW)
  digitalWrite(ledPin2, LOW);
  delay(1000); // Wait for 1 second
}



///////////////////////////////////////////////// Combined Program ///////////////////////////////

// Simple Blink Program for Arduino Nano

// Define the pin for the LED
const int ledPin1 = 13;

// Define the pin for the LED
const int ledPin2 = 6;

void setup() {
  // Set the LED pin as an output
  pinMode(ledPin1, OUTPUT);

  // Set the LED pin as an output
  pinMode(ledPin2, OUTPUT);
}

void loop() {
  // Turn the LED on (HIGH)
  digitalWrite(ledPin1, HIGH);
  delay(1000); // Wait for 1 second

  // Turn the LED off (LOW)
  digitalWrite(ledPin1, LOW);
  delay(1000); // Wait for 1 second

 if (function() == true)
 {
  // Turn the LED on (HIGH)
  digitalWrite(ledPin2, HIGH);
  delay(1000); // Wait for 1 second

  // Turn the LED off (LOW)
  digitalWrite(ledPin2, LOW);
  delay(1000); // Wait for 1 second
}

}

bool function()
{ 
  //code here
  return true/false;
}

yes, you can

Is your combine code compiled?

Yes compiled.

And what is the problem?

Sure you can.
Any programming language is like Lego
A lot of different shaped bricks and pieces that can be put together in billions of different ways to create billions of different "functionalities"

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

I am sure if you have worked through this course you will proceed faster

best regards Stefan

3 Likes

:slight_smile: Great response! The Arduino Cookbook would also be a good read for the OP.

1 Like

Thank you so much for your great response. Will go through the course. :slightly_smiling_face::slightly_smiling_face:

1 Like

"Compile" with computer programming means "to process" an not just the English "to gather together."

Sketch 1 compiled and worked.

// sketch 1

// Simple Blink Program for Arduino Nano

// Define the pin for the LED
const int ledPin1 = 13;

void setup() {
  // Set the LED pin as an output
  pinMode(ledPin1, OUTPUT);
}

void loop() {
  // Turn the LED on (HIGH)
  digitalWrite(ledPin1, HIGH);
  delay(1000); // Wait for 1 second

  // Turn the LED off (LOW)
  digitalWrite(ledPin1, LOW);
  delay(1000); // Wait for 1 second
}

Sketch 2 compiled and worked.

// sketch 2

// Simple Blink Program for Arduino Nano

// Define the pin for the LED
const int ledPin2 = 6;

void setup() {
  // Set the LED pin as an output
  pinMode(ledPin2, OUTPUT);
}

void loop() {
  // Turn the LED on (HIGH)
  digitalWrite(ledPin2, HIGH);
  delay(1000); // Wait for 1 second

  // Turn the LED off (LOW)
  digitalWrite(ledPin2, LOW);
  delay(1000); // Wait for 1 second
}

Sketch 3 "compiled" (it processed) but did not work. LED 13 turned ON then OFF then the sketch got stuck in loop() without passing function() because it returned nothing ( true / false ). I think you wanted the LEDs to alternate blinking, so changing function() to return ( true / true ), makes the LEDs alternate...

// sketch 3
// Simple Blink Program for Arduino Nano

// Define the pin for the LED
const int ledPin1 = 13;
// Define the pin for the LED
const int ledPin2 = 6;

void setup() {
  // Set the LED pin as an output
  pinMode(ledPin1, OUTPUT);
  // Set the LED pin as an output
  pinMode(ledPin2, OUTPUT);
}

void loop() {
  // Turn the LED on (HIGH)
  digitalWrite(ledPin1, HIGH);
  delay(1000); // Wait for 1 second
  // Turn the LED off (LOW)
  digitalWrite(ledPin1, LOW);
  delay(1000); // Wait for 1 second
  if (function() == true)
  {
    // Turn the LED on (HIGH)
    digitalWrite(ledPin2, HIGH);
    delay(1000); // Wait for 1 second
    // Turn the LED off (LOW)
    digitalWrite(ledPin2, LOW);
    delay(1000); // Wait for 1 second
  }
}
bool function() {
  //code here
  // return true / false; // <-- Changed this...
  return true / true; // <-- to ... this
}

blinky

Excellent... BTW is there any application which we can simulate our code with arduino before the actual hardware settings?

I like your simulator tool.

@chandu-mca06

Thank you :slight_smile: this tool is working great. I have tried the following program based on my experiment but somehow the results are not accurate and unable to find it out the issue.

requirement:

simply if all +5v,3,4 pins are connected together (or) if all pins +5v,5,6 are disconnected with each other then the relay should OFF
like wise, If pins +5v,3,4 are disconnected with each other and, either pins +5v,5,6 are connected together or just +5v,5 are connected then the relay should ON

the following program is working somehow but not getting the actual state of the pins dynamically. I am not able to figure it out the issue.

#define TOP_ULIMIT 4
#define TOP_LLIMIT 3
#define BTM_ULIMIT 6
#define BTM_LLIMIT 5
#define RELAY_PIN 7

void setup() {
  pinMode(TOP_ULIMIT, INPUT);
  pinMode(TOP_LLIMIT, INPUT);
  pinMode(BTM_ULIMIT, INPUT);
  pinMode(BTM_LLIMIT, INPUT);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW); // Initialize relay to off state
  Serial.begin(9600);
}

void loop() {
  int topULimit = digitalRead(TOP_ULIMIT);
  int topLLimit = digitalRead(TOP_LLIMIT);
  int btmULimit = digitalRead(BTM_ULIMIT);
  int btmLLimit = digitalRead(BTM_LLIMIT);

  Serial.print("Top Upper Limit: ");
  Serial.print(topULimit);
  Serial.print(", TOP Lower Limit: ");
  Serial.print(topLLimit);
  Serial.print(", BTM Upper Limit: ");
  Serial.print(btmULimit);
  Serial.print(", BTM Lower Limit: ");
  Serial.println(btmLLimit);

  // Auto OFF Section
  if ((topULimit == HIGH && topLLimit == HIGH) || (btmULimit == LOW && btmLLimit == LOW)) {
    digitalWrite(RELAY_PIN, LOW);
    Serial.println("Relay OFF");
  }

  // Auto ON Section
  if (topULimit == LOW && topLLimit == LOW && ((btmULimit == HIGH && btmLLimit == HIGH) || (btmULimit == HIGH && btmLLimit == LOW))) {
    digitalWrite(RELAY_PIN, HIGH);
    Serial.println("Relay ON");
  }

  delay(1000); 
}

attached is the wiring connections diagram:

Without showing a picture of how you connected your buttons it is impossible to understand how you wired the buttons.

If you have created a WOKWI-Simulation simply post the link of this simulation.

I will not speculate much about your wiring just a little bit.
You might have connected two IO-pins to each other but I don't know. This is the reason why you shall post a picture or a WOKWI-link

You are using delay(1000)

A better name for delay is

stop_code_execution_for_1_second_DONT_react_on_anything_until_blocking_time_is_over()

So this might be what you mean when you wrote

these two videos explain non-blocking timing based on function millis()

explain non-blocking timing part 1

explain non-blocking timing part 2

best regards Stefan

1 Like

Sorry my bad. Attached is the wiring connections

Perhaps you believe that if a pin is not connected to 5V, it will be LOW? This is not true. A pin that is not connected to either 5V or ground is "floating" and could read HIGH or LOW randomly. You need to connect pull-down resistors (10K) from each pin to ground to prevent them from floating.

To avoid the need for resistors, you can use INPUT_PULLUP mode. Then pins that are connected to ground will read LOW and pins that are disconnected will read HIGH.

1 Like

Post the link to your WOKWI-simulation

Here is a modified version of your simulation.

The switches are connected for use with INPUT_PULLUP-mode of the IO-pins.

I have increased the delaytime to make it easier to read the serial printing
The non-dynamically behaviour of your code is caused by

stop_code_execution_for_5_second_DONT_react_on_anything_until_blocking_time_is_over()
ehm sorry I mean
delay(5000);

best regards Stefan

Now the reading are fine but the conditions are not working as expected.

To which code are you referring to?
You will have to describe two things in detail

what is the code-behaviour that you observe?
what is the code-behaviour that you want to have?

1 Like

The same project which you have shared. If I change the switches then the relay is not reaponding properly.