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
}
}
millis()
?if
statement is unnecessarily long ... you are testing values that were tested already