const byte TIME = 5;
unsigned long previousMillis = 0;
int Timer=0 ;
int Printed=0;
int TIME1[] = {LOW,LOW,LOW,LOW,LOW};
int Case1=500;
int Case2=20000;
int Case3=30000;
long Case4=40000;
long Case5=50000;
#define D4 3
unsigned long previousMillisCase1;
unsigned long previousMillisCase2;
unsigned long previousMillisCase3 ;
unsigned long previousMillisCase4;
unsigned long previousMillisCase5;
void setup()
{
Serial.begin(115200);
}
void loop(){
unsigned long currentMillis=millis();
if ((unsigned long)(currentMillis - previousMillisCase1 >=Case1))
{
TIME1[0]=!TIME1[0];
previousMillisCase1 = currentMillis;
};
if ((unsigned long)(currentMillis - previousMillisCase2 >=Case2))
{
TIME1[1]=!TIME1[1];
previousMillisCase2 = currentMillis;
};
if ((unsigned long)(currentMillis - previousMillisCase3 >=Case3))
{
TIME1[2]=!TIME1[2];
previousMillisCase3 = currentMillis;
};
if ((unsigned long)(currentMillis - previousMillisCase4 >=Case4))
{
TIME1[3]=!TIME1[3];
previousMillisCase4 = currentMillis;
};
if ((unsigned long)(currentMillis - previousMillisCase5 >=Case5))
{
TIME1[4]=!TIME1[4];
previousMillisCase5 = currentMillis;
};
if ((TIME1[0]==1)&&Printed==0) //suppress i=0 timer only show 1-4 i=0 is for status timer
{
int i=0;
for(int i = 1; i < 5; i++)
{
Serial.print(TIME1[i]);
};
Serial.println();
Printed=1;
}
else
{
if (TIME1[0]==0)
Printed=0;
};
}
this is what I tried but return wont return array . how do I do it ?
the //*** are what I tried
I am inserting this into a larger code and want to make a function so the main code is not so clumsy. I was also going to put it in its own tab .
const byte TIME = 5;
unsigned long previousMillis = 0;
int Timer=0 ;
int Printed=0;
int TIME1[] = {LOW,LOW,LOW,LOW,LOW};
int Case1=500;
int Case2=20000;
int Case3=30000;
long Case4=40000;
long Case5=50000;
#define D4 3
unsigned long previousMillisCase1;
unsigned long previousMillisCase2;
unsigned long previousMillisCase3 ;
unsigned long previousMillisCase4;
unsigned long previousMillisCase5;
void setup()
{
Serial.begin(115200);
}
void loop(){
//*******
[color=red]int TimeState ();[/color]
//********
if ((TIME1[0]==1)&&Printed==0) //suppress i=0 timer only show 1-4 i=0 is for status timer
{
int i=0;
for(int i = 1; i < 5; i++)
{
Serial.print(TIME1[i]);
};
Serial.println();
Printed=1;
}
else
{
if (TIME1[0]==0)
Printed=0;
};
}
//*****
[color=red]int TimeState ()
{
unsigned long currentMillis=millis();
if ((unsigned long)(currentMillis - previousMillisCase1 >=Case1))
{
TIME1[0]=!TIME1[0];
previousMillisCase1 = currentMillis;
};
if ((unsigned long)(currentMillis - previousMillisCase2 >=Case2))
{
TIME1[1]=!TIME1[1];
previousMillisCase2 = currentMillis;
};
if ((unsigned long)(currentMillis - previousMillisCase3 >=Case3))
{
TIME1[2]=!TIME1[2];
previousMillisCase3 = currentMillis;
};
if ((unsigned long)(currentMillis - previousMillisCase4 >=Case4))
{
TIME1[3]=!TIME1[3];
previousMillisCase4 = currentMillis;
};
if ((unsigned long)(currentMillis - previousMillisCase5 >=Case5))
{
TIME1[4]=!TIME1[4];
previousMillisCase5 = currentMillis;
};
return TIME1;
};
[/color]//****
-
Use code tags. The forum software eats some of your code if you don't.
-
It appears poorly-formatted. Try the auto-formatter in Arduino. Control-T. If it makes a mess of your code then the code was wrong to begin with.
-
What do you want this code to do?
-
What does it actually do?
code works perfect until I try and make function .
the code will be used for a status led . the led will blink according to TIME1 true . this is just single function part of a whole. The LED will mimic the TIME1 and I will create a state 1-3 and pass that state to the led.
the problem im having is breaking to code into a function , I cant get it to return the array back to the main loop for use.
Fix your posts to show the code correctly. Then we'll look at it. I've got some ideas what may be wrong but I can't be sure when it's posted like that.
I edited it should be in code snippit now
thanks
If you now learn to properly indent (as mentioned above, use T in the IDE), it's a lot easier for us to help,
Anyway
void loop() {
int TimeState ();
...
...
What do you expect the above to do? It's a function prototype (and does not belong there). If you want to call the function, use
void loop() {
TimeState ();
...
...
return TIME1;
What do you want this to do? You declared the function was going to return an integer but you're trying to return an array of integers. The variable TIME1 actually contains a pointer to the first element in the array so you could declare the function as int * TimeState().
But TIME1 is global. Any function can read and modify it. You don't need to return it at all. Declare the function as void type.
if ((unsigned long)(currentMillis - previousMillisCase1 >= Case1))
You have the brackets the wrong way around. You're taking the boolean result of the >= and casting it to an unsigned long before the if() converts it back to a boolean. This should compile just fine without the type cast.
Thanks guys it was that simple. All I had to do is void function and don't worry about return because of the global TIME1. it works perfect with the two changes. Thanks for the help and the patience. Im learning but if have not done any programing in 20 years.