10 Replies Latest reply on Apr 18, 2014 3:43 AM by Stefano84

    millis() ... help me


      Hi guys,

      my name is Stefano and I come from Italy, i'm so sorry for my english and i hope u guys can help me to resolve my problem. I've used this sketch for my first little project with Intel Galielo. Everything works well but when i start a Serial monitor the information are too fast! ... at the end of the code i've used a delay(100) because of the reactivity of the potentiometer with the RGB led. So, someone give me an idea with the command  ''millis()'', that allow me to use my serial monitor with an for example delay (5000) and the program will run without being stopped 'cause of the delay and still using the ''velocity'' of 100ms. I hope that u guys understand my problem.



      void setup ()
        Serial.begin (9600);
        pinMode (A0, INPUT);
        pinMode (A1, INPUT);
        pinMode (A2, INPUT);
        pinMode (3, OUTPUT);
        pinMode (5, OUTPUT);
        pinMode (6, OUTPUT);
      void loop ()
        int R = map ( analogRead (A2),0,1023,0,255);
        int G = map ( analogRead (A0),0,1023,0,255);
        int B = map ( analogRead (A1), 0,1023,0,255);
        Serial.print ("R: ");
        Serial.print ( R );
        Serial.print ("G: ");
        Serial.print ( G );
        Serial.print ("B: ");
        Serial.print ( B );
        analogWrite ( 6, R);
        analogWrite ( 3, G);
        analogWrite ( 5, B);
        delay (100);
        • 1. Re: millis() ... help me

          Do you mean using something like this:




          A technique like this is the standard way to "delay" execution in your Arduino sketch loop() function, but allow the rest of the loop() function code to continue processing.

          1 of 1 people found this helpful
          • 2. Re: millis() ... help me

            so, during the loop, i map the analogic input how the sketch say, and print on serial monitr the result, my goal is to make the result appear after 5 sec o r10sec just to avoid the massive amount of data. The rest of the loop how is going to work at 100ms ? i don't want move the potentiometer and after 5 sec see the light change. that's the point i don't understand. I'm not a programmer

            • 3. Re: millis() ... help me

              Hi Stefano,


              If you want to reduce the sampling/output rate of your RGB values coming from the analog pins, you should use the delay() function.


              To pause for 1 second, call delay(1000). As you can see, the units are in milliseconds.


              The millis() function is a timing function that reads out the number of milliseconds since the Real Time Clock (RTC) either started or rolled over. Just FYI, the RTC on the Galileo board will rollover every ~1.2 hours. This is because the RTC keeps track of time in increments of microseconds and has a 32bit register to count the number of microseconds elapsed. Once it hits 2^32-1, it will reset to 0.

              • 4. Re: millis() ... help me

                Hi billy

                i don't want reduce the sampling /output rate of my RGB values, I want increase the sampling/output!!...but... if i increase the delay() at 100ms as i did, when i start  serial monitor it works at 100ms and read the values is almost impossible, what i wanna do is decrease the time of the Serial.print() that READ on the analog pin without decrease the speed of sampling!


                i'm sorry if i can't explain well my idea because of my english.




                P.s. i've read an article in Italian that is explained how to do but i don't know how to use in my sketch... he call this ''trick'' like a kind of ''multitasking'', if u are able to translate the article here is the url Vittorio Zuccalà: Multitasking Arduino: millis() -- PARTE 1

                • 5. Re: millis() ... help me

                  You explain it fine.


                  You need to use the technique in the tutorial I proposed BlinkNotDelay (and not the delay() method, which prevents the loop running at all while it's asleep)


                  I can provide you with a sample piece of code later, no time to do it now since I am still in work.

                  But to work on the Galileo/Arduino without being able to write some simple C code is going to be ... challenging ...


                  Take a look at that tutorial while you're waiting for me to send you the answer :-) You'll probably have it done before I get around to it.

                  • 6. Re: millis() ... help me

                    Thanks Liam, i'm newbie in C ... if you have time, take a look at my Post Scriptum and translate the Article. It seems very interesting use of mills(), and he does exactly what i need... the program works at 300ms and he read the values at 3000ms without any delay()

                    • 7. Re: millis() ... help me



                      I think the code below will do what you want:


                      int numMillisToDelayForOutput = 1000;
                      int currMillis = 0;
                      int prevMillis = 0;
                      void setup()
                        Serial.begin( 9600 );
                        pinMode( A0, INPUT );
                        pinMode( A1, INPUT );
                        pinMode( A2, INPUT );
                        pinMode( 3, OUTPUT );
                        pinMode( 5, OUTPUT );
                        pinMode( 6, OUTPUT );
                      void loop()
                        bool OutputToSerial = false;
                        // Get current milli
                        currMillis = millis();
                        // Check if you should output to serial
                        if( ( currMillis - prevMillis ) < 0 )
                          // Choose how to handle a roll over
                        else if( ( currMillis - prevMillis ) >= numMillisToDelayForOutput )
                          // Set flag
                          OutputToSerial = true;
                        // Update prev time
                        prevMillis = currMillis;
                        int R = map( analogRead( A2 ), 0, 1023, 0, 255 );
                        int G = map( analogRead( A0 ), 0, 1023, 0, 255 );
                        int B = map( analogRead( A1 ), 0, 1023, 0, 255 );
                        // If you it's time to output to serial
                        if( OutputToSerial )
                          // Output results to serial
                          Serial.print( "R: " );
                          Serial.print( R );
                          Serial.print( "G: " );
                          Serial.print( G );
                          Serial.print( "B: " );
                          Serial.print( B );
                        analogWrite( 6, R );
                        analogWrite( 3, G );
                        analogWrite( 5, B );


                      Essentially, you're using the millis() function to check whether enough time has elapsed to determine whether you should output to serial.

                      • 8. Re: millis() ... help me

                        it seems not work, the program works but at the serial monitor is printed just the first value when i turn on the board and nothing else. If if move my potentiometer nothing happens even after 1 second or 1 minute.

                        • 9. Re: millis() ... help me

                          The idea in that code is correct, but there is a small bug.


                          Move line 33

                          prevMillis = currMillis;

                          to just before line 29


                          prevMillis = currMillis;

                          OutputToSerial = true

                          prevMillis should be the last time you printed out a line, not the last time the loop ran.

                          1 of 1 people found this helpful
                          • 10. Re: millis() ... help me

                            Yes!!! IT WORKS!!!!!!!!!!! thank you!!!! i have added a Sistem.println() too to make the data organized. i don't understand the code, but now i'll start to study line per line.