View Full Version : C programming question


cato
01-14-06, 12:53 PM
at the end of this semester my digital systems class will have, as it does every semester, and obstacle course for programmable robots to make it through.

we will be given a light sensor for an input. so basically, we are to figure out a way to guide our robots through the maze by giving it different signals, to be interpreted by the C program we wrote

anyway, my question is: is it possible (and fairly easy) make a C program count the number of flashes I gave it in a certain time frame? I don't think I encountered such a program in my programming class.

I do not want you do write the program for me, I am just looking for methods of counting the number of inputs within a certain time frame.

here are my two ideas thus far:
1. Make the program so that a flash of light will stop its forward motion. It will then wait X seconds for another flash. If it does not get another flash, it will turn left. If it does get another flash, it will wait X more seconds. If it now does not get another flash, it will turn right. However, if it gets a third flash, it will drive straight.

2. If given a flash of light it will stop, and wait 2 seconds, if it gets zero flashes in that time it will turn left, if it gets one, it will turn right, and if it gets 2 it will go straight.

I would rather do the second one, but I am not sure how to make it count the flashes.

any help would be great =]

Xerxes
01-14-06, 01:18 PM
Duck Hunt worked by using the gun to count scintillations from the TV, sort of like you are trying to do know. Maybe read up on how they did it?

You could probably make it something like morse code. Three long flashes right, three short flashes left, one short followed by two long forward, one long and two short back.. or something like that. Or maybe instead of counting the flashes, count the length of time the light is on, and use that.. Of course that might be slower.

cato
01-14-06, 01:39 PM
well, I would still have to figure out how to make it count my number of flashes, but also add the complication of measuring the length of the flash.

on the other hand, I might be able to google how to make a c program understand morse code =]. I will google it later.

domesticated om
01-14-06, 02:08 PM
I'm currently working my way up to C (I'm doing vb .net right now), so I'll throw in a few ideas---- but it's going to look like really n00b level basic VB.

At any rate, I assume it prolly works the same way but the way it's written is different, so perhaps you could...

Dimension your 'light detection event' (a single flash??) As an instance = 1 As an Integer
Dimension input As an Integer equal to the sum from the number of light detection events


Then write "if- then" recursion statements that perform the function (whatever script is required to instruct robot to move left/right/etc) based on "If Input ="

cato
01-14-06, 02:20 PM
yeah, that is what I plan to do (or something similar) I was just wondering how to make it count the number of flashes. I can't sum them if I can't count them =]

domesticated om
01-14-06, 02:22 PM
What kind of data/values are you capable of retrieving from the light detector?

cato
01-14-06, 10:13 PM
I don't know yet. my teacher has not said anything about it yet. but I know they will have it, they do every semester, from what I hear.

I am pretty sure its just a basic photo-cell. so I can probably only get a 1 or a 0 for a higher and lower voltage. where a 0 would be normal room lighting, and a 1 would be brighter.

leopold99
01-14-06, 10:34 PM
the first question is can you get the flashes into your program?
this might be an instance where you have to generate an irq for each flash
when the irq is generated your prgram will access the input port that is connected to your sensor then do a loop to count the seconds

Bowser
01-14-06, 10:56 PM
They're still using C? Wow, it's been a long time... Hmm...

I don't remember much, but it sounds like you need a conditional loop...


Loops
Most real programs contain some construct that loops within the program, performing repetitive actions on a stream of data or a region of memory. There are several ways to loop in C. Two of the most common are the while loop:

while (expression)
{
...block of statements to execute...
}

and the for loop:

for (expression_1; expression_2; expression_3)
{
...block of statements to execute...
}

The while loop continues to loop until the conditional expression becomes false. The condition is tested upon entering the loop. Any logical construction (see below for a list) can be used in this context.

The for loop is a special case, and is equivalent to the following while loop:

expression_1;

while (expression_2)
{
...block of statements...

expression_3;
}

For instance, the following structure is often encountered:

i = initial_i;

while (i <= i_max)
{
...block of statements...

i = i + i_increment;
}

This structure may be rewritten in the easier syntax of the for loop as:

for (i = initial_i; i <= i_max; i = i + i_increment)
{
...block of statements...
}

Infinite loops are possible (e.g. for(;;)), but not too good for your computer budget! C permits you to write an infinite loop, and provides the break statement to ``breakout '' of the loop. For example, consider the following (admittedly not-so-clean) re-write of the previous loop:

angle_degree = 0;

for ( ; ; )
{
...block of statements...

angle_degree = angle_degree + 10;
if (angle_degree == 360) break;
}

The conditional if simply asks whether angle_degree is equal to 360 or not; if yes, the loop is stopped.





The above is from the following site: http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/c_tutorial.html#loops

Bowser
01-14-06, 10:59 PM
Have a look at the "if" statement too.

if(t=>d)
{
...
}

leopold99
01-14-06, 11:19 PM
well, I would still have to figure out how to make it count my number of flashes, but also add the complication of measuring the length of the flash.

when the first flash is received your program will jump to a subroutine
there it will loop for the required time
each flash will increment a variable or mabe a memory location instead
at the end of the required time the program will return to the main body
the main body will access the variable or memory location used to count the flashes
the mainbody clears the variable or memory location for the next use

this is a typical example of computers being as dumb as a box of rocks

domesticated om
01-15-06, 12:23 AM
I don't know yet. my teacher has not said anything about it yet. but I know they will have it, they do every semester, from what I hear.

I am pretty sure its just a basic photo-cell. so I can probably only get a 1 or a 0 for a higher and lower voltage. where a 0 would be normal room lighting, and a 1 would be brighter.

Hmmmmmm.
Well-- first off, does the bot need to be doing anything prior to being flashed at? ........or is it just going to sit there and maybe a flash signals it to start moving with whatever future flashes it receives being translated into additional commands?

cato
01-17-06, 12:59 PM
ok, I almost have it down. how do you declare an array of a size to be determined later? I tried just doing int b[n]={0}, after the point in my program where I do a loop to determine the value of n i will need, but I keep getting an error that tells me I need a ; before "type". gerr, C can be frustrating =]

Zephyr
01-17-06, 02:35 PM
int *b;

/*before use*/
b = malloc(sizeof(int)*n);

/*...later...*/
free(b);

/*If it were C++ you could just use a vector class... if it were Java or Python or any other language with a garbage collector, you wouldn't have to worry about freeing the memory later...*/

Zephyr
01-17-06, 02:42 PM
An alternative is to make the array 'as large as you'll need' if you have enough memory, and just keep track of how much of it you actually use.

Blue_UK
01-17-06, 06:11 PM
I have, just today, been programming a PIC chip using the CPU clock frequency as a reference.

You should be able to access an internal counter on the robot's CPU. PC's also have calls for checking this sort of thing. You'll need to read up on the specific compiler for whatever chipset you are writing for to access this counter. So if you're coding for a PC (and not an embedded system) you should use some other kind time function - such as the one that gives the number of microseconds since the program was started.

Come to think of it - the PIC counter actually calls an interupt request (IRQ) when it fills up which, quite handily, can be setup to trigger another function. Again, something to check up in the compiler manual.

One straight-forward but probably crap way to do it would be to have a while loop that waits for a flash but also increments a variable. the value of the variable upon exit of the while loop will be proportional to the amount of time since the last flash.

I would use a stack of structs rather than an basic array to store each occurance of a flash, with elements that tell you about when the flash was.

cato
01-17-06, 10:04 PM
int *b;

/*before use*/
b = malloc(sizeof(int)*n);

/*...later...*/
free(b);

just to be clear, b is my array and n is my size?

Zephyr
01-18-06, 01:14 AM
just to be clear, b is my array and n is my size?
Yep.

Although, if you're programming a microchip are you sure it supports the full C language as opposed to some subset? I wonder how it would deal with the memory management issues created by malloc...

cato
01-18-06, 03:36 PM
I wonder how it would deal with the memory management issues created by malloc
good point =]

I guess I will have to wait till the end of the semester to find out.

AntonK
01-18-06, 05:11 PM
What you're asking isn't really so much a C programming question. Honestly in a project like this, knowing the C language is the least of your problems. My guess is that you are working with a PIC chip from Microchip. These are cheap, easy to use, and really nice for beginners. They usually come in at about 4MHz. They aren't actually programmed in C at all, just like any other processor they have their own ISA or instruction set architecture. The compiler you have will most likely use a subset of the C library and enable you to program.

Some things to watch out for. Most likely you are using an 8 bit or 16 bit chip. If it is 8 bit, then you cannot use a signed number greater than 128 or less than -127 and no unsigned number larger than 255. Similarly with 16 bit and 32k and 64k. Also, remember you have NO operating system, unless you are using a bootloader or some sort of micro OS, of which there are many, but the overhead is usually not worth it especially on a project so small. With that in mind, there is no such thing in malloc. On a computer, malloc is translated to a system call to the operating system for memory. In this embedded world you already have all the memory, you just allocate it when you start out. If you're design calls for dynamic memory, chances are you have a bad design.

PIC chips usually also have some build in features such as A/D converters, which will give you a number usually ranging between 0 and 255 or 0 an d 64K which describes a voltage on a specific pin. Sometimes they also give you numbers related to PWM or other sorts of input. Your C library from your C compiler will have specific functions already built in that will give you the ability to read from these input pins. But you'll have to give me more information before I can help you on how exactly to program a small embedded system like you're describing. Oh and just for the record, I've built several small projects by myself like this, most simple. I've also been involved in writing autonomous software for robots in the 100-1000 lb range, as well as worked a bit with a team on the Darpa Grand Challenge.

-AntonK