Viewing 15 posts - 1 through 15 (of 15 total)
  • Messing about with an Arduino v2
  • spursn17
    Free Member

    Me again!

    I think that I can create a custom function to do repetitive stuff, but I don’t seem to be able to despite reading loads of tutorials. I understand that I can use it to calculate variables (I think!), but I want my function to switch stuff on and off regularly. The code I’ve written is this….

    const int motorPin1 = 7;
    const int motorPin2 = 8;

    void setup() {
    pinMode(motorPin1, OUTPUT);
    pinMode(motorPin2, OUTPUT);

    }

    void loop() {

    digitalWrite(motorPin1, HIGH); // I need the code between the two comments…
    delay(200);
    digitalWrite(motorPin1, LOW);
    delay(200);
    digitalWrite(motorPin2, HIGH);
    delay(300);
    digitalWrite(motorPin2, LOW);
    delay(300); // …as a function so I can cut down the code
    }

    This is the shortened version as the actual one will be a lot longer and more complicated.

    What I need is ” Function A = runs this that and the other several times”, “Function B runs it a different way” and so on.

    Any ideas?

    aracer
    Free Member

    You appear to already have a function there, though it doesn’t do a loop (despite being called that). Does what you have there work for a start, and what exactly do you want to do with it? Is it the lengths of delay you want to vary?

    I’m not quite sure what it is you’re trying to achieve here, or what the problem is.

    nickjb
    Free Member

    This sort of thing:

    const int motorPin1 = 7;
    const int motorPin2 = 8;

    void setup() {
    pinMode(motorPin1, OUTPUT);
    pinMode(motorPin2, OUTPUT);

    }

    void loop() {
    runMotor1();
    runMotor2();
    }

    void runMotor1() {
    digitalWrite(motorPin1, HIGH);
    delay(200);
    digitalWrite(motorPin1, LOW);
    delay(200)
    }

    void runMotor2 () {
    digitalWrite(motorPin2, HIGH);
    delay(300);
    digitalWrite(motorPin2, LOW);
    delay(300);
    }

    Loop and setup are special functions in the Arduino code. Setup will run first then loop will loop for ever. Other functions can be created in the same way and then called. You can send and return variables too if you like

    void loop() {
    runMotor1(200);
    runMotor2(300);
    }

    void runMotor1(int delayTime) {
    digitalWrite(motorPin1, HIGH);
    delay(delayTime);
    digitalWrite(motorPin1, LOW);
    delay(delayTime)
    }
    void runMotor2(int delayTime) {
    digitalWrite(motorPin1, HIGH);
    delay(delayTime);
    digitalWrite(motorPin1, LOW);
    delay(delayTime)
    }

    Or


    void loop() {
    runMotor(motorPin1, 200);
    runMotor(motorPin2; 300);
    }

    void runMotor(int Pin, int delayTime) {
    digitalWrite(Pin, HIGH);
    delay(delayTime);
    digitalWrite(Pin, LOW);
    delay(delayTime)
    }

    richmars
    Full Member

    (I am not a software engineer)
    But, there are better ways of doing delays, that don’t stop anything else from happening.
    Avoid Delay

    nickjb
    Free Member

    I wouldn’t say better, just different. If you need other things to happen then use the millis method but if you don’t then delay is fine.

    richmars
    Full Member

    Agreed, but if you’re turning a motor on, you may also need to look at other things, like sensors, which you can’t do with delay.
    It all depends what else is happening.

    tomparkin
    Full Member

    I guess using delay() would probably be easiest for initial prototyping, but its good to at least be aware of alternative approaches for later if you do need the extra flexibility.

    Otherwise nickjb is right so far as I can see. The only caveat being that (if I’m correct in recalling that the arduino env is basically C++ with a few helpers) the compiler will need to know about runMotor1() and runMotor2() before you call them in loop(). So you will need to shift them up before loop() in your code, or declare them before loop().

    spursn17
    Free Member

    Thanks everyone for your help, really appreciate it as a lot of the books I’ve read assume a certain level of knowledge already (which I don’t have!).

    It’s not going to be sensing anything or using any variables, I just want to switch motors on and off in a sequence repeated continuously, but the periods that they are on need to have different timings hence wanting to shorten the code.

    nickjb, thanks, that was close to what I’d written but I didn’t put runMotor1();
    runMotor2(); after void loop. If I want to run that function again later in the loop do I just write…

    void runMotor1() {
    }

    or do I have to code the whole function again?

    spursn17
    Free Member

    tomparkin, thanks. I realise that there are better ways of doing the delays but I’m doing it this way as I really am just starting out and trying not to get in above my neck (too late 😀 ).

    nickjb’s code compiled OK for me

    tomparkin
    Full Member

    You call functions like so: runMotor1().

    So in context:

    void runMotor1() {
    // code here…
    }

    void loop() {
    runMotor1();
    runMotor1();
    }

    a lot of the books I’ve read assume a certain level of knowledge already (which I don’t have!).

    I think this may be your problem :-). I know it may feel like a step backward, but going and learning a bit about C syntax would be worthwhile at this point. Its a long time since I started learning C but I think these days something like Zed Shaw’s Learn C The Hard Way (look it up, it’s free online) might help you get the basics down.

    allthepies
    Free Member

    runMotor2(); after void loop. If I want to run that function again later in the loop do I just write…

    void runMotor1() {
    }

    The construct:

    runMotor2(); after void loop. If I want to run that function again later in the loop do I just write…

    void runMotor1() {
    }

    Is defining the function “runMotor1” (in the case above with no actual embedded code). To execute the function then you’d use the construct:

    runMotor1();

    within your loop() function.

    If you want to run the function again then just place “runMotor1();” again in the loop() function.

    e.g.

    void loop() {
    runMotor1(200);
    runMotor2(300);
    runMotor1(100);
    }

    spursn17
    Free Member

    Cheers tomparkin, I’ve been reading Arduino Cookbook and Arduino for Dummies but I need to do something practical to relate it to so that I can understand what I’m reading.

    allthepies, thanks.

    So if this is my code….

    const int motorPin1 = 7;
    const int motorPin2 = 8;

    void setup() {
    pinMode(motorPin1, OUTPUT);
    pinMode(motorPin2, OUTPUT);

    }

    void loop() {
    runMotor1();
    runMotor2();
    }

    void runMotor1() {
    digitalWrite(motorPin1, HIGH);
    delay(200);
    digitalWrite(motorPin1, LOW);
    delay(200);
    }

    void runMotor2 () {
    digitalWrite(motorPin2, HIGH);
    delay(300);
    digitalWrite(motorPin2, LOW);
    delay(300);
    }

    …and I want to run the function ‘runMotor1’ again later in the loop I should just write ‘runMotor1();’ ?

    nickjb
    Free Member

    and I want to run the function ‘runMotor1’ again later in the loop I should just write ‘runMotor1()’ ?

    Yep. That’s the point of functions

    void loop() {
    runMotor1();
    runMotor2();
    runMotor1();
    runMotor1();
    delay(1000);
    runMotor1();
    }

    Also you can call a function from inside another function if you want to compress things more. It doesn’t matter which order the functions are written in ie you don’t needn’t define a function before you call it

    spursn17
    Free Member

    Fantastic, thanks all. I’ve been trying to clear this up in my head by reading since Friday, 15 minutes on here and I understand it now!

    I’ll be back soon! 😀

    tomparkin
    Full Member

    Good luck with it 🙂

    What are you making, out of interest? I’m envisaging some kind of stepper-driven dropper seat post…

Viewing 15 posts - 1 through 15 (of 15 total)

The topic ‘Messing about with an Arduino v2’ is closed to new replies.