I am trying to make a counter using the millis function. The for statment seem to ignore the millis function and only prints 15 i'm sure is because i don't know how it works although it looks simple. i tried to move the for statment to the top and bottom and diffrent places in the middle. If some one can look at this and tell me where i messed up or even if it will work at all
Thank
here is my code
void daycounter(){ //function call in setup
int i;
const unsigned long interval = 10000; // interval at which to blink (milliseconds)
unsigned long currentMillis = millis();
unsigned long previousMillis = 0;
{
if (currentMillis - previousMillis >= interval);
{
for (i = 0; i < 15; i++); //trying to increment 1 to 15 based on millis
previousMillis = currentMillis;
}
}
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.blink();
lcd.setCursor(2,1),
lcd.print("start"), // so i could tell that loop started over
delay(1000);
lcd.clear();
//Print day on display
delay(1000);
//int i; //Trying to increment count from 1 to 15
//for (i = 1; i < 16; i++){
delay(2000); //Just trying to slow thing down
lcd.clear();
lcd.setCursor (0,0);
lcd.print("DAY (");
lcd.print(i);
lcd.print(")");
Serial.println(i); //just to debug
void daycounter() //function call in setup
{
int i;
const unsigned long interval = 10000; // interval at which to blink (milliseconds)
unsigned long currentMillis = millis();
unsigned long previousMillis = 0; //// This is not 'static' so it will be reset each call
{ ///// Unnecessary open-bracket
//// Does nothing because the body of
//// 'if' statement is one null statement: ';'
if (currentMillis - previousMillis >= interval)
;
{ //// Unnecessary open-bracket
//// This loop does nothing 15 times because
//// the body of the loop is one null statement: ';'
for (i = 0; i < 15; i++)
; //trying to increment 1 to 15 based on millis
previousMillis = currentMillis;
} ///// close-bracket matching unnecessary open-bracket
} ///// close-bracket matching unnecessary open-bracket
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.blink();
lcd.setCursor(2, 1), ///// This comma should have been a semicolon
lcd.print("start"), ///// This comma should have been a semicolon
// so i could tell that loop started over
delay(1000);
lcd.clear();
//Print day on display
delay(1000);
//int i; //Trying to increment count from 1 to 15
//for (i = 1; i < 16; i++){
delay(2000); //Just trying to slow thing down
lcd.clear();
lcd.setCursor (0, 0);
lcd.print("DAY (");
lcd.print(i);
lcd.print(")");
Serial.println(i); //just to debug
}
Still confused when i make sketch with the for statement for (i=0; 1 <15;i++);
my display shows DAY () and inside the parentheses it counts from 1 to 15 when i try to add the miiliis functions all that is display DAY (15). I did not under stand about the for statement being null. i am very new to this programing those {} and : are sometimes hard to figure out. any way i am very grateful for you help may be soon i will get a hold on this i am working on a big project and i am beginning to think i may have taken on to much, but as they say no guts no glory. and i have no time line to worry a bout. Again thanks
Still having problems with millis function. Every youtube video i have watched and rewatched and watched again all say the same thing. Once the arduino starts so does the millis counter. if you want an event to happen every second then you up date the current mllis to the previous mills and the if statement s starts again.below is my code and serial monitor results. i do not under stand what is going on. thanks for you help
const unsigned long interval = 2000;
unsigned long previousMillis = 0;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval){
12:03:14.427 -> current =1929 previous =0
12:03:14.474 -> current =1956 previous =0
12:03:14.474 -> current =1984 previous =0
12:03:14.521 -> hello
12:03:14.521 -> current =2013 previous =2013
12:03:14.568 -> hello
12:03:14.568 -> current =2054 previous =2054
12:03:14.615 -> hello
12:03:14.615 -> current =2096 previous =2096
12:03:14.662 -> hello
12:03:14.662 -> current =2138 previous =2138
12:03:14.662 -> hello
As you can see untill millis reaches 2 seconds it does not print hello, but every time after 2 seconds current = previous which should return false the if statement should skip over print hello untill millis gets to 2000 seconds.
at least thats my understanding Let me know if i am wrong thanks
David
sorry it took so long to get to get back the board would not let my replay. making the varible static fixed my problem.
now i need some guidance on passing values Please help me kinda new to arduino. I need help wrapping by head around passing varibles. i have a function called
viod gravity
if (((((senseAve == 275) || ( senseAve == 276) || (senseAve == 277) || (senseAve == 278) || (senseAve == 279))))){ //||(y == 275){
float a = 1.100;
Serial.print("cg=");
Serial.println(a, 3);
lcd.println(a);
}
if ((((((senseAve == 280) || (senseAve == 281) || (senseAve == 282) & (y == 280)|| (y == 281) || (y == 282)))))) {
float a = 1.090;
Serial.print("og=");
Serial.println(a,3 );
}
The if statements continue like this until i get to 1.000 each increment is .010.
then there is more, y is a static unsigned value based on the sensorAve.
if (((((y == 275) || ( y == 276) || (y == 277) || (y == 278) || ( y == 279))))) {
float b = 1.100;
Serial.print("sg=");
Serial.println(b , 3 );
lcd.print(b , 3);
}
//if (((y == 280) || (y == 281) || (y == 282))) {
if (((y == 293) || (y == 294))) {
float b = 1.050;
Serial.print("sg =");
Serial.println(b , 3);
lcd.print(b , 3);
delay(5000);
These if statments continue untill 1.000
Now i need to pass these variables a and b to another function i named void passby (cg, og) so i can do some math with these two varables
so i add passby(a,b); at the end and i get a is not in Scope. Which i Understand so then i put float a; then float b; right after void garvity then when i pass a and b to the passby function it passs 0.000 and 0.000 if i give flaot a and float b a value such as float a = 1.050 and float b = 1.060 those will be passed to my bypass function is there way to pass the values of my if statements to floats at the top of the void gravity function. i have tried so many different thing i have totally confused my self. so if any one can put on the right path i would greatly appreciate it.
david
Thank you for your response. I have been working on this almost four months. Lord knows i often get ahead of my self and have to slow down. As far as step by step I thought i was doing that. I thought of a project i wanted to do, and what i wanted to do with it. I started with the first thing i wanted the sketch to do and I read and studied until i figured out how to do it. then moved on two step and did the same. Passing these variables is the last step. I need to figure out how to do some math with these two variables and write the results to the 20x4 lcd display . then send the same variable a to my data logger so i can write that current value to sdcard. and other than some house keeping and and cleaning up the code it will be done. I am sure if some one who knows what to
look at the code it is full errors. and i would value that very much. At my stage of skill which is low i feel good when the code compiles. I learn best when i am doing until i figure things out. if you have suggestion about going step by step i am all ears. thank again
David
Thanks for your reply. I tried for quite awhile to find another way to write this these if statements, and like i said in my previous post there are a lot of mistakes i have made. But the only way i know how to learn the code is to try to do it, and to get some advice from people like you and others who know more and can set me on the right path. Do you have any suggestions for reference materials that my help in my journey to write c++ code. Thanks
David
Keep things simple. When you get stuck, isolate your problem in a new smaller sketch from your main sketch and work out that problem alone.
int i = 1;
const unsigned long interval = 86400000; // one day in milliseconds
unsigned long previousMillis;
void setup() {
Serial.begin(9600);
previousMillis = millis();
Serial.print(i);
}
void loop() {
if (millis() - previousMillis >= interval) {
previousMillis += interval;
if (i >= 15) i = 1;
else i++; //increment 1 to 15 based on millis
Serial.print(i);
}
}
This line when run starts by assigning i to 0 and then loops through itself 15 times (0-14) giving i a final value of 15 with the final increment before leaving this line to go to the next.
No, this is completely inadequate. It will take you forever to learn that way. Because, then you are not benefiting from the many online resources such as C/C++ reference sites, tutorials, books (yes I have one C++ reference book), so you can follow a self directed learning path without a lot of guesswork.
In the context of Arduino, there is so much educational material available for you, because it is specifically a learning platform. You ask for these reference materials, but they are all easy to locate in a fraction of an hour, using Google. If you bookmark a few good sites, you can build an online "shelf" to go to for answers. There is, in fact, already a good resources list in the introductory forum threads.
When you do this, don't fall into the temptation of following "brag and run" sites that pretend to make things easier. Usually, the people who post there, have only reprocessed other projects, their project is rather poorly done, they exaggerate their competence or hide their incompetence.
If you adopt the attitude of a regular student and treat it as a subject you will learn far more than you could ever gain here, one bad line of code and one problem/question at a time.
The mistakes you made happened mainly because you did not research it.
Again thanks for your comments. So what kind of question are acceptable on this forum. I Thought this forum was to ask questions about coding. when one was stuck. I know that i have a lot to learn and am willing to do it. As far as research goes i did a lot. and i knew there had to be a better way to to do it, Once I got the code to do what i needed I kinda just moved on to other things. From now on i want give up as quickly.
David