• AVR Freaks

Hot!Looking for code examples for externally programmable LED flash speed and mode scroll

Author
CptChronic
Starting Member
  • Total Posts : 49
  • Reward points : 0
  • Joined: 2016/11/20 20:58:44
  • Location: 0
  • Status: offline
2021/02/28 15:14:26 (permalink)
0

Looking for code examples for externally programmable LED flash speed and mode scroll

Are there any code examples or tutorials for an LED setup where you can change between modes via external button presses and also program the flash speed?
For example;
hold button A and tap button B to enter programming, while in programming, each press of button A would slow the led flash speed. Each press of button C would increase the flash speed. Once at the desired speed, press button B to exit programming and return to main function.
 
Hold button C, then each tap of button B would increase the mode by 1. The first tap would reset the mode number to 1, then each press after that would increase the mode number by 1.
 
I only know C so it would have to be in that language. Ive looked everywhere and cant find anything that fits
#1

8 Replies Related Threads

    Mysil
    Super Member
    • Total Posts : 4130
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: online
    Re: Looking for code examples for externally programmable LED flash speed and mode scroll 2021/02/28 15:54:03 (permalink)
    +2 (2)
    Hi,
    The devices are more correctly called Addressable LED, and have a trade name "NeoPixel".
    A current type is WS2812B .
    They may be obtained as single devices, or mounted on strips or in arrays.
    There is some tricky timing required to control color and intensity.
     
    Microchip have a Youtube video demonstrating use of the CLC peripheral in newer PIC18 microcontrollers,
    together with SPI for controlling such devices:
    https://www.youtube.com/watch?v=hHLMnJs87VE
     
        Mysil
     
    #2
    CptChronic
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2016/11/20 20:58:44
    • Location: 0
    • Status: offline
    Re: Looking for code examples for externally programmable LED flash speed and mode scroll 2021/03/02 04:16:02 (permalink)
    +1 (1)
    Thanks for the reply Mysil, I was thinking more for my own breadboard though. So I make a circuit with an led and 3 buttons, I have the code for the led flashing but I then want to be able to adjust the flash speed by using the buttons.

    I have written a code that uses a set number for the flash speed. It's making the flash speed adjustable that I'm struggling with. I've thought of having it as a state Machine so when I hold button A and tap button B, it would take me in to a programming state where I can then increase or decrease the value that defines how quickly the led flashes. Not sure this is the best way to do it though.

    Also with the modes, am I best to just have a load of if statements? For example:

    if (buttonC==0){ //if button C is held
    if (buttonB==0){ // and Button C is pressed
    modenumber=0; // reset the mode to 0
    modenumber++; // and add 1
    }}

    How would I code that so it didn't reset the mode to 0 with each press of button B?
    #3
    RISC
    Super Member
    • Total Posts : 6067
    • Reward points : 0
    • Status: offline
    Re: Looking for code examples for externally programmable LED flash speed and mode scroll 2021/03/02 06:36:35 (permalink)
    -1 (1)
    hi,
    there are hundreds of PICs...which one do you use ?
    regards
     

    For support make sure to check first here : http://microchipdeveloper.com
    There are hundreds of PIC, AVR, SAM...which one do YOU use ?
    #4
    CptChronic
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2016/11/20 20:58:44
    • Location: 0
    • Status: offline
    Re: Looking for code examples for externally programmable LED flash speed and mode scroll 2021/03/03 11:49:18 (permalink)
    +1 (1)
    RISC
    hi,
    there are hundreds of PICs...which one do you use ?
    regards
     


    Hello, Sorry yes that might help, Im using a pic16f18325
     
    Thanks
     
     
    #5
    ric
    Super Member
    • Total Posts : 30244
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Looking for code examples for externally programmable LED flash speed and mode scroll 2021/03/03 12:16:14 (permalink)
    -1 (1)
    What is your programming level?
    You don't appear to want to write any code yourself. You're not going to get anywhere if you just looked for code that has already been written by someone else.
     
    Also, I'm unsure when you say "externally programmable LED" if you really mean something like Neopixels, or if you just mean a plain LED controlled by the PIC.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    CptChronic
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2016/11/20 20:58:44
    • Location: 0
    • Status: offline
    Re: Looking for code examples for externally programmable LED flash speed and mode scroll 2021/03/03 12:54:28 (permalink)
    +1 (1)
    ric
    What is your programming level?
    You don't appear to want to write any code yourself. You're not going to get anywhere if you just looked for code that has already been written by someone else.
     
    Also, I'm unsure when you say "externally programmable LED" if you really mean something like Neopixels, or if you just mean a plain LED controlled by the PIC.




    I have been coding about 5 months now. I am working on a task and have been since i started coding. Ill hit a roadblock so have to come and ask for help, find a tutorial or some other way of solving it then im back to my code.
    Its not something i want to just be posting for everyone to see, i have worked hard on this and do not want to risk it being copied. I didnt ask for anyone to write me a code and of course, copying someone elses code would not work but it would give me an idea of how its done and i can then tailor it to what i need. You mean to tell me you have never had to look at an example before to see where you're going wrong?
     
    As ive said above, this is for a breadboard setup, not neopixel. Imagine i have a circuit of 3 switches, a pic16f18325 and one led on a breadboard, i turn the thing on and my led constantly flashes. I want to be able to use the buttons to enter a programming mode which would allow me to increase or decrease the flash speed. For example, hold button A and tap button B to enter programming, decrease spped with button A and increase the speed with button C, exit programming by holding button A and tapping button B.
    Then i want to be able to scroll through modes, for example, hold button C and tap button B 4x for mode 4.
     
    Im not expecting anyone to write the code for me but a bit of help would of been nice, this style of thing has surely been done before, there must be an example somewhere.
    #7
    HGO
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2020/11/10 13:26:57
    • Location: 0
    • Status: offline
    Re: Looking for code examples for externally programmable LED flash speed and mode scroll 2021/03/03 15:04:08 (permalink)
    +1 (1)
    What i denotes from your explanation, i think your design should simply be refer to as LED modes imbibed with flash speed, because it will only function base on the limit of your code, not that the end user will be able to externally programmed it to any form he/she wanted, but the modes will only be selected (or set at random) and the flashing speed will be vary base on the state of the externally interfered switches.
     
    I think timer module will best for varying the speed just by creating a counter variable which to count the number of times  switch A is being pressed while hold B, then use that for increase or decrease the number of the timer overflows to vary the flashing delays.  and for the modes just create functions for whatever modes u want, then check the state of switch A and B by polling or interrupt. 
    #8
    dan1138
    Super Member
    • Total Posts : 4324
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: Looking for code examples for externally programmable LED flash speed and mode scroll 2021/03/03 20:40:38 (permalink)
    -1 (3)
    CptChronic
    ric
    What is your programming level?

    You don't appear to want to write any code yourself. You're not going to get anywhere if you just looked for code that has already been written by someone else.

    Also, I'm unsure when you say "externally programmable LED" if you really mean something like Neopixels, or if you just mean a plain LED controlled by the PIC.

    I have been coding about 5 months now. I am working on a task and have been since I started coding. I'll hit a roadblock so have to come and ask for help, find a tutorial or some other way of solving it then I'm back to my code.

    It's not something I want to just be posting for everyone to see, I have worked hard on this and do not want to risk it being copied.

    I didn't ask for anyone to write me a code and of course, copying someone elses code would not work but it would give me an idea of how its done and I can then tailor it to what I need.

    You mean to tell me you have never had to look at an example before to see where you're going wrong?

    As I've said above, this is for a breadboard setup, not neopixel. Imagine I have a circuit of 3 switches, a pic16f18325 and one led on a breadboard, I turn the thing on and my led constantly flashes. I want to be able to use the buttons to enter a programming mode which would allow me to increase or decrease the flash speed. For example, hold button A and tap button B to enter programming, decrease speed with button A and increase the speed with button C, exit programming by holding button A and tapping button B.

    Then I want to be able to scroll through modes, for example, hold button C and tap button B 4x for mode 4.

    I'm not expecting anyone to write the code for me but a bit of help would of been nice, this style of thing has surely been done before, there must be an example somewhere.

    It's a good thing that you are "not expecting anyone to write the code for me" as what you have posted here will just about guarantee that.

    Should you want actual help I would suggest that you post some code of what you have done to validate that your hardware is assembled correctly.

    As a start, post a complete MPLABX project that will flash an LED on for one second then off for one second.

    For an embedded application this is the equivalent of the C language "Hello world" program.

    This is an important exercise to post here as it shows that your know how to use the tools and we can skip telling you where to find stuff in MPLABX.

    Posting code here is not easy to do. Microchip has made this forum very hard to use.

    If you can do this it shows in no uncertain terms that you are committed to learning how to code for Microchip controllers with the Microchip tools and you will not be deterred by Microchip stupidity. You will see a lot of that.

    In my opinion this would show you are worth spending time to help.

    <EDIT>
    You have been coding for 5 months, working on "a task" and hit a roadblock so would like to see coding examples.

    I spent 2 hours just poking around on the internet for PIC16F18345 examples and found these:

    PIC16F18345 Internet of Things (IoT) Sensor Demo
    MPLAB® Xpress PIC16F18345 Evaluation Board
    AN1606 Using the Configurable Logic Cell (CLC) to Interface a PIC16F1509 and WS2811 LED Driver
    MPLAB Xpress Code Examples
    Tutorial of 10 Labs with DM164137 LPC Curiosity - PIC16F18345
    Hardware Blink PIC16F18345
    Serial Monitor PIC16F18345
    Hello World PIC16F18345
    Timer ISR LED blink

    This seems like a lot of controller specific code examples.

    Do you know how to use internet search tools?
    post edited by dan1138 - 2021/03/04 14:09:05
    #9
    Jump to:
    © 2021 APG vNext Commercial Version 4.5