yasguj
February 7, 2023, 3:50pm
1
I have the following loop where I am setting a pin to LOW followed by HIGH and repeating it for 10 more pins. There is a time delay between each state change. Finally, I have further more loops, but the next few loops don't follow the pins in order. Meaning, I am setting pin 3 to LOW, Delay, then HIGH, next can be pin 8 then pin 12 and so on.
void loop() {
//Lock-Unlock Code below
digitalWrite(2,LOW);
delay(delayTime);
digitalWrite(2,HIGH);
delay(delayTime);
digitalWrite(3,LOW);
delay(delayTime);
digitalWrite(3,HIGH);
delay(delayTime);
digitalWrite(4,LOW);
delay(delayTime);
digitalWrite(4,HIGH);
delay(delayTime);
digitalWrite(5,LOW);
delay(delayTime);
digitalWrite(5,HIGH);
delay(delayTime);
digitalWrite(A0,LOW);
delay(delayTime);
digitalWrite(A0,HIGH);
delay(3000);
digitalWrite(A1,LOW);
delay(delayTime);
digitalWrite(A1,HIGH);
delay(3000);
}
Thank you!
Welcome to the forum
A for loop with the LED pins in an array seems like the obvious answer
See what you make of this
const byte ledPins[] = {3, 5, 6, 9};
const byte LED_COUNT = sizeof(ledPins) / sizeof(ledPins[0]);
void setup()
{
Serial.begin(115200);
for (int p = 0; p < LED_COUNT; p++)
{
pinMode(ledPins[p], OUTPUT);
}
}
void loop()
{
for (int p = 0; p < LED_COUNT; p++)
{
digitalWrite(ledPins[p], !digitalRead(ledPins[p]));
delay(1000);
}
}
guix
February 7, 2023, 4:29pm
4
Welcome
Try something like that t1087332 - Wokwi Arduino and ESP32 Simulator
Also look at Blink Without Delay example if needed
yasguj
February 7, 2023, 4:49pm
5
Thanks for the ideas and guidance, everyone. I was able to figure out a method to shorten my code. I assigned void to each output and called them as needed in my loop.
void Press1(){ digitalWrite(2,LOW); delay(delayTime); digitalWrite(2,HIGH);delay(delayTime); } //1
void Press2(){ digitalWrite(3,LOW); delay(delayTime); digitalWrite(3,HIGH);delay(delayTime); } //2
void Press3(){ digitalWrite(4,LOW); delay(delayTime); digitalWrite(4,HIGH);delay(delayTime); } //3
void Press4(){ digitalWrite(5,LOW); delay(delayTime); digitalWrite(5,HIGH);delay(delayTime); } //4
void Press5(){ digitalWrite(6,LOW); delay(delayTime); digitalWrite(6,HIGH);delay(delayTime); } //5
void Press6(){ digitalWrite(7,LOW); delay(delayTime); digitalWrite(7,HIGH);delay(delayTime); } //6
void loop() {
Press1();
Press2();
Press3();
Press4();
Press5();
}
Thanks again to the community!
If you are using symbols that differ only because an appended digit is changed, it's a sign that you shoukd be learning about array variables .
Array variables are well-suited for use in for and while loops, which should also be on your very short list of things to look into. Soon.
And functions with arguments, viz:
void Press(int N)
{
digitalWrite(N + 1, LOW);
delay(delayTime);
digitalWrite(N + 1, HIGH);
delay(delayTime);
}
a7
yasguj
February 7, 2023, 6:34pm
7
I have the following code and it shows too many arguments to function. Is there a way I can make this work with a single line rather than 4 separate lines?
const int relayPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, A0, A1};
int relayStates[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
void Press(int N)
{
digitalWrite(N + 1, LOW);
delay(delayTime);
digitalWrite(N + 1, HIGH);
delay(delayTime);
}
void setup() {
for (int i = 0; i < 14; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], relayStates[i]);
}
}
void loop() {
Press(1,2,3,4);
}
LarryD
February 7, 2023, 6:39pm
9
yasguj:
Press(1,2,3,4);
You are passing 3 parameters where your function is looking for only 1.
void Press(int N)
yasguj
February 7, 2023, 6:40pm
10
My loop example is very basic. The end goal is to enable the outputs at user-defined order.
Example:
Press(6,2,5,1,14);
Press(7,2,6,8,1);
I can do the above with the following:
Press(6);
Press(2);
Press(5);
Press(1);
Press(14);
But then my code keeps getting longer as I automate my task further and further.
Array, for loop. Suggest you dig deeper!
LarryD
February 7, 2023, 6:58pm
12
yasguj:
Press(6,2,5,1,14);
Press(7,2,6,8,1);
Make an array of your pins numbers in the order needed.
Using a for( ) loop, iterate thru this array at the speed you want handling things as you go.
Try this:
#include <Arduino.h>
#define NUM_RELAYS 13
const int relayPins[NUM_RELAYS] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, A0, A1};
int relayStates[NUM_RELAYS];
int delayTime = 1000;
bool currentState = HIGH;
void press(int pin, bool state)
{
digitalWrite(relayPins[pin], state);
relayStates[pin] = state;
delay(delayTime);
}
void setup()
{
for (int i = 0; i < NUM_RELAYS; i++)
{
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], currentState);
relayStates[i] = currentState;
}
}
void loop()
{
for (int i = 0; i < NUM_RELAYS; i++)
{
press(i, currentState);
}
currentState = !currentState;
}
could make press take an array and count..
const int relayPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, A0, A1};
int relayStates[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
int delayTime = 100;
void Press(int N[], int NumPins)
{
Serial.println("pressing pins");
for (int i=0;i<NumPins;i++)
{
Serial.print(N[i]);
digitalWrite(N[i] + 1, LOW);
delay(delayTime);
digitalWrite(N[i] + 1, HIGH);
delay(delayTime);
}
}
void setup() {
Serial.begin(115200);
for (int i = 0; i < 14; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], relayStates[i]);
}
int pins[4] ={4,3,2,1};
Press(pins, 4);
}
void loop() {
}
can test here...
1 Like
In my example the loop could be like this to change the status of relayPins[0] 4 times in sequence.
void loop()
{
int numberOfPress = 4;
int currentPin = 0; // Goes from 0 to NUM_RELAYS - 1
for (int i = 0; i < numberOfPress; i++)
{
press(currentPin, currentState);
currentState = !currentState;
}
}
gcjr
February 7, 2023, 7:31pm
16
what are you trying to do?
alto777
February 7, 2023, 8:53pm
17
Like I said, for loop
void loop() {
for (byte theN = 1; theN <= 4; theN++)
Press(theN);
}
theN is just the index variable, call it anything you want.
HTH
a7
1 Like
pert
February 7, 2023, 9:10pm
18
I have merged your posts due to them having too much overlap on the same subject matter @yasguj .
In the future, please only create one topic for each distinct subject matter and be careful not to cause them to converge into parallel discussions.
The reason is that generating multiple threads on the same subject matter can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.
Thanks in advance for your cooperation.
1 Like
system
Closed
August 6, 2023, 9:10pm
19
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.