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?
//////////////////////////////////////////////// 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;
}
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"
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
"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
}
Thank you 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);
}
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
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.