New to coding and need some help. Can someone write a quick scrip for me that will turn on 4 relays, in sequence with about a 1 second delay between as soon as the unit is powered up? I would appreciate any help. Thanx!
One of the exciting things about Arduino is that you can do it yourself. If you do NOT want to do it yourself, post your specifications at "Gigs and Collaborations" and bring your wallet.
I am also not convinced that this is not a homework assignment.
fire41fighter:
New to coding ...
... but been working on this problem for over 2 years?
Check the line marked <<<<<<
// https://forum.arduino.cc/index.php?topic=664010
// 11 feb 2020
//turn on 4 relays, in sequence with about a 1 second delay
//between as soon as the unit is powered up
int about1second = 1001;
//pins from https://forum.arduino.cc/index.php?topic=513098
#define RELAY1 6
#define RELAY2 7
#define RELAY3 8
#define RELAY4 9
#define RELAY5 10 //not used, current thread asks for 4
//*************************
//to use RELAY_ON or RELAY_OFF in digitalWrite(), instead of HIGH or LOW
//(un-)comment the next line depending on the relay's logic <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//#define weHaveAnActiveLowRelay
#ifdef weHaveAnActiveLowRelay
#define RELAY_ON LOW // most relay modules use this *active low* logic
#define RELAY_OFF HIGH
#else
#define RELAY_ON HIGH // for testing with an *active high* relay or led
#define RELAY_OFF LOW
#endif
//************************
void setup()
{
Serial.begin(9600);
Serial.println("setup() starting...");
Serial.print("... turning relays on ...");
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
digitalWrite(RELAY1, RELAY_ON);
Serial.print(" 1");
delay(about1second);
digitalWrite(RELAY2, RELAY_ON);
Serial.print("2");
delay(about1second);
digitalWrite(RELAY3, RELAY_ON);
Serial.print("3");
delay(about1second);
digitalWrite(RELAY4, RELAY_ON);
Serial.print("4");
Serial.println(" relays on ...");
Serial.println("setup() done...");
}
void loop()
{
// do nothing
}
(deleted)
spycatcher2k:
explain, line by line to the OP exactly what is happening
For all I know, OP doesn't give a rat's how it works, and just wants to get the 2y old project out the door. S/he, who has been doing this for 2y after all (admittedly with no evident progress ) after looking up the various commands in the reference, is more than welcome to ask.
spycatcher2k:
why you used this method of programming, rather than a much shorter Array based code.
That's simple: if OP hasn't progressed from here to today, he or she's definitely not going to be ready for an array approach. The approach I took is at likely to be a bit intuitive to the OP whereas an array approach would more likely not be.
But seeing as you asked:
// https://forum.arduino.cc/index.php?topic=664010
// 11 feb 2020
// relays in an array now... note numbered from 0 to 3 now, not 1-4
//turn on 4 relays, in sequence with about a 1 second delay
//between as soon as the unit is powered up
int about1second = 1001;
//pins from https://forum.arduino.cc/index.php?topic=513098
const byte RELAY[] = {6, 7, 8, 9};
const byte howManyRelays = sizeof(RELAY) / sizeof(RELAY[0]);
//*************************
//to use RELAY_ON or RELAY_OFF in digitalWrite(), instead of HIGH or LOW
//(un-)comment the next line depending on the relay's logic <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//#define weHaveAnActiveLowRelay
#ifdef weHaveAnActiveLowRelay
#define RELAY_ON LOW // most relay modules use this *active low* logic
#define RELAY_OFF HIGH
#else
#define RELAY_ON HIGH // for testing with an *active high* relay or led
#define RELAY_OFF LOW
#endif
//************************
void setup()
{
Serial.begin(9600);
Serial.println("setup() starting...");
Serial.print("... turning on relay ");
for (byte i = 0; i < howManyRelays; i++)
{
pinMode(RELAY[i], OUTPUT);
digitalWrite(RELAY[i], RELAY_ON);
Serial.print(i);
delay(about1second);
}
Serial.println(", all relays on ...");
Serial.println("setup() done...");
}
void loop()
{
// do nothing
}
(We could always wait for code by Blackfin, of course.)
(deleted)
spycatcher2k:
the object of this forum
Link, please, to where the de jure object of this forum is actually documented.
I've long held the view that the de facto object of this forum is to belittle those who ask for help, and only once they've jumped through enough hoops and extended their ladders far enough to reach you riders on your high horses*, to grudgingly dole out little bits of help until eventually many helpees disappear.
(* There are 3 exceptions.)
The OP asked for "a quick scrip [sic]" and I provided one. If I want to do that, it's my prerogative to do so. If you think my help should be moderated away, report it and get it deleted; it's no skin off my teeth. If he or she should have asked in a different part of the forum, or should have reached for, or offered to reach for, their wallet, guess what? I couldn't care less.
(Try this with your local car machanic).
Funny you should say that: I recently took my wife's car back to where the alternator had been repaired, since it wasn't charging and turned out a nut holding a wire on had worked loose. Fixed in 30 seconds. But I also mentioned the car was running a bit rough: he got his guy to take the plugs out, bit oily, replaced on the house, simply because my local car machanic [sic] is a good guy.
Sometimes, we just do what's right.
(deleted)
WOW! I'm shocked and quite disappointed in this forum.
First, THANK YOU to the person who actually answered my sincere question. I had a quick need and don't have time to learn right now. Very much appreciated.
Second...this is not a "homework assignment"...my school days are long past...I have no idea where that came from. Why would someone immediately go negative and assume that? I doubt that is what this forum is supposed to be about.
Third...I had posted on here several years ago when I was experimenting with these great boards. Life got in the way and I had to put aside hobbies and am now returning only to be immediately shamed by others. Wow! That, is a horrible way to treat others...and certainly doesn't build a good reputation for this forum. It doesn't help anyone.
Very sad to ask a genuine question, while trying to learn, immediately be attacked and see a firestorm start.
Again a very sincere THANK YOU to the one person who reached out to help. I will study the code and will learn from it. Your kindness is much appreciated. Also thank you to those who asked for more info, like what platform or relay. Actually I would love to see the code explained...that's how those of us who want to learn do so. But, I don't want to suffer further attacks.
I would suggest that in the future, if someone asks for help, those who have a genuine love of this hobby and want to help others learn, reach out. Those who have nothing good to contribute, belittle others, be negative, or charge for their services...keep quiet. Or simply tell the OP that "this isn't the place to get help". Negativity is counter-productive.
I'm always open to constructive criticism, but not false accusations and attacks that are unwarranted.
At least there are good mechanics out there! But people even had negative things to say about that! How sad.
Lesson learned...don't ask for help on this forum, it's not the place. I'm sure I will be banned now...simply for sincerely asking for help.
fire41fighter:
while trying to learn, immediately be attacked and see a firestorm start.
Ah, this is obviously some strange usage of the word 'learn' that I wasn't previously aware of.
fire41fighter:
New to coding and need some help. Can someone write a quick scrip for me that will turn on 4 relays, in sequence with about a 1 second delay between as soon as the unit is powered up? I would appreciate any help. Thanx!
There are many example programs, both in the Arduino IDE and elsewhere. Learning need not come from this forum. The usual way that this forum works (I know, I know, de facto vs. de jure) is that someone posts their existing code (posting properly is a bonus, especially for people such as me using a mobile device) and asks for assistance with a specific issue. Requests for an entire but quite simple program (especially when the request is for a script but the result would be C or C++ code) often turn out to be for a school assignment - not here, not after two years.
There are many willing helpers available through this forum but a student should be using other resources as well. Some of the helpers here have written excellent tutorials that should be studied. I have not written one yet because I am too busy yelling at the damn kids to get off my damn lawn.