element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum How to Sequentially Activate Pins while Reading from a Sensor ?
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 8 replies
  • Subscribers 392 subscribers
  • Views 561 views
  • Users 0 members are here
Related

How to Sequentially Activate Pins while Reading from a Sensor ?

aureta
aureta over 10 years ago

Hi, I am starting with Arduino and I am in need of some help. I am trying to sequentially activate 5 pins. I need each pin to be active for 20 seconds in a PWM manner. During their active period I want an arduino sensor to constantly read out the signals emmited by each pin effector. So far I have no problems with the PWM or sensing module code. However I am not able to find how to correctly sequentially activate the pins. I used millis(), etc, but so far I was able to only activate / read the signals from the first pin effector. The sketch doesn´t proceed to the 2nd --- pins. I really very much appreciate your help ....

  • Sign in to reply
  • Cancel

Top Replies

  • dtsartsanis
    dtsartsanis over 10 years ago +1
    If you post your code someone will help you
  • balearicdynamics
    balearicdynamics over 10 years ago in reply to aureta +1
    Hello, your mistake is in the use of millis() With this function you get the number of milliseconds occured from the last power-on of the microcontroller . So you should not put while(millis()<5000) because…
Parents
  • dtsartsanis
    0 dtsartsanis over 10 years ago

    If you post your code someone will help youimage

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • dtsartsanis
    0 dtsartsanis over 10 years ago

    If you post your code someone will help youimage

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • aureta
    0 aureta over 10 years ago in reply to dtsartsanis

    Hi Dimitros, here´s the code so far...




    const int greenPin = 11;

    const int redPin = 10;

    const int irPin = 5;

    const int bluePin = 6;

    const int yellowPin = 9;

    const int outputLDR = 12;

     

    const int ldrSensor = A0;

     

    int brightness = 0;

    int increment = 1;

     

    int readingoutputLDR = 0;

     

     

     

    void setup()

    {

      pinMode(greenPin, OUTPUT);

      pinMode(redPin, OUTPUT);

      pinMode(irPin, OUTPUT);

      pinMode(bluePin, OUTPUT);

      pinMode(yellowPin, OUTPUT);

      Serial.begin(9600);

     

      setup_parallax();

    }

     

    void loop()

    {

     

    digitalWrite(outputLDR, HIGH);

    readingoutputLDR = analogRead(ldrSensor);

     

    while (millis()<5000)

                    {                   

                       if(brightness > 255)

                       {

                        increment = -1; // count down after reaching 255

                       }

                       else if(brightness < 1)

                       {

                       increment = 1; // count up after dropping back down to 0

                       }

                       brightness = brightness + increment;                                    

                       analogWrite(greenPin, brightness); digitalWrite(redPin, LOW); digitalWrite(irPin, LOW);

                       digitalWrite(bluePin, LOW); digitalWrite(yellowPin, LOW);

                       Serial.print("Reading Green"); parallax_output();                   

                     }

     

    delay(1);

     

    while (millis()<5000)

                    {                   

                       if(brightness > 255)

                       {

                        increment = -1; // count down after reaching 255

                       }

                       else if(brightness < 1)

                       {

                       increment = 1; // count up after dropping back down to 0

                       }

                       brightness = brightness + increment;

                       delay(1);

     

                       digitalWrite(greenPin, LOW); analogWrite(redPin, brightness); digitalWrite(irPin, LOW);

                       digitalWrite(bluePin, LOW); digitalWrite(yellowPin, LOW);

                      Serial.print("Reading Red"); parallax_output();                 

                    }

     

     

    while (millis()<5000)

                    {                   

                       if(brightness > 255)

                       {

                        increment = -1; // count down after reaching 255

                       }

                       else if(brightness < 1)

                       {

                       increment = 1; // count up after dropping back down to 0

                       }

                       brightness = brightness + increment;

                       delay(1);

     

                       digitalWrite(greenPin, LOW); digitalWrite(redPin, LOW); analogWrite(irPin, brightness);

                       digitalWrite(bluePin, LOW); digitalWrite(yellowPin, LOW);

                        Serial.print("Reading IR"); parallax_output();                 

                    }

     

     

    while (millis()<5000)

                    {                   

                       if(brightness > 255)

                       {

                        increment = -1; // count down after reaching 255

                       }

                       else if(brightness < 1)

                       {

                       increment = 1; // count up after dropping back down to 0

                       }

                       brightness = brightness + increment;

                       delay(1);

     

                       digitalWrite(greenPin, LOW); digitalWrite(redPin, LOW); digitalWrite(irPin, LOW);

                       analogWrite(bluePin, brightness); digitalWrite(yellowPin, LOW);

                       Serial.print("Reading Blue"); parallax_output();                 

                    }

     

     

    while (millis()<5000)

                    {                   

                       if(brightness > 255)

                       {

                        increment = -1; // count down after reaching 255

                       }

                       else if(brightness < 1)

                       {

                       increment = 1; // count up after dropping back down to 0

                       }

                       brightness = brightness + increment;

                       delay(1);

     

                       digitalWrite(greenPin, LOW); digitalWrite(redPin, LOW); digitalWrite(irPin, LOW); digitalWrite(bluePin, LOW);                   analogWrite(yellowPin, brightness);

                      Serial.print("Reading Yellow"); parallax_output();                 

                    }

     

     

     

    }

     

     

    void parallax_output()

    {

     

                       Serial.print("DATA,DATE,TIME,");

                                               

                       Serial.println(readingoutputLDR);

                      

    }

     

     

     

    void setup_parallax()

    {

        Serial.println("CLEARDATA");

        Serial.println("LABEL,Date,Time,Green Response, Red Response, IR Response, Blue Response, Yellow Response");

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • balearicdynamics
    0 balearicdynamics over 10 years ago in reply to aureta

    Hello,

     

    your mistake is in the use of millis() With this function you get the number of milliseconds occured from the last power-on of the microcontroller. So you should not put while(millis()<5000) because it occurs only one time, before the first 5 seconds the program run! Try something like this

     

    [...]

    int myTime = 0; // The time delay counter

    int eventCounter = 0; // The event counter you need


    void setup() {

    // your stuff

    myTime = millis(); // init the time counter

    eventCounter = 1; // start first event

    }

     

    void loop() {

     

    // Check if 5 seconds has passed

    if( (millis() - myTime) > 5000) ) {

         // Process the current event

         switch(eventCounter) {

              case 1: // event 1

                   // your stuff for event 1

                  eventCounter++; // next event ...

                  myTime = millis(); // reset the time counter

              break;

              case 2: // event 2

                   // your stuff for event 2

                  eventCounter++; // next event ...

                  myTime = millis(); // reset the time counter

              break;

              case 3: // event 3

                   // your stuff for event 3

                  eventCounter++; // next event ...

                  myTime = millis(); // reset the time counter

              break;

             

              // And so on... for all the events you need to manage in sequence every 5 seconds

              // Then, the last event, as you should restart to the first event

     

              case 5: // event 5, the last

                   // your stuff for event 5

                   eventCounter = 1; // next time restart to the first event

                   myTime = millis(); // reset the time counter

              break;

         } // end event processing

    } // every 5 seconds

     

    }

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • aureta
    0 aureta over 10 years ago in reply to balearicdynamics

    Hi Enrico, thanks a lot for your help. I modified the code but seems there are compilation problems that I can´t see. I am att below the modified code, I will appreciate very much if you can take a look at it, Alex.

     

    const int greenPin = 11;

    const int redPin = 10;

    const int irPin = 5;

    const int bluePin = 6;

    const int yellowPin = 9;

    const int outputLDR = 12;

     

    const int ldrSensor = A0;

     

    int brightness = 0;

    int increment = 1;

     

    int readingoutputLDR = 0;

     

    int myTime = 0; // The time delay counter

    int eventCounter = 0; // The event counter you need

     

    void setup()

    {

      pinMode(greenPin, OUTPUT);

      pinMode(redPin, OUTPUT);

      pinMode(irPin, OUTPUT);

      pinMode(bluePin, OUTPUT);

      pinMode(yellowPin, OUTPUT);

      Serial.begin(9600);

      myTime = millis(); // init the time counter

    eventCounter = 1; // start first event

      setup_parallax();

    }

     

    void loop()

     

     

    {

     

    // Check if 5 seconds has passed

    if ((millis()- myTime) > 5000)) 

    {

         // Process the current event

                       switch(eventCounter)

                       {

                              case 1: // event 1

                                     // your stuff for event 1

                                     if(brightness > 255)

                                      {

                                       increment = -1; // count down after reaching 255

                                      }

                                      else if(brightness < 1)

                                      {

                                       increment = 1; // count up after dropping back down to 0

                                      }

                                       brightness = brightness + increment;                                   

                                      analogWrite(greenPin, brightness); digitalWrite(redPin, LOW); digitalWrite(irPin, LOW);

                                      digitalWrite(bluePin, LOW); digitalWrite(yellowPin, LOW);

                                      Serial.print("Reading Green"); parallax_output();                  

                      

                   eventCounter++; // next event ...

                   myTime = millis(); // reset the time counter

                   break;

             

             

             

                              case 2: // event 2

                                      // your stuff for event 2

                                     if(brightness > 255)

                                        {

                                         increment = -1; // count down after reaching 255

                                        }

                                         else if(brightness < 1)

                                        {

                                         increment = 1; // count up after dropping back down to 0

                                        }

                                        brightness = brightness + increment;                                    

                                         digitalWrite(greenPin, LOW); analogWrite(redPin, brightness); digitalWrite(irPin, LOW);

                                         digitalWrite(bluePin, LOW); digitalWrite(yellowPin, LOW);

                                         Serial.print("Reading Red"); parallax_output(); 

                    eventCounter++; // next event ...

                    myTime = millis(); // reset the time counter

                    break;

            

            

              case 3: // event 3

                   // your stuff for event 3

                   if(brightness > 255)

                       {

                        increment = -1; // count down after reaching 255

                       }

                       else if (brightness < 1)

                       {

                       increment = 1; // count up after dropping back down to 0

                       }

                       brightness = brightness + increment;

                   

     

                       digitalWrite(greenPin, LOW); digitalWrite(redPin, LOW); analogWrite(irPin, brightness);

                       digitalWrite(bluePin, LOW); digitalWrite(yellowPin, LOW);

                        Serial.print("Reading IR"); parallax_output(); 

                  eventCounter++; // next event ...

                  myTime = millis(); // reset the time counter

              break;

            

             

              case 4: // event 4

                   // your stuff for event 4

                   if(brightness > 255)

                       {

                        increment = -1; // count down after reaching 255

                       }

                       else if(brightness < 1)

                       {

                       increment = 1; // count up after dropping back down to 0

                       }

                       brightness = brightness + increment;

                      

     

                       digitalWrite(greenPin, LOW); digitalWrite(redPin, LOW); digitalWrite(irPin, LOW);

                       analogWrite(bluePin, brightness); digitalWrite(yellowPin, LOW);

                       Serial.print("Reading Blue"); parallax_output(); 

                  eventCounter++; // next event ...

                  myTime = millis(); // reset the time counter

              break;

             

             

            

             

              // And so on... for all the events you need to manage in sequence every 5 seconds

              // Then, the last event, as you should restart to the first event

     

              case 5: // event 5, the last

                   // your stuff for event 5

                    if(brightness > 255)

                       {

                        increment = -1; // count down after reaching 255

                       }

                       else if(brightness < 1)

                       {

                       increment = 1; // count up after dropping back down to 0

                       }

                       brightness = brightness + increment;

                   

     

                       digitalWrite(greenPin, LOW); digitalWrite(redPin, LOW); digitalWrite(irPin, LOW); digitalWrite(bluePin, LOW);    

                       analogWrite(yellowPin, brightness); Serial.print("Reading Yellow"); parallax_output();   

                  

                  

                   eventCounter = 1; // next time restart to the first event

                   myTime = millis(); // reset the time counter

              break;

         } // end event processing

    } // every 5 seconds

    }

     

     

     

     

    void parallax_output()

    {

     

                       Serial.print("DATA,DATE,TIME,");

                                              

                       Serial.println(readingoutputLDR);

                     

    }

     

     

     

    void setup_parallax()

    {

        Serial.println("CLEARDATA");

        Serial.println("LABEL,Date,Time,Green Response, Red Response, IR Response, Blue Response, Yellow Response");

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • balearicdynamics
    0 balearicdynamics over 10 years ago in reply to aureta

    Line 37 - removed an extra ')'

    Correct as follows:

    if ( (millis()- myTime) > 5000)

     

     

    That's all. Then compile.

     

    Regards.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • aureta
    0 aureta over 10 years ago in reply to balearicdynamics

    Right, LEDs are glowing. There´s a problem though, my intention is that each pin or led should be continuosly active for at least five seconds in a PWM manner. During this time I want to read the effect on a LDR, I need LDR response curves to LED lights. In this modified sketch, the pins are sequentially activated for 5 sec but the PWM increase is seen only after successive cycles (next activation step for each LED). Seems that it goes all  through the code before changing LED intensity levels. ..... If you can help me with this (I really need it) I will be extremely happy.  Alex

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • aureta
    0 aureta over 10 years ago in reply to aureta

    I´ve got it working, i kept the original while millis() statements but included windows of time for during which each pin should be activated. ....

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube