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 inverse signal by timer millis
  • 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
  • Replies 6 replies
  • Subscribers 389 subscribers
  • Views 1500 views
  • Users 0 members are here
Related

inverse signal by timer millis

balexfox
balexfox over 4 years ago

i need a help i cant create single inverse signal by millis. my code cyclical delay but i need single by millis

please who can help to show me example

image

  • Sign in to reply
  • Cancel

Top Replies

  • balexfox
    balexfox over 4 years ago +1
    i need a two single also needs a shift for the second signal int ledPin1 = 12 ; // the number of the LED pin int ledState1 = LOW; // ledState used to set the LED unsigned long previousMillis1 = 0 ; //…
  • balexfox
    balexfox over 4 years ago

    i need a two single also needs a shift for the second signal

     

    1. int ledPin1 =  12;  // the number of the LED pin
    2. int ledState1 = LOW;  // ledState used to set the LED
    3. unsignedlong previousMillis1 = 0;  // will store last time LED was updated
    4. longOnTime1 = 250;  // milliseconds of on-time
    5. longOffTime1 = 750;  // milliseconds of off-time
    6. int ledPin2 =  13;  // the number of the LED pin
    7. int ledState2 = LOW;  // ledState used to set the LED
    8. unsignedlong previousMillis2 = 0;  // will store last time LED was updated
    9. longOnTime2 = 330;  // milliseconds of on-time
    10. longOffTime2 = 400;  // milliseconds of off-time
    11. void setup()
    12. {
    13. // set the digital pin as output:
    14.   pinMode(ledPin1, OUTPUT); 
    15.   pinMode(ledPin2, OUTPUT); 
    16. }
    17. void loop()
    18. {
    19. // check to see if it's time to change the state of the LED
    20. unsignedlong currentMillis = millis();
    21. if((ledState1 == HIGH) && (currentMillis - previousMillis1 >= OnTime1))
    22.   {
    23.   ledState1 = LOW;  // Turn it off
    24.   previousMillis1 = currentMillis;  // Remember the time
    25.   digitalWrite(ledPin1, ledState1);  // Update the actual LED
    26.   }
    27. elseif ((ledState1 == LOW) && (currentMillis - previousMillis1 >= OffTime1))
    28.   {
    29.   ledState1 = HIGH;  // turn it on
    30.   previousMillis1 = currentMillis;  // Remember the time
    31.   digitalWrite(ledPin1, ledState1);  // Update the actual LED
    32.   }
    33. if((ledState2 == HIGH) && (currentMillis - previousMillis2 >= OnTime2))
    34.   {
    35.   ledState2 = LOW;  // Turn it off
    36.   previousMillis2 = currentMillis;  // Remember the time
    37.   digitalWrite(ledPin2, ledState2);  // Update the actual LED
    38.   }
    39. elseif ((ledState2 == LOW) && (currentMillis - previousMillis2 >= OffTime2))
    40.   {
    41.   ledState2 = HIGH;  // turn it on
    42.   previousMillis2 = currentMillis;  // Remember the time
    43.   digitalWrite(ledPin2, ledState2);  // Update the actual LED
    44.   }
    45. }
    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • balexfox
    balexfox over 4 years ago

    thank you my code))

    I wanted a signal to be executed on a single press. but if pressing the button falls into the timing, then the signal is triggered accordingly if you wait a long time and press the button nothing happens))) how can I fix unsigned long previousMillis = 0;

    thanks a lot for the code

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • balexfox
    balexfox over 4 years ago

    #define load_button 9

     

     

    const int ledPin =  11;

    const int ledPin2 =  12;

     

     

    // Variables will change:

    int ledState = LOW;             // ledState used to set the LED

    int ledState2 = LOW;

     

     

    // Generally, you should use "unsigned long" for variables that hold time

    // The value will quickly become too large for an int to store

           // will store last time LED was updated

     

     

    unsigned long previousMillis = 0;

     

     

    void setup() {

      // set the digital pin as output:

      pinMode(ledPin, OUTPUT);

      pinMode(ledPin2, OUTPUT);

      pinMode(load_button, INPUT_PULLUP);

    }

     

     

    void signal(){

      unsigned long currentMillis = millis();

     

     

      if (currentMillis - previousMillis >= 0 && currentMillis - previousMillis <= 1000) {

        digitalWrite(ledPin,HIGH);

         }

      else{

        digitalWrite(ledPin,LOW);

        }

     

     

      if (currentMillis - previousMillis >= 400 && currentMillis - previousMillis <= 5000) {

        digitalWrite(ledPin2,HIGH);

         }

      else{

        digitalWrite(ledPin2,LOW);

        } 

      

    }

     

     

    void loop() {

    if (digitalRead(load_button) == LOW) {

        signal();

      }

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • balexfox
    balexfox over 4 years ago

    sorry i cant correctly input this code

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • balexfox
    balexfox over 4 years ago in reply to balexfox

    its a repeat cycle of pwm i need a single

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Andrew J
    Andrew J over 4 years ago

    You need to set previousMillis to currentMillis at the end of signal() otherwise it is always zero and after 5 seconds neither of your ‘if’ checks will return true.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • 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