Need some help with C

victor

Antediluvian as Feck
Orderite
I have a teaching experiment in electronics on Monday that involves flashing a microcontroller controlling a H-bridge in turn connected to a DC motor. I need to write code for it in C, and I'd like to get most of it done before the actual experiment, so I'm doing it now. Or at least trying to. I wouldn't be surprised my code is riddled with syntax errors, inconsistencies and missing functions.

Most of the programs I've written so far have been fairly simple, so not much can go wrong, and even if it has, it's easy to correct. I'm having a lot of trouble with the final one, though. I have to write a timed sequence that varies the voltage, and hence the motor speed. Here's the sequence:



(hope it's not too much copyright infringement to post it here...)

Nevermind the x and y, we'll be given those values during the experiment.

I'm trying to do this with for functions with an integer variable called "time", with 1 second increments. The vertical axis is the motor speed, but that's controleld by the voltage, so the object variable of the for functions can be either:

1. voltage "volt", a floating variable that can adopt any value from 0 to 5

2. "duty_cycle", an integer variable, basically a percentage of the voltage.


I reckon the easiest would be to use "duty_cycle", as it's an integer variable. But as I said, I'm not sure how the function works. Can I include the entire sequence in a single function? Is it better to use a while function or switch/case.

However, the increase (or decrease) in motor speed is, judging from the sequence, linear and continuous, which I interpret as best described by a floating variable. I'm also not sure how to have the objective variable vary continuously with a 1 increment increase in time.


I don't expect anyone to solve it (nor do I want anyone to), as there's a lot of information missing, but I'd appreciate any hint or suggestion. This is the first time ever I'm programming, and I started this morning.
 
Actually, when asking any related programming questions, programmers(or hackers) appreciate a lot that instead of describing how are you trying to do something, you describe what's the main objective. For describing what are you trying to do actually, there's nothing better than code, as long as it remains short.

I really can't help you much since I don't really know what you need to calculate, You gave a short explanation, but I still don't understand what are you supposed to get... Maybe my brain's just blocked though.
 
I'm supposed to control the voltage output given by the curve. I'm looking for a virtual function, and just the principle behind it (I'm not supposed to use the processor's actual timer as it's not included in the given command list).

So the idea is "virtual timer variable" gives "relative voltage output differential". It could be anything, really; doesn't have to be voltage. Could be an increase in animal population. I just want to know how to define a function with intervals like the one described by the graph. Here's some code:

Code:
#include "gnu_met3.h"

char * prog = "Motor_Medel";
int ver	    = 0;


int ad_value;
int duty_cycle;
int time;
float volt;


int main(void)

{
	init_met();

	move_cursor(1,1);
	dprintf("%s v.%i", prog, ver);	


for(time = 1; time < x; time = time + 1)	    
		{						    
			duty_cycle = duty_cycle + 	          
			duty_cycle = 0; 	
		}

                PWM0(duty_cycle);	

		
		for(time = x; time < x+y; time = time + 1)
		{
			duty_cycle = 50; 
		}

		PWM0(duty_cycle);	

		
		for(time = x+y; time < x+y+x; time = time + 1)
		{
			duty_cycle = duty_cycle + 	
			duty_cycle = 50; duty_cycle <= 100
		}

		PWM(duty_cycle);	

	
		for(time = x+y+x; time < x+y+x+y; time = time+1)
		{
			duty_cycle = 100
		}

		PWM0(duty_cycle);	

		for(time = x+y+x+y; time < x+y+x+y+x; time = time+1)
		{
			duty_cycle = duty_cycle - 1


As you can see, I tried to "brute force" the sequence by just defining the intervals in order, and trying something a little different for each function. Half motor speed (i.e. nmax/2) is the equivalent of duty_cycle = 50 percent, and full motor speed (i.e. nmax) is the equivalent of duty_cycle = 100.

gnu_met3.h is just the library, init_met() is to start the microcontroller, and there's also a display, so I defined a text message for it. Those things are less important though.

I can't really testrun this program before tomorrow, though.
 
Are you supposed to do any visual output for this program? I mean, I could teach you in some minutes to do a simple graphic in the Allegro library if you need it. Because personally, I hate using the console for doing graphical output.

Also, did you cut the code intentionally at the end or there is a limit in the code tag?

I'm doing an example for you. I'll post it when it's finished.

EDIT: Ok, here is is.

You'll need Allegro 4.2 to compile it. You can download it from here.

http://www.allegro.cc/files/.

I've provided a .cbp project and a Windows binary so you can test the program. It's a simple program that depending on the position of the mouse, it'll draw a circle in the corresponding section.

I've commented it a lot. Enjoy it!
http://www.zshare.net/download/75631964989220e0/
 
Back
Top