Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Using MikroBasic to make LED chaser effects

Status
Not open for further replies.
Sure. Here's a Charlieplexed 20-LED circuit with 32 duty cycle levels per LED.


The software PWM driver uses a 20 element array which contains duty cycles values of 0..31 for each LED. You stuff the array in 'main' with the values you want to animate the display.

Here's the code Futz came up with for the pattern shown in the video above (note that LED 0 is top left and LED 19 is bottom right). I had to use very low duty cycle values to keep from overloading the camera. The entire BoostC program listing can be found here.

Cheerful regards, Mike

Code:
//  simple interface to pwm driver using duty cycle values of 0..63

  led[0] = led[19] = 11;    // 11 * 0.3125% = 3.4% of 20% max
  led[1] = led[18] = 7;     //  7 * 0.3125% = 2.2%
  led[2] = led[17] = 5;     //  5 * 0.3125% = 1.5%
  led[3] = led[16] = 4;     //  4 * 0.3125% = 1.2%
  led[4] = led[15] = 3;     //  3 * 0.3125% = 0.9%
  led[5] = led[14] = 2;     //  2 * 0.3125% = 0.6%
  led[6] = led[13] = 1;     //  1 * 0.3125% = 0.3%
  led[7] = led[12] = 0;     //
  led[8] = led[11] = 0;     //
  led[9] = led[10] = 0;     //

  while(1)
  {                         // simple animation demo
    temp = led[0];          //
    while(!last_column);    // sync' to 16.25 msec frame rate
    for(x=0; x<19; x++)     //
    { led[x] = led[x+1];    //
    }                       //
    led[19] = temp;         //
    delay_ms(26);           //
  }
}

Hi Mike,

Thanks for the code but what language is it?

I only have experience of MikroBasic and in your code I don't see anything resembling the Array command that I have?

Also what is the X++ meaning?

Al
 
Last edited by a moderator:
I just got done doing a Excel chart for my leds for the chaser

Hi Burt,

I would be interested to see the Excel chart you made. It may help get my head around how the sequence of these work.

BTW have you got the chaser bug now, bet yours will be something out of the ordinary!

Thanks Al
 
Hi Al,

The code is written in source boost C, and the X++ means increment X, a bit like X = X + 1 in Basic.

It's just C/C++ has the ++ operator to increment the variable instead of having to do it like in Basic, it also has the -- and some others like += -= which would for example:
Code:
X = 1;
X++;  //X = 2
X+=4; 2(Original X after ++) + 4 = 6

So X Plus Equals 4. It really is simple once you get into it.

The main differences between Basic and C is the use of the terminator ( ; ) you have to put a semicolon after each line to tell it that is the end of the command.
The curly braces {} which tells the compiler that this is the function for a particular statement, such as an IF statement
In basic you would do:
Code:
if a = b then
   a = a + 1
end if
in C/C++ you would do this:
Code:
if(a == b)
{
    a++;
}
'=' in C means assign, == means compare, != means not equals. Because the code for the if statement is inside curly braces you dont need an "end if" statement, the '}' is effectively the "end if" and the a++; is already covered, you will also notice the if clause is in ( ) brackets, this is another small change.

The last real one is the way C/C++ implements the varibles, there is no DIM keyword, and they are case sensitive.

For example,

Code:
In Basic:
DIM I as Integer
DIM A as String
i = 1
a = "Test"

The above example all would be happy

In C/C++:

int i;
string a;
I = 1;
a = "1234";

Now, in the above example, 'I' would be invalid, instead we have to use:
i = 1;

My examples are not perfect, but they will hopefully allow you to understand how to either:
a) move to C/C++ OR
b) convert code from C/C++ into BASIC.

Functions are also done in the same fashion, with curly braces to indicate the start and end of a function and a declaration.

You will sometimes notice what is called a "Function Prototype" which in simple terms tells the compiler what is there before you implement the function.

For example ( note VOID in C/C++ means it has nothing to return):
Code:
void PrintStr(string str);  //Notice the ; ? This means it is allocating that function and that it will be created somewhere later on in the program.

int main() //Entry point
{
       //Blah blah do something
      PrintStr("Hello World"); //See, we have used the function BEFORE it has been created, but it has been allocated.
      return 0;   //Return 0 (exit code as in main function)
}

void PrintStr(string str)
{
     printf("%s\r\n", str);
}

Now without going into details of C functions etc, hopefully this will help you to work out what is going on. If you attempted to call the function without actually declaring it properly you would get a compile error.

Regards

Wilksey
 
Last edited:
Hi again guys,

Thanks Wilksey for the small C lesson, it has helped but I don't think I'll ever understand anything other than basic. :eek:

However I am getting very interested in charlieplexing. In Mikes original schematic for the chaser I am wondering if I could add some more transistors on the Cathode side of the LEDs to be able to use higher power strings (12v to run blocks of three white ones).

What I am wondering is would it be better to use PNPs or use NPNs and invert the signal to their gates? I don't fully understand the charlieplexing so I didn't want to damage anything.

Al
 
Ahh, haven't managed to convert you to C then? lol
If you look at Wikipedia at Charlieplexing you will see it uses TriState (3 States, output high, output low, input (high impedence) ).

There is probably a formula you can use to calculate the states and the LED's etc, I am not an expert, so I will hand this one back to Mike!

Regards

Wilksey
 
... I am getting very interested in charlieplexing. In Mikes original schematic for the chaser I am wondering if I could add some more transistors on the Cathode side of the LEDs to be able to use higher power strings (12v to run blocks of three white ones). ...

Sorry Al. You can't do that with Charlieplexing.

If you want to drive a string of LEDs at a higher voltage then you might consider using one of the many nice 8-bit or 16-bit serial to parallel sinking or sourcing driver ICs. You could also use an 8-bit parallel sinking driver IC like the ULN2803.

Cheerful regards, Mike
 
Sorry Al. You can't do that with Charlieplexing.

If you want to drive a string of LEDs at a higher voltage then you might consider using one of the many nice 8-bit or 16-bit serial to parallel sinking or sourcing driver ICs. You could also use an 8-bit parallel sinking driver IC like the ULN2803.

Cheerful regards, Mike

Hi Mike,

How do you mean to use the ULN2803? Do you mean steer clear of charlieplexing altogether or are you saying the C-Plex is possible WITH the 2803?

I am out of my comfort zone with most of the aspects of this project! I just wish there were more examples of PWM and chasing written in MikroBasic.

I only seem to be able to learn from examples nowadays, it seems every year older I get the harder it is to take things in. :(

Al
 
Maybe we should back up a second? I think I may have missed something here.

Originally we were talking about 32 individually controlled LEDs, weren't we? Why are you talking about strings of 3 LEDs now? Is this for a different project perhaps?

Anyway, you can't use a ULN2803 in a Charlieplexed array. I just mentioned it as one way to drive a string of series LEDs.

Regards, Mike
 
Last edited:
Maybe we should back up a second? I think I may have missed something here.

Originally we were talking about 32 individually controlled LEDs, weren't we? Why are you talking about strings of 3 LEDs now? Is this for a different project perhaps?

Anyway, you can't use a ULN2803 in a Charlieplexed array. I just mentioned it as one way to drive a string of series LEDs.

Regards, Mike

Hi Mike,

Well after more research I have finally started understanding that C-Plex is not for my project.

As to the project it is all for the same project. I am trying to build a very large circle of PWM controlled LEDs and just to get the most LEDs I can in the circle I wanted to use 3 LEDs and the appropriate resistor for EACH segment! It just means I can make the circle phisically biggerand still maintain the effect.

I would be happy enough with a 16 pin PWM set up and could then mirror the LEDs and using 3 per segment would be brilliant (pun intended), imagine 96 leds in a circle, that would be about 300mm circumference even if I used 3mm ones.

I just really would love to get this project going but there is nowhere near enough examples in MikroBasik to give me any idea how it would be done.

Thanks.........Al
 
Thanks to all! Finally got a working prototype :)

Hi to all,

At last I have a working prototype of my long imagined idea.

Thanks go to all who contributed and helped me in this.

Many many thanks to Darrel Taylor for his MIBAM project which I am using for the basis of this. Cheers Darrel and keep up the excellent PicBasic Pro work. MIBAM is genius!

Below is the zipped video (.MOV for Quicktime) of my prototype, only half the circle of LEDs are in at the moment and I'm driving 20 sets of 3 LEDs. The other half will be a mirror image of the first half so 120 3mm LEDs in all. I just couldn't wait to see it in action and fitting the leds takes a dogs age!

Sorry about the interference on the screen but the LEDs are so bright that they are overloading the camera I think.

If anyone is interested in the circuit I will post the drawing I did on VeroDes, but its pretty simple anyway, just a PIC and some ULN2803s.

Thanks again..........Al
 

Attachments

  • P1030084.zip
    16 MB · Views: 234
Hi Guys,

Just an update.

Not had a lot of time to build lately but I did find myself thinking of something.

We discussed charlieplexing and determined that it was not possible to uprate the LEDs as in power or number of them per port, but I was wondering theoretically could you use opto isolators instead of the LEDs in a charlieplexed display( as they are leds internally) and the use them to drive any number of LEDs like I was at first wanting to?

Would that work?

Al
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top