0

How do run a code on arduino for only 60 secondes and than shut it down? I know I have to use the millis() function, but I do not know how to it in my code. Sorry if it looks unclear, but I only need to know how to program the millis() function. This is my first code so I have no experience.

int main(void)
{
pin_mode(A13,INPUT);
pin_mode(A12,INPUT);
pin_mode(A11,INPUT);
pin_mode(A10,INPUT);
pin_mode(A15,INPUT);
pin_mode(D0,OUTPUT);
pin_mode(D1,OUTPUT);
pin_mode(D2,OUTPUT);
pin_mode(D3,OUTPUT);

while (1)
{
    int potmeter1 = read_analog_pin(A13);
    if (potmeter1 >= 1000 && potmeter1 < 1100){
    gen_pwm(D0, 1000, 100);

        int potmeter2 = read_analog_pin(A12);
        if (potmeter2 >= 2000 && potmeter2 < 2050){
        gen_pwm(D1, 1000, 100); 

            int potmeter3 = read_analog_pin(A11);
            if (potmeter3 >= 500 && potmeter3 < 525){
                gen_pwm(D2, 1000, 100);     

                    int potmeter4 = read_analog_pin(A10);
                    if (potmeter4 >= 3500 && potmeter4 < 3510){
                        gen_pwm(D3, 1000, 100);

    {
        int lichtsensor = read_analog_pin(A15);
        if (lichtsensor >= 2000 && lichtsensor < 3500 && potmeter1 >= 1000 && potmeter1 < 1100 && potmeter2 >= 2000 && potmeter2 < 2050 && potmeter3 >= 500 && potmeter3 < 525 && potmeter4 >= 3500 && potmeter4 < 3510){
            gen_pwm(D0, 1000, 100);
            gen_pwm(D1, 1000, 100);
            gen_pwm(D2, 1000, 100);
            gen_pwm(D3, 1000, 100);
            _delay_ms(250);
            gen_pwm(D0, 1000, 0);
            gen_pwm(D1, 1000, 0);
            gen_pwm(D2, 1000, 0);
            gen_pwm(D3, 1000, 0);
            _delay_ms(250);

        }
    }
                }else{
                gen_pwm(D3, 1000, 0);
                }
            }else{
            gen_pwm(D3, 1000, 0);
            gen_pwm(D2, 1000, 0);
            }
        }else{
        gen_pwm(D3, 1000, 0);
        gen_pwm(D2, 1000, 0);   
        gen_pwm(D1, 1000, 0);
        }
    }else{
    gen_pwm(D3, 1000, 0);
    gen_pwm(D2, 1000, 0);
    gen_pwm(D1, 1000, 0);
    gen_pwm(D0, 1000, 0);
    }


                }
            }

I have merged my program with your program and I have added the #include "Arduino.h". The millis() function is colored now and works. My code still won't stop at 60 secondes, did I do something wrong?

#include "hva-xmega.h"
#include "Arduino.h"


unsigned long EndTime;

void setup()
{
    pin_mode(A13,INPUT);
    pin_mode(A12,INPUT);
    pin_mode(A11,INPUT);
    pin_mode(A10,INPUT);
    pin_mode(A15,INPUT);
    pin_mode(D0,OUTPUT);
    pin_mode(D1,OUTPUT);
    pin_mode(D2,OUTPUT);
    pin_mode(D3,OUTPUT);


    EndTime = millis() + (60*1000L);
}


void loop() {
    if( millis() < EndTime ){

while (1)
{
    int potmeter1 = read_analog_pin(A13);
    if (potmeter1 >= 1000 && potmeter1 < 1100){
        gen_pwm(D0, 1000, 100);

        int potmeter2 = read_analog_pin(A12);
        if (potmeter2 >= 2000 && potmeter2 < 2050){
            gen_pwm(D1, 1000, 100);

            int potmeter3 = read_analog_pin(A11);
            if (potmeter3 >= 500 && potmeter3 < 525){
                gen_pwm(D2, 1000, 100);

                int potmeter4 = read_analog_pin(A10);
                if (potmeter4 >= 3500 && potmeter4 < 3510){
                    gen_pwm(D3, 1000, 100);

                    {
                        int lichtsensor = read_analog_pin(A15);
                        if (lichtsensor >= 2000 && lichtsensor < 3500 && potmeter1 >= 1000 && potmeter1 < 1100 && potmeter2 >= 2000 && potmeter2 < 2050 && potmeter3 >= 500 && potmeter3 < 525 && potmeter4 >= 3500 && potmeter4 < 3510){
                            gen_pwm(D0, 1000, 100);
                            gen_pwm(D1, 1000, 100);
                            gen_pwm(D2, 1000, 100);
                            gen_pwm(D3, 1000, 100);
                            _delay_ms(250);
                            gen_pwm(D0, 1000, 0);
                            gen_pwm(D1, 1000, 0);
                            gen_pwm(D2, 1000, 0);
                            gen_pwm(D3, 1000, 0);
                            _delay_ms(250);

                        }
                    }
                    }else{
                    gen_pwm(D3, 1000, 0);
                }
                }else{
                gen_pwm(D3, 1000, 0);
                gen_pwm(D2, 1000, 0);
            }
            }else{
            gen_pwm(D3, 1000, 0);
            gen_pwm(D2, 1000, 0);
            gen_pwm(D1, 1000, 0);
        }
        }else{
        gen_pwm(D3, 1000, 0);
        gen_pwm(D2, 1000, 0);
        gen_pwm(D1, 1000, 0);
        gen_pwm(D0, 1000, 0);
            }
        }
    }   
}
 else {
     // Time is up! Hang here
     for(;;)
     ;      // infinite loop here stops further processing
    }
 }
  • look at the BlinkWithoutDelay example code in the Arduino IDE – jsotola Jun 9 at 16:31
  • 1
    It doesn't look like you are using an Arduino core. What framework are you using? Does it provide something similar to millis()? – Edgar Bonet Jun 9 at 16:38
  • note: the long if statement is unnecessarily long ... you are testing values that were tested already – jsotola Jun 9 at 17:47
  • My university designed his own board, but it uses a atxmega256a3u microcontroller. I do not know if it provide something similar as millis(). – Dennis de Kruijf Jun 9 at 19:08
  • @DennisdeKruijf, it is not Arduino. it is a proprietary board and library. we can't help you – Juraj Jun 10 at 7:58
2

Note that in an Arduino environment, main() is already provided for you. It calls your function void setup(void){ ... } once, then loops forever, calling your function void loop(void){ ... }. Either function may be empty but both must be present.

Without going into the specifics of your code, here is a program that fits the Arduino conventions, and will execute some procedure for 60 seconds, then hang, doing nothing:

unsigned long EndTime;

void setup()
{
   // Any setup code you need goes here.


   EndTime = millis() + (60*1000L);
}


void loop() {
   if( millis() < EndTime ){
      // Do your processing here
   }
   else {
      // Time is up! Hang here
      for(;;)
         ;      // infinite loop here stops further processing
   }
}

My setup codes are the pin_modes right? and everything below is my processing?

Update:

Correct - Put pin modes and anything else that needs to be done once, at the beginning of the run in setup(). Everything else goes in loop(). Or if you're more comfortable writing your entire program starting with main() (as we do in other environments than Arduino), you can use the setup() function exactly as you would use main(), and leave loop() empty. The idea of a loop function is that embedded programs are typically not meant to end as long as the device has power. There are instances when this isn't strictly true and you seem to have one example.

I have to remove the int main(void) from my code when I use your code right?

Correct - the Arduino environment provides a main() function, so writing your own - within that environment - would result in a multiple-definition error.

Update 2:

"WProgram.h" is obsolete. Instead, use: #include <Arduino.h>. There is no function called elapsedMillis(), just millis(). You won't need an include statement for it, or any of the other standard or common stuff: Serial I/O, digital I/O, analog I/O, pin definitions for specific boards, etc. 'Arduino.h' pulls in all that stuff. Your university should have add a description of the proprietary board to the Arduino's board description file.

(But if you/they are not using standard Arduino libraries, board descriptions, etc., then there is a lot of behind-the-scenes "housekeeping" you may be responsible for. That isn't something we can know; you may have to ask those kinds of questions of others more familiar with your university's programming environment. I think it is unlikely they would abandon the existing/working resources, but it's something to keep in mind.)

  • Alternatively, if (millis() >= 60000) exit(0);. – Edgar Bonet Jun 9 at 18:26
  • My setup codes are the pin_modes right? and everything below is my processing? I don't have much knowledge about programming ^^ . I have to remove the int main(void) from my code when I use your code right? @JRobert – Dennis de Kruijf Jun 9 at 19:19
  • problem is, it is not Arduino, so the answer is not applicable – Juraj Jun 10 at 7:59
  • I have done exactly what you said and my program still works fine, except the millis() function. I have added the #include "elapsedMillis.h" library, but the millis() function stays as a normal text, so my program doesn't stop after the 60 secondes. Furthermore it can't find the WProgram.h: file or directory. Do you maybe know how to fix that? @JRobert – Dennis de Kruijf Jun 10 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.