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 5v pulse input to run code
  • 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 9 replies
  • Subscribers 387 subscribers
  • Views 1940 views
  • Users 0 members are here
Related

5v pulse input to run code

Phrostedflakes
Phrostedflakes over 3 years ago

I have a basic car alarm and i want to replace the blinking led with a attiny85 (or something similar) and a neopixel strip to run when the alarm is active. But the power to the factory led is not constant. It is a pulsed 5v, (5v hot for a couple seconds, then no power, then repeat)   Is it possible to still use the pulsed voltage to trigger the attiny85 to run my code when the alarm is active and stop it when the alarm is deactivated? This is the code I wish to run

#include <Adafruit_NeoPixel.h>
#define PIN D1
#define NUMPIXELS 11
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delay_time = 125; // 200 msec = 1/5th of a second

void setup() {
  pixels.begin();
}

void loop() {

  // from left to right
  for(int i=0; i<NUMPIXELS; i++){
    pixels.setPixelColor(i, 0, 0, 255);
    pixels.show();
    delay(delay_time);
    pixels.setPixelColor(i, 0, 0, 0);
    pixels.show();
  }

  // from right to left
  for(int i=NUMPIXELS; i>0; i--){
    pixels.setPixelColor(i, 0, 0, 255);
    pixels.show();
    delay(delay_time);
    pixels.setPixelColor(i, 0, 0, 0);
    pixels.show();
  }


}

  • Sign in to reply
  • Cancel

Top Replies

  • kmikemoo
    kmikemoo over 3 years ago in reply to Phrostedflakes +2
    Phrostedflakes The teacher in me is just screaming right now; "Teach them to fish!" You've already altered other sketches, so we're heading in the right direction. Your current loop [ void loop() ] can…
  • robogary
    robogary over 3 years ago +1
    Does the flashing LED change flash rate on an alarm ? Maybe count the rising edge of the 5V LED pulses ? If they happen every 2-3 seconds then life is good. Check the LED pulsing for what is expected in…
  • beacon_dave
    beacon_dave over 3 years ago in reply to kmikemoo +1
    kmikemoo said: I personally like the second one better, but I am the exception to the rule. When starting out, the second option may tend to be more readable if you have nesting going on as all the braces…
  • robogary
    robogary over 3 years ago

    Does the flashing LED change flash rate on an alarm ? Maybe count the rising edge of the 5V LED pulses ? If they happen every 2-3 seconds then life is good. Check the LED pulsing for what is expected in the alarm state ? 

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • kmikemoo
    kmikemoo over 3 years ago

    I think this really depends on how you plan to power the ATtiny.  If you plan to power it continuously from the car battery, you could try using a millis() timer to create a pseudo watchdog arrangement.  This isn't a fully developed thought, but below is my pseudo code for what I'm recommending.  Set the delay for slightly longer than the normal blink off time.  So it might not be "to the second" accurate, but it will correct on the next flashing neopixel cycle.

    while (sensePin==HIGH) {   // this would be the input from the alarm LED
      timer = millis();        // keep running while the LED is on
      pixel();                 // run subroutine (your current loop)
      }
      
    now = millis();            // get current time
    while (now < (timer + delay)) {        // keep running during blink pause
      pixel();
      }
     

    Back to work. Laughing

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Phrostedflakes
    Phrostedflakes over 3 years ago in reply to kmikemoo

    kmikemoo, i do plan of powering the attiny from the car battery. How to i incorporate your sketch into mine?  I have no idea how to write my own or how to combine 2 sketches. I usually just find one that does what I need and the alter it to fit my project.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Phrostedflakes
    Phrostedflakes over 3 years ago in reply to robogary

    Yes the led flashes about every 2-3 seconds. I run a meter on the led wires and when it's live it is around 5 volts, then power shuts off, then live again, and so on.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • kmikemoo
    kmikemoo over 3 years ago in reply to Phrostedflakes

    Phrostedflakes  The teacher in me is just screaming right now; "Teach them to fish!"

    You've already altered other sketches, so we're heading in the right direction.  Your current loop [ void loop() ] can be turned into a subroutine.
    https://create.arduino.cc/projecthub/innovech/lesson-8-subroutines-466bb2
    Subroutines are modular segments of code.  If you need to do the same thing from more than one place in your process flow, why write the same code twice (or more)?  Just re-run that segment.  It also really helps me try to simplify what I'm doing.  Besides, we know that segment works.

    void pixel() {
      < put the contents of your current loop here >
      }

    This is the same as 
    void pixel()
      {
      < put the contents of your current loop here >
      }

    I personally like the second one better, but I am the exception to the rule.

    On an Arduino, you should be able to put the code snippet above into the now empty void loop().

    Go back and declare the variables.
    sensePin is the input from the the flashing LED.  Pick an input.  Declare it as an INPUT.  Tell the Arduino to set it LOW to start.  There is a chance that a digital pin might not get the job done.  You can alway shift to analog mode.
    https://learn.sparkfun.com/tutorials/tiny-avr-programmer-hookup-guide/attiny85-use-hints

    For timer and now, I'd make those unsigned long.

    Sparkfun has an awesome ATtiny85 reference guide.  https://cdn.sparkfun.com/assets/2/8/b/a/a/Tiny_QuickRef_v2_2.pdf

    Hopefully, I've given you enough to move forward.  As makerkaren would say, Happy Learning.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • phoenixcomm
    phoenixcomm over 3 years ago in reply to kmikemoo

    mike I like the first one, but with a small change.

    void pixel ( ) {

    // loop goes here

    }

    the change that I have added 3 spaces for readability.

    and most of the time the closing } is on the end of the last line. 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Jan Cumps
    Jan Cumps over 3 years ago in reply to kmikemoo
    kmikemoo said:
    I personally like the second one better, but I am the exception to the rule.

    haha yes. This leads to hot debates in dev teams and on internets.

    I have a preference, but if I work for a team that has a standard (or habit), I stick to that. I usually also create a style for the rules in my IDE and let it format my source code before a commit.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • BigG
    BigG over 3 years ago in reply to kmikemoo

    Did not know that the (preferable) 2nd option is the exception to the rule.

    Of course I like to over complicate, so I use the 2nd option for functions but the first for my if and while loops within etc.

    E.g.

    void pixel()
     {

           if (like-complicated) {
               < put the contents of your current loop here >

          }

      }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • beacon_dave
    beacon_dave over 3 years ago in reply to kmikemoo
    kmikemoo said:
    I personally like the second one better, but I am the exception to the rule.

    When starting out, the second option may tend to be more readable if you have nesting going on as all the braces line up vertically, but it is often at the expense of it taking up more lines on the screen.

    It may encourage the overuse of nesting, however.

    Similarly with indentation, it perhaps helps with readability initially but once your text starts to go off the side of the screen...

    Ideally though, with a free-from language, the development environment should perhaps allow you to pick your layout preference avoiding the need for all the disagreements over layout style.

    • Cancel
    • Vote Up +1 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