C Help

I'm working on an electronic project for school, and I've been trying to learn C, I have a quick question, I think the answer is a function, but I don't know and I need an example. Basically I want something where I can break up the code into sections (I'm pretty sure it's called a function) and bring them all back together in the main() for example, lets say I want to have a function to turn on an led, then I have a different function to wait for so long, and then another function to turn it off, except I can control what LED I want to turn on/off and for how long (I want to be able to input to the function) Once again, I'm pretty sure it's called a function, I just don't know how to use them.

sort by: active | newest | oldest
1-10 of 16Next »
guyfrom7up (author) 9 years ago
okay, still in C, I'm also wondering if you can do matrixes in C. I'm not sure how I would do this, but I'm sure it would be much simpler in C. To light up an individual led an individual color i want the function to be LEDON(int x,int y,int z, int color), with a 5x5x5 cube, this leads to 375 possibilities, and if I were to just use while or for loops, well it'd take up a huge amount of space, so is there a way of shrinking this down? for example: LEDON(5,2,3,R) would refer to turning on the LED at location 5 (on the x-axis), 2 (on the y-axis), and 3 (on the z-axis) to the color red. or is there no way a matrix would help this?
-- Yes, you can use "subscripted arrays" in C (and you're not limited to just three dimensions, either.)
int ledmatrix[5][5][5];
There are several points you should consider, however:

-- 375 options indicates you have three color choices (I assume R, G, B.) However, that's 375 bits, or about 47 bytes. But RGB LEDs can use PWM to achieve much more subtle color variations. So, you might also be planning to use 8 bits / color, that's 3 bytes per LED, for a total count of ...375 bytes.

Frankly, if you weren't planning on RGB LEDs, you wouldn't need a memory array at all -- the I/O bits themselves have an on/off state, and would be the memory. But If you're pulsing a cube, either for RGB or for charlieplexing, you will need a separate memory array (the I/O bits will be constantly changing, and cannot hold the current state.)

-- Obviously (if you do use arrays, and you are using 8-bits / color) a 32-bit integer per LED wastes 8 bits, but using bytes breaks the "one-array-element-per-LED" concept that makes arrays so attractive. But you could get around that with a structure:
struct LED {unsigned char red;unsigned char green;unsigned char blue;};
And an array of that structure:
struct LED ledmatrix[5][5][5];
Such a structure would be exactly three bytes in size, and the array 375 bytes... You don't need LEDON() or LEDOFF() functions, just LEDCOLOR() -- the color values will include everything from full-on RGB (white) to full-off RGB (no light emitted at all....)

-- Don't get too attached to the matrix / array concept. One way or another, you'll have to algorithmically convert the matrix values to the actual LED addressing schema.

Store the state of each LED element in some order that makes sense in regard to the LED hardware addressing. Do a little research on how computer screen pixels are memory-mapped, and how each pixel is algorithmically addressed....
guyfrom7up (author)  gmoon9 years ago
ok, so, underneither the led matrix would I write something like this? struct ledmatrix[5][5][1][3] //then something like? while ({ {0, 0, 0, 0, 1}, {0, 0, 0, 0, 1}, {1}, {R} }); then led code here? I don't know, I'm pretty lost
Yeah, there's a learning curve here....

First, here's your code snippet (unmangled):
struct ledmatrix[5][5][1][3]//then something like?while ({  {0, 0, 0, 0, 1}, {0, 0, 0, 0, 1}, {1}, {R} });then led code here?

First, about the above code.

-- There's no point in declaring one of those array subscripts with a single dimension (the third subscript is 1 -- that's essentially a "unity dimension"; a dimension of one element..) The bracket would always contain the same value and could never be changed, so why include it?

-- This array declaration includes the struct keyword, a name for the array (ledmatrix) and it's dimensional makeup, but no datatype...
struct ledmatrix[5][5][1][3];

The struct keyword needs to be followed by the structure type (for example, the previously declared "LED" struct), or if the datatype isn't a structure, then a defined standard type by itself will do:
struct LED ledmatrix[5][5][5];
or a standard type (no structure):
int ledmatrix[5][5][5];


It's difficult to proceed with an answer beyond this point, since you haven't really specified if you're going binary (on/off) or multi-state for the LEDs. I.E., what is the size and type of the R variable?

If it's 8-bits per color (and you're using the "LED" structure defined in an earlier post), then you can set the values of one LED element in the array like:

// Set the matrix location, // which corresponds to the physical 3D matrixunsigned char X,Y,Z;X = 2;Y = 3;Z = 0;// Now set the color values within the LED structureledmatrix[X][Y][Z].red = 255;ledmatrix[X][Y][Z].green = 0;ledmatrix[X][Y][Z].blue = 0;
guyfrom7up (author)  gmoon9 years ago
I think I'm starting to get it,

I want to multi-state my LEDs, but it's alos complicated because I only want to have one layer on at a time (so with a max of 25 LEDs, or 75 elements), and so if might get complicated siwtching between layers a lot of times every second...

BTW here's a you tube video which is very similar to what I'm building:


That's pretty cool (and just plain pretty.) It'll be a challenge, too. You might need to multiplex your I/O lines, or use more than one microcontroller...
guyfrom7up (author)  gmoon9 years ago
well, I'm using the ATmega2560, it has a whopping 86 I/O pins (need 80 for this project, consisting of 75 columns (5x5x 3 colors) and 5 planes of ground (multiplexing). Also the size of the program isn't an issue as it has a huge 256kbytes of memory.
Whoa. Memory won't be an issue, indeed. Looks like fun!
guyfrom7up (author)  gmoon9 years ago
I'll post more when all of the hardware is done, so that things like specific pins can be defined and such.
gmoon9 years ago
Yep, that's a function, alright. I can give you some examples, but we don't know what hardware, etc. you're using--you'll have to supply the LED code.

Basically, a function has

-- a body (defined by brackets)
-- arguments (data passed to the function at the start.) You can pass the function many arguments.
-- a return value (data passed back from the function at exit.) A function has only one return value (but that value can be a structure, which can be made of many variables.)
-- a scope (variables defined inside the function can't be accessed outside...in fact, they don't even exist, and will not be "persistent" unless you declare them static..)

Here's a function that returns a a random value integer from 0 to an input number (this has been made more verbose than necessary, just to illustrate the function.):

int getrandom(int range){int rvalue;   rvalue = rand() % range;return(range);}

And it's called like (from inside main(), or some other code block):
rnum = getrandom(15)

Note:

-- the initial int before the function defines the return value type (this returns an integer.)
-- the int before the "range" argument defines it's type.
-- return tells the function to return this value to whatever called it.
-- rnum is a previously declared integer. The value returned by the function is passed to it.

-- C passes by value, C++ can explicitly pass by reference, as well. But it's easy to make C pass by reference as well with variable pointers and addresses....(If you don't know what that means, don't worry about it too much.)

Quick explanation--if you pass by value, an entire copy of the variable is handed to the function; and an entire copy of the internal variable is passed back to whatever call the function. If you're passing structures, that can be a lot of overhead. Passing by reference allows you to tell the function "here's the address of my structure, now access it directly."
1-10 of 16Next »