DELAY FOR PINMODE 1 AND 2

IT IS POSSIBLE TO USE A DELAY FOR SEVERAL DIFFERENT PINS? BECAUSE I TRY TO USE A DIFFERENT DELAY WITH THIS CODE:

void setup () {
pinMode (2, OUTPUT);
pinMode (3, OUTPUT);
}
void loop () {
int pin2 = digitalRead (2); // put pin 3 in the high state
int pin3 = digitalRead (3);
if (pin2 == HIGH)

  • {*
  • digitalWrite (2, LOW);*
  • digitalWrite (2, HIGH);*
  • delay (1);*
  • }*
  • else*
  • {*
  • digitalWrite (2, LOW);*
  • digitalWrite (2, HIGH);*
  • delay (1);*
  • }*
    if (pin3 == HIGH)
  • {*
  • digitalWrite (3, LOW);*
  • digitalWrite (3, HIGH);*
  • delay (6);*
  • }*
  • else*
  • {*
  • digitalWrite (3, LOW);*
  • digitalWrite (3, HIGH);*
  • delay (6);*
  • }*
    }

BUT WHEN I LOAD THE PROGRAM BOTH WORK AT THE SAME SPEED

How are you judging 6ms vs.1ms ?

Please remember to use code tags when posting code

@marcosfrias28

.

Welcome to the Arduino forums.

The people here want to help you but to do that we need certain bits of information. Without the correct information it is difficult or impossible to give you the help and advice you need. To help us to help you please read General guidance below and How to use this forum and provide the information asked for.

Being new here you might think this is asking too much or having rules for the sake of rules, that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of message while we try to get you to tell us what we need in order to help you, this is frustrating for you and frustrating for us.

Common mistakes for people posting here the first time:

Code problems:
Not posting any code while expecting us to work out what is wrong with your code; we are not telepathic and can only find problems in code we can see.
Posting a snippet of code in the belief that the problem is in the code snippet. It is almost always the case that the problem is not where you think it is but elsewhere in the code, hence we need all the code.
Not correctly formatting code. Before posting code use Tools / auto format at the top of the IDE, or use control T. Once you’ve done that use edit / copy for forum.
Posting code without code tags (</>), these are explained in the instructions linked to above. Using code tags makes the code easier to read. Not using code tags means some of the code gets interpreted as HTML with the result that it is displayed with smiley faces and other stuff that should not be there.
Not being clear about what is expected from code or what happened instead. Code ALWAYS works, that is the nature of code. Whether it does what you expect is a different thing altogether. We need to know what you expected the code to do and what happened instead.
Asking for complete code; we are not here to write code for you, we are here to help you if you get stuck writing code yourself. If you really want someone to write code for you please click on ‘report to moderator’ and ask them to move this to ‘Gigs and Collaborations’ and indicate how much you are willing to pay.

Schematics:
The language of electronics is a schematic or circuit diagram. Long descriptions of what is connected to what are generally useless. A schematic is a drawing of what connects to what, please make one and photograph it. We don’t mind if it’s hand drawn, scruffy and does not use the correct symbols. We do mind if you don’t at least try to post a schematic. Most images will self-attach to the bottom of your post but if it doesn’t Please read How to post an image

General:
Posting links to other sites where code or photos or schematics are hosted. Most of us will not follow such links, partly because of the risk that they hold malware or other unwanted content, partly to maintain everything on this site for the benefit of future users looking for an answer to a similar question and partly because we feel that if you want our help you should provide everything we need on this site not expect us to go hunting elsewhere for it. Exceptions are where you may be asked for a specific link such as a datasheet or product etc. However don’t post links to “similar” items only the ones you have there or want advice about.

Questions:
Not being clear about what is being asked, for example not asking a question at all or asking a vague question like ‘please help’ or some such thing. The same if you don’t follow somebody’s answer. Ask them to explain a little more and be clear you did not quite understand the answer you were given.

About us:
Those of us answering questions have a wide variety of backgrounds and electronics knowledge, some though working in electronics, some through electronics as a hobby, some both. Most of us are not trained as teachers so probably miss the finer points of how to teach and explain things effectively. We are not, with the rare exception, employees or representatives of Arduino.
Please also remember we are volunteers doing this for free in our spare time and are more inclined to help people who make it easy for us to provide help by providing the information we ask for.

About you:
We only know about you what you tell us; we need to know your level of experience with electronics or microcontrollers, if we don’t know then we can’t tailor our answer to your level of knowledge. We also don’t know if you have problems with English or language or communications in general unless you tell us. We can be sympathetic about problems we know about, but if you don’t tell us we don’t know.
Thank you.

COMMON ISSUES

  • Check you have a COMMON GROUND where required.
  • Where possible use USB 2.0 ports or a USB 2.0 POWERED HUB to rule out USB 3.0 issues.
  • Try other computers where possible.
  • Try other USB leads where possible.
  • You may not have the correct driver installed. CH340/341 or CP2102
  • There may be a problem with the board check your wiring first.
  • Remove any items connected to pins 0 and 1.

COMPUTER RELATED

  • close any other serial programs before opening the IDE.
  • Ensure you turn off any additional security / antivirus just to test.
  • There may be a problem with the PC try RESTARTING it.
  • You may be selecting the wrong COM port.
  • Avoid cloud/network based installations where possible OR ensure your Network/Cloud software is RUNNING.
  • Clear your browsers CACHE.
  • Close the IDE before using any other serial programs.
  • Preferably install IDE's as ADMINISTRATOR or your OS equivalent

Arduino specific boards

  • CH340/341 based clones do not report useful information to the "get board info" button.
  • NANO (Old Types) some require you to use the OLD BOOTLOADER option.
  • NANO (New Types) See the specific sections in the forum.
  • NANO (New Types) Install your board CORE's.
  • Unless using EXTERNAL PROGRAMMERS please leave the IDE selection at default "AVRISP mkII".
  • Boards using a MICRO usb connector need a cable that is both DATA and CHARGE. Many are CHARGE ONLY.

CREATE editor install locations.

  • On macOs ~/Applications/ArduinoCreateAgent-1.1/ArduinoCreateAgent.app/Contents/MacOS/config.ini
  • On Linux ~/ArduinoCreateAgent-1.1/config.ini
  • On Windows C:\Users[your user]\AppData\Roaming\ArduinoCreateAgent-1.1

Performing the above actions may help resolve your problem without further help.
Language problem ?
Try a language closer to your native language:

Thanks to all those who helped and added to this list.

You have constructs like, "if x do y, otherwise do y".

1ms and 6ms are both really really fast. There's no way you will be able to tell the difference by eye. Try something like 1000 and 5000.

Steve

int pin2 = digitalRead (2); // put pin 3 in the high state

Comment is inaccurate - digitalRead() does not change the voltage on the pin.

When you set the pin OUTPUT, assuming you have not previously written a value to it, it will drive the pin LOW.

So, I think your expectations here are wrong...

I apologize for making the wrong section to post
I modified the code, but it always adds the two delays and a different one is not assigned to each one as I need it to work

void setup () {
pinMode (2, OUTPUT);
pinMode (3, OUTPUT);
}
void loop () {
int pin2 = digitalRead (2);
int pin3 = digitalRead (3);
if (pin2 == HIGH)

  • {*
  • digitalWrite (2, LOW);*
    //digitalWrite (2, HIGH);
  • }*
  • else*
  • {*
  • // digitalWrite (2, LOW);*
  • digitalWrite (2, HIGH);*
  • }*
  • delay(1000);*
    if (pin3 == HIGH)
  • {*
  • digitalWrite (3, LOW);*
  • //digitalWrite (3, HIGH);*
    delay(5000);
  • }*
  • else*
  • {*
  • digitalWrite (3, HIGH);*
  • }*

}

Do you have a question?

Code tags?

marcosfrias28:
I apologize for making the wrong section to post
I modified the code, but it always adds the two delays and a different one is not assigned to each one as I need it to work

Because your code is not doing what you seem to think, it's what you think that needs fixing before you can understand what you have enough to know what to do.

What do you think those lines do?

What do you -really- want the sketch to do in hard detail? Simply if you don't really know you can't really say or code.

What EXACTLY does your latest code do, in detail? What do you want it to do that's different?

Steve

I would like to give two different speeds to two stepper motors, putting a delay of 1000 ms to make the movement in pin 2 faster, and 5000 ms in pin 3 to make it slower

Delay isn't going to do it for you. Use millis for timing. See Robin2's sticky thread at the top of the page.

OK, some basic things first.

You need to go and read the forum instructions so that you can go back and modify your original posts (not re-post them) - using the "More -> Modify" option below the right hand corner of your post - to mark up your code as such using the "</>" icon in the posting window. Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.

In fact, the IDE itself has a "copy for forum" link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don't forget to use the "Auto-Format" (Ctrl-T) option first to make it easy to read. If you do not post it as "code" it can easily be quite garbled and is always more difficult to read due to the font. Using italics is absurd.

It is inappropriate to attach it as a ".ino" file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And even that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks.

marcosfrias28:
I would like to give two different speeds to two stepper motors, putting a delay of 1000 ms to make the movement in pin 2 faster, and 5000 ms in pin 3 to make it slower

  1. Would it be okay if one led changed ON/OFF state every 1 second while the other changed state every 5 seconds?
  2. And of each does its thing independently?

Because when using delay it would take more code and be a pain to change to different rates. Every complication would require m-o-a-r code and that code will more and more resemble a bowl of spaghetti.

The tutorial at this address: Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs
will clear up how to achieve both 1 and 2. The main lesson is about code that blocks vs non-blocking code and the example teaches how to do that with timing. It shows that you can as many "timers" as you have memory for which can be liberating.

Time triggers are one kind of trigger and you know or learn others like pin state trigger and Serial.available() and then you use a variable that code in one independent section (like the 2 leds above, each runs by itself) changes and -that- triggers another thing to happen. By then you're ready to write state machine code, likely to make your own form if you don't get the lesson from someone else.

State Machine lesson is a little way down but near the top: Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking

I am not Nick Gammon whose site and tutorials those links are for.