PDA

View Full Version : need some help with C


twenglish1
February 27th, 2009, 06:47 PM
here is a little project i am working on, and i am having some problems, i am new to C programming and i am trying to custom make my own control board for a paintball gun, right now i am starting out simple, just with a programmable firing speed, i am using an attiny2313 avr microcontroller, when i program it(the code compiles, but it needs fixed) all it does it turn the fire_indicator led on, so can anyone help me??

here is my code:

#include <avr\io.h>
#include <util\delay.h>

#define TRIGGER_PORT PORTB
#define TRIGGER_BIT PB0
#define TRIGGER_PIN PINB

#define PROGRAM_BUTTON_PORT PORTB
#define PROGRAM_BUTTON_BIT PB1
#define PROGRAM_BUTTON_PIN PINB

#define FIRE_INDICATOR_ON PORTD |= (1<<PD2)
#define FIRE_INDICATOR_OFF PORTD &= (~(1<<PD2))

#define PROGRAM_MODE_ON PORTD |= (1<<PD1)
#define PROGRAM_MODE_OFF PORTD &= (~(1<<PD1))

#define DEBOUNCE_BUTTONS _delay_ms(24)
#define RATE_OF_FIRE _delay_ms(x)
#define ACTIVATION_TIME _delay_ms(1000*2)

int x = 100;

int TRIGGER_PULL()
{
if (bit_is_clear(TRIGGER_PIN, TRIGGER_BIT))
{
DEBOUNCE_BUTTONS;
if (bit_is_clear(TRIGGER_PIN, TRIGGER_BIT))
{
return 1;
}
else
{
return 0;
}
}
}


int INIT_IO()
{
/* PORTD SETUP */
DDRD |= (1<<PD2);
DDRD |= (1<<PD1);

/* PORTB SETUP */
DDRD &= (~(1<<PB0));
DDRD &= (~(1<<PB1));

/* ENABLE BUTTON PIN PULLUP */
PORTB |= (1<<PB0);
PORTB |= (1<<PB1);
}

int FIRE()
{
FIRE_INDICATOR_ON;
RATE_OF_FIRE;
FIRE_INDICATOR_OFF;
RATE_OF_FIRE;
FIRE_MODE();
}

int PROGRAM_BUTTON_PRESS()
{
if (bit_is_clear(PROGRAM_BUTTON_PIN, PROGRAM_BUTTON_BIT))
{
DEBOUNCE_BUTTONS;
if (bit_is_clear(PROGRAM_BUTTON_PIN, PROGRAM_BUTTON_BIT))
{
return 1;
}
else
{
return 0;
}
}
}

int PROGRAM_MODE()
{
/* TELL PROGRAM MODE IS ACTIVE */
PROGRAM_MODE_ON;
_delay_ms(500);
PROGRAM_MODE_OFF;

if (TRIGGER_PULL())
{
x = 0;
while(1)
{
if (TRIGGER_PULL())
{
_delay_ms(100);
if (TRIGGER_PULL())
{
x = x + 10;
}
else
{
/* TELL PROGRAM MODE IS INACTIVE */
PROGRAM_MODE_ON;
_delay_ms(500);
PROGRAM_MODE_OFF;
FIRE_MODE();
}
}
}
}
}


int FIRE_MODE()
{
while(1)
{
if (TRIGGER_PULL())
{
FIRE();
}
if (PROGRAM_BUTTON_PRESS())
{
_delay_ms(500);
if (PROGRAM_BUTTON_PRESS())
{
PROGRAM_MODE();
}
}
}
}

int main()
{
INIT_IO();
FIRE_MODE();
}

ishkur88
February 28th, 2009, 04:46 PM
Well first of all, I suggest you learn about the wonders of pastebins:

http://pastie.org/403325

Now, as for a few little nitpicks about your code.

* In your PROGRAM_MODE method, I see no reason for the variable "x". As a matter of fact, I don't see the use of "x" as a global variable either. My suggestion is to either figure out why you had it there in the first place and do something useful with it, or just get rid of it: it's absurdly pointless as it stands.

* I'm not entirely sure what your FIRE method is supposed to be doing. FIRE makes a call to FIRE_MODE, which itself calls back to FIRE. So what you end up having is some weird and useless form of recursion.

* You need to learn about ternary operations (http://en.wikipedia.org/wiki/Conditional_operator). They save space and make things less verbose.

* Going with the need to learn more operators, you could also stand to learn about some of the arithmetic operators (http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Arithmetic_operators), na***y the "assignment by addition" one. Instead of having:

x = x + 10;

you could have, instead:

x += 10;

which is a lot more concise and less verbose, and wont get you laughed at.

Overall, however, I suggest you rewrite this, but with a little more understanding of what you're doing. I see you're trying to do this by abstracting certain actions into their own little methods, but they don't seem like they work.

Other than that, I'm not sure what else to say.

twenglish1
March 1st, 2009, 01:30 AM
yah i just rewrote this, but still nothing, i got rid of the FIRE() function and made it part of FIRE_MODE() and the same thing with PROGRAM_MODE() but its still not working:
here is my new code:

http://pastie.org/403620