Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   A few simple programming questions; (http://www.chiefdelphi.com/forums/showthread.php?t=44028)

Oumonkey 14-02-2006 20:23

A few simple programming questions;
 
Well, First we have a few problems so I'll label them. I'll upload the code at the end, so far its all in the user_routines.c

1. I'm not sure anymore if I am programming the buttons correctly, I don't know much about C so its a good problem I'm messing up somewhere. Here is one part I am trying the buttons will do the "else" part but will not do the if part of it. I'm sure I am doing something wrong here so help is nice.
if(p3_sw_top == 1)
{
pwm09 = 127;
pwm10 = 127;
}
else
{
pwm09 = 254;
pwm10 = 254;
}
and
if(p3_sw_trig == 1)
{
pwm09 = 0;
pwm10 = 0;
}
else (p3_sw_trig == 0);
{
pwm09 = 254;
pwm10 = 254;
}

2. We are using spikes this year, and I am not sure how to program them, I'm sure I have to program it somehow but I am at a lost of how to.

3. I am getting a syntax error at line 467 in our user_routines.c...The code ends at 466, before that really cause the last few lines are the from the /**/ stuff. I hope I put the code on here correctly. If not I'll edit it after I post.

4. I'm still looking but can someone tell me where to get a white paper about limit switches, this isn't something I am worrying to much about now, though. I'll just have to play with it later. Which will me create a few threads in the future most likely.

Thats all, I know its a lot, but I'm the only programmer on the team and I know nothing about C. So I would really appreciate any help you guys can give me. Also sorry the code is kinda sloppy, I have been commenting things in and out adding so much stuff it makes my brain hurts. I'll also give a cookie to all who help :D
Thanks
Josh


Code:

/*******************************************************************************
* FILE NAME: user_routines.c <FRC VERSION>
*
* DESCRIPTION:
*  This file contains the default mappings of inputs 
*  (like switches, joysticks, and buttons) to outputs on the RC. 
*
* USAGE:
*  You can either modify this file to fit your needs, or remove it from your
*  project and replace it with a modified copy.
*
*******************************************************************************/

#include <stdio.h>

#include "ifi_aliases.h"
#include "ifi_default.h"
#include "ifi_utilities.h"
#include "user_routines.h"
#include "user_Serialdrv.h"

extern unsigned char aBreakerWasTripped;

/*** DEFINE USER VARIABLES AND INITIALIZE THEM HERE ***/
/* EXAMPLES: (see MPLAB C18 User's Guide, p.9 for all types)
unsigned char wheel_revolutions = 0; (can vary from 0 to 255)
unsigned int  delay_count = 7;      (can vary from 0 to 65,535)
int          angle_deviation = 142; (can vary from -32,768 to 32,767)
unsigned long very_big_counter = 0;  (can vary from 0 to 4,294,967,295)
*/

/*******************************************************************************
* FUNCTION NAME: Limit_Switch_Max
* PURPOSE:      Sets a PWM value to neutral (127) if it exceeds 127 and the
*                limit switch is on.
* CALLED FROM:  this file
* ARGUMENTS:   
*    Argument      Type            IO  Description
*    --------      -------------    --  -----------
*    switch_state  unsigned char    I    limit switch state
*    *input_value  pointer          O  points to PWM byte value to be limited
* RETURNS:      void
*******************************************************************************/
void Limit_Switch_Max(unsigned char switch_state, unsigned char *input_value)
{
  if (switch_state == CLOSED)
  {
    if(*input_value > 127)
      *input_value = 127;
  }
}


/*******************************************************************************
* FUNCTION NAME: Limit_Switch_Min
* PURPOSE:      Sets a PWM value to neutral (127) if it's less than 127 and the
*                limit switch is on.
* CALLED FROM:  this file
* ARGUMENTS:   
*    Argument      Type            IO  Description
*    --------      -------------    --  -----------
*    switch_state  unsigned char    I    limit switch state
*    *input_value  pointer          O  points to PWM byte value to be limited
* RETURNS:      void
*******************************************************************************/
void Limit_Switch_Min(unsigned char switch_state, unsigned char *input_value)
{
  if (switch_state == CLOSED)
  {
    if(*input_value < 127)
      *input_value = 127;
  }
}


/*******************************************************************************
* FUNCTION NAME: Limit_Mix
* PURPOSE:      Limits the mixed value for one joystick drive.
* CALLED FROM:  Default_Routine, this file
* ARGUMENTS:   
*    Argument            Type    IO  Description
*    --------            ----    --  -----------
*    intermediate_value    int    I   
* RETURNS:      unsigned char
*******************************************************************************/
unsigned char Limit_Mix (int intermediate_value)
{
  static int limited_value;
 
  if (intermediate_value < 2000)
  {
    limited_value = 2000;
  }
  else if (intermediate_value > 2254)
  {
    limited_value = 2254;
  }
  else
  {
    limited_value = intermediate_value;
  }
  return (unsigned char) (limited_value - 2000);
}


/*******************************************************************************
* FUNCTION NAME: User_Initialization
* PURPOSE:      This routine is called first (and only once) in the Main function. 
*                You may modify and add to this function.
* CALLED FROM:  main.c
* ARGUMENTS:    none
* RETURNS:      void
*******************************************************************************/
void User_Initialization (void)
{
  Set_Number_of_Analog_Channels(SIXTEEN_ANALOG);    /* DO NOT CHANGE! */

/* FIRST: Set up the I/O pins you want to use as digital INPUTS. */
  digital_io_01 = digital_io_02 = digital_io_03 = digital_io_04 = INPUT;
  digital_io_05 = digital_io_06 = digital_io_07 = digital_io_08 = INPUT;
  digital_io_09 = digital_io_10 = digital_io_11 = digital_io_12 = INPUT;
  digital_io_13 = digital_io_14 = digital_io_15 = digital_io_16 = INPUT;
  digital_io_18 = INPUT;  /* Used for pneumatic pressure switch. */
    /*
    Note: digital_io_01 = digital_io_02 = ... digital_io_04 = INPUT;
          is the same as the following:

          digital_io_01 = INPUT;
          digital_io_02 = INPUT;
          ...
          digital_io_04 = INPUT;
    */

/* SECOND: Set up the I/O pins you want to use as digital OUTPUTS. */
  digital_io_17 = OUTPUT;    /* Example - Not used in Default Code. */

/* THIRD: Initialize the values on the digital outputs. */
  rc_dig_out17 = 0;

/* FOURTH: Set your initial PWM values.  Neutral is 127. */
  /*pwm01 = pwm02 =*/ pwm03 = pwm04 = pwm05 = pwm06 = pwm07 = pwm08 = 127;
  pwm09 = pwm10 = pwm11 = pwm12 = pwm13 = pwm14 = pwm15 = pwm16 = 127;

/* FIFTH: Set your PWM output types for PWM OUTPUTS 13-16.
  /*  Choose from these parameters for PWM 13-16 respectively:              */
  /*    IFI_PWM  - Standard IFI PWM output generated with Generate_Pwms(...) */
  /*    USER_CCP - User can use PWM pin as digital I/O or CCP pin.          */
  Setup_PWM_Output_Type(IFI_PWM,IFI_PWM,IFI_PWM,IFI_PWM);

  /*
    Example: The following would generate a 40KHz PWM with a 50% duty cycle on the CCP2 pin:

        CCP2CON = 0x3C;
        PR2 = 0xF9;
        CCPR2L = 0x7F;
        T2CON = 0;
        T2CONbits.TMR2ON = 1;

        Setup_PWM_Output_Type(USER_CCP,IFI_PWM,IFI_PWM,IFI_PWM);
  */

  /* Add any other initialization code here. */
 
  Putdata(&txdata);            /* DO NOT CHANGE! */

  Serial_Driver_Initialize();

  printf("IFI 2006 User Processor Initialized ...r");  /* Optional - Print initialization message. */

  User_Proc_Is_Ready();        /* DO NOT CHANGE! - last line of User_Initialization */
}

/*******************************************************************************
* FUNCTION NAME: Process_Data_From_Master_uP
* PURPOSE:      Executes every 26.2ms when it gets new data from the master
*                microprocessor.
* CALLED FROM:  main.c
* ARGUMENTS:    none
* RETURNS:      void
*******************************************************************************/
void Process_Data_From_Master_uP(void)
{
  static unsigned char i;

  Getdata(&rxdata);  /* Get fresh data from the master microprocessor. */

  Default_Routine();  /* Optional.  See below. */

  /* Add your own code here. (a printf will not be displayed when connected to the breaker panel unless a Y cable is used) */

  printf("Port1 Y %3d, X %3d, Fire %d, Top %dr",pwm01,pwm05,p1_sw_trig,p1_sw_top);  /* printf EXAMPLE */

  Generate_Pwms(pwm13,pwm14,pwm15,pwm16);

  /* Example code to check if a breaker was ever tripped. */

  if (aBreakerWasTripped)
  {
    for (i=1;i<29;i++)
    {
      if (Breaker_Tripped(i))
        User_Byte1 = i;  /* Update the last breaker tripped on User_Byte1 (to demonstrate the use of a user byte)
                            Normally, you do something else if a breaker got tripped (ex: limit a PWM output)    */
    }
  }

  Putdata(&txdata);            /* DO NOT CHANGE! */
}

/*******************************************************************************
* FUNCTION NAME: Default_Routine
* PURPOSE:      Performs the default mappings of inputs to outputs for the
*                Robot Controller.
* CALLED FROM:  this file, Process_Data_From_Master_uP routine
* ARGUMENTS:    none
* RETURNS:      void
*******************************************************************************/
void Default_Routine(void)
{
 

 /*---------- Analog Inputs (Joysticks) to PWM Outputs-----------------------
  *--------------------------------------------------------------------------
  *  This maps the joystick axes to specific PWM outputs.
  pwm01 = p1_y; ORIGANAL STUFF
  pwm02 = p2_y; 
  pwm03 = p3_y; 
  pwm04 = p4_y; 
  pwm05 = p1_x; 
  pwm06 = p2_x; 
  pwm07 = p3_x; 
  pwm08 = p4_x; 
  pwm09 = p1_wheel;
  pwm10 = p2_wheel; 
  pwm11 = p3_wheel; 
  pwm12 = p4_wheel; 
  */
 

  /*pwm01;*/ 
 
  /*pwm02;*/   
 
  /*pwm03;  */  /*cmucam power*/
 
  /*pwm04; */    /*none*/
 
  /*pwm05;*/    /*solenoid-plate*/
  /*???????*/
  /*pwm06;*/  /*door-bush-1 on/off fwd/off-2 on/off rev if rev then pw 11/12 off*/ 
 /*  if(p3_sw_aux1 == 1)
  {
  pwm06 = 0;
  }
/*
 else if(p3_sw_aux1 == 0)
  {
        pwm06 = 254;                /*
  }
  /*pwm07;*/      /*globe H pan r/l*/
  pwm07 = p3_x;
  /*pwm08;*/      /*globe V tilt u/d*/
  pwm08 = p3_y;
  /*pwm09 = pwm10;*/  /*MB shoot* 1/2*/
 
 

  if(p3_sw_trig)
        {       
       

  /*if(p3_sw_trig == 1)
  {
        pwm09 = 0;
    pwm10 = 0;
  }
 /* else (p3_sw_trig == 0);
  {
  pwm09 = 254;
  pwm10 = 254; 
  }                                */

                    /*window r on/off*/
  /*pwm11 = pwm12; */  /*window l on/off*/
 
  if(p3_sw_top == 1)
  {
  pwm09 = 127;
  pwm10 = 127;
  }
  else
  {
  pwm09 = 254;
  pwm10 = 254;
  }               
  if(p4_sw_trig == 1)
  {
        pwm05 = 127;                /*else if statement after*/
        pwm06 = 127;
        pwm07 = 127;
        pwm08 = 127;
        pwm09 = 127;
        pwm10 = 127;
        pwm11 = 127;
        pwm12 = 127;
        }
 /*---------- 2 Joystick Drive ----------------------------------------------
  *--------------------------------------------------------------------------
  *  This code mixes the Y and X axis on Port 1 to allow one joystick drive.
  *  Joystick forward  = Robot forward
  *  Joystick backward = Robot backward
  *  Joystick right    = Robot rotates right
  *  Joystick left    = Robot rotates left
  *  Connect the right drive motors to PWM13 and/or PWM14 on the RC.
  *  Connect the left  drive motors to PWM15 and/or PWM16 on the RC.
  */ 
  pwm13 = 254 - p2_y;
  pwm14 = 254 - p2_y;
  pwm15 = p1_y;
  pwm16 = p1_y;
  if((pwm13>118)&&(pwm13<136)) pwm13 = 127;
  if((pwm14>118)&&(pwm14<136)) pwm14 = 127;
  if((pwm15>118)&&(pwm15<136)) pwm15 = 127;
  if((pwm16>118)&&(pwm16<136)) pwm16 = 127;
  /*if((pwm03>145)) pwm03 = 255;
  if((pwm03<109)) pwm03 = 0;*/  /*old robot arm*/
  /*if((pwm13>189)) pwm13 = 230;pwm13
  if((pwm14>189)) pwm14 = 230;
  if((pwm15>189)) pwm15 = 230;
  if((pwm16>189)) pwm16 = 230;
  if((pwm13<65)) pwm13 = 25;
  if((pwm14<65)) pwm14 = 25;
  if((pwm15<65)) pwm15 = 25;
  if((pwm16<65)) pwm16 = 25; */
 
        /*
        orange = banana
        undefened int = orange
        undefened int = banana
        if((orange>129)){orange = 254};
        if((orange>120)&&(orange<134)) orange = 127;
        else if((orange>=0)&&(orange<=120))orange=0; 
        else((orange>=134)&&(orange<=254)) orange=254;
*/
 /*------
 
  /*---------- Buttons to Relays----------------------------------------------
  *--------------------------------------------------------------------------
  *  This default code maps the joystick buttons to specific relay outputs. 
  *  Relays 1 and 2 use limit switches to stop the movement in one direction.
  *  The & used below is the C symbol for AND                               
  */
  /*
 /* relay1_fwd = p1_sw_trig & rc_dig_in01;  /* FWD only if switch1 is not closed. */
 /* relay1_rev = p1_sw_top  & rc_dig_in02;  /* REV only if switch2 is not closed. */
 /* relay2_fwd = p2_sw_trig & rc_dig_in03;  /* FWD only if switch3 is not closed. */
 /* relay2_rev = p2_sw_top  & rc_dig_in04;  /* REV only if switch4 is not closed. */
  /*
  relay3_fwd = p3_sw_trig;
  relay3_rev = p3_sw_top;
  relay4_fwd = p4_sw_trig;
  relay4_rev = p4_sw_top;
  relay5_fwd = p1_sw_aux1;
  relay5_rev = p1_sw_aux2;
  relay6_fwd = p3_sw_aux1;
  relay6_rev = p3_sw_aux2;
  relay7_fwd = p4_sw_aux1;
  /*relay7_rev = p4_sw_aux2; */
  /*relay8_fwd = !rc_dig_in18;  /* Power pump only if pressure switch is off. */
 /* relay8_rev = 0;
        */
 
  /*---------- PWM outputs Limited by Limit Switches  ------------------------*/
 
  Limit_Switch_Max(rc_dig_in05, &pwm03);
  Limit_Switch_Min(rc_dig_in06, &pwm03);
  Limit_Switch_Max(rc_dig_in07, &pwm04);
  Limit_Switch_Min(rc_dig_in08, &pwm04);
  Limit_Switch_Max(rc_dig_in09, &pwm09);
  Limit_Switch_Min(rc_dig_in10, &pwm09);
  Limit_Switch_Max(rc_dig_in11, &pwm10);
  Limit_Switch_Min(rc_dig_in12, &pwm10);
  Limit_Switch_Max(rc_dig_in13, &pwm11);
  Limit_Switch_Min(rc_dig_in14, &pwm11);
  Limit_Switch_Max(rc_dig_in15, &pwm12);
  Limit_Switch_Min(rc_dig_in16, &pwm12);
 
 /*---------- ROBOT FEEDBACK LEDs------------------------------------------------
  *------------------------------------------------------------------------------
  *  This section drives the "ROBOT FEEDBACK" lights on the Operator Interface.
  *  The lights are green for joystick forward and red for joystick reverse.
  *  Both red and green are on when the joystick is centered.  Use the
  *  trim tabs on the joystick to adjust the center.   
  *  These may be changed for any use that the user desires.                     
  */       
 
  if (user_display_mode == 0) /* User Mode is Off */
   
  { /* Check position of Port 1 Joystick */
    if (p1_y >= 0 && p1_y <= 56)
    {                    /* Joystick is in full reverse position */
      Pwm1_green  = 0;    /* Turn PWM1 green LED - OFF */
      Pwm1_red  = 1;      /* Turn PWM1 red LED  - ON  */
    }
    else if (p1_y >= 125 && p1_y <= 129)
    {                    /* Joystick is in neutral position */
      Pwm1_green  = 1;    /* Turn PWM1 green LED - ON */
      Pwm1_red  = 1;      /* Turn PWM1 red LED  - ON */
    }
    else if (p1_y >= 216 && p1_y <= 255)
    {                    /* Joystick is in full forward position*/
      Pwm1_green  = 1;    /* Turn PWM1 green LED - ON  */
      Pwm1_red  = 0;      /* Turn PWM1 red LED  - OFF */
    }
    else
    {                    /* In either forward or reverse position */
      Pwm1_green  = 0;    /* Turn PWM1 green LED - OFF */
      Pwm1_red  = 0;      /* Turn PWM1 red LED  - OFF */
    }  /*END Check position of Port 1 Joystick
   
    /* Check position of Port 2 Y Joystick
          (or Port 1 X in Single Joystick Drive Mode) */
    if (p2_y >= 0 && p2_y <= 56)
    {                    /* Joystick is in full reverse position */
      Pwm2_green  = 0;    /* Turn pwm2 green LED - OFF */
      Pwm2_red  = 1;      /* Turn pwm2 red LED  - ON  */
    }
    else if (p2_y >= 125 && p2_y <= 129)
    {                    /* Joystick is in neutral position */
      Pwm2_green  = 1;    /* Turn PWM2 green LED - ON */
      Pwm2_red  = 1;      /* Turn PWM2 red LED  - ON */
    }
    else if (p2_y >= 216 && p2_y <= 255)
    {                    /* Joystick is in full forward position */
      Pwm2_green  = 1;    /* Turn PWM2 green LED - ON  */
      Pwm2_red  = 0;      /* Turn PWM2 red LED  - OFF */
    }
    else
    {                    /* In either forward or reverse position */
      Pwm2_green  = 0;    /* Turn PWM2 green LED - OFF */
      Pwm2_red  = 0;      /* Turn PWM2 red LED  - OFF */
    }  /* END Check position of Port 2 Joystick */
   
    /* This drives the Relay 1 and Relay 2 "Robot Feedback" lights on the OI. */
    Relay1_green = relay1_fwd;    /* LED is ON when Relay 1 is FWD */
    Relay1_red = relay1_rev;      /* LED is ON when Relay 1 is REV */
    Relay2_green = relay2_fwd;    /* LED is ON when Relay 2 is FWD */
    Relay2_red = relay2_rev;      /* LED is ON when Relay 2 is REV */

    Switch1_LED = !(int)rc_dig_in01;
    Switch2_LED = !(int)rc_dig_in02;
    Switch3_LED = !(int)rc_dig_in03;
   
  } /* (user_display_mode = 0) (User Mode is Off) */
 
  else  /* User Mode is On - displays data in OI 4-digit display*/
  {
    User_Mode_byte = backup_voltage*10; /* so that decimal doesn't get truncated. */
  } 
 
} /* END Default_Routine(); */


/******************************************************************************/
/******************************************************************************/
/******************************************************************************/


Inverted 14-02-2006 20:54

Re: A few simple programming questions;
 
Sorry I'm not being as big of a help as I should be, but I'm in the middle of homework and just answering the questions where the solution is on the top of my head.

Quote:

else (p3_sw_trig == 0);
{
pwm09 = 254;
pwm10 = 254;
}
You don't need (p3_sw_trig == 0) after the else, it's already implied. Also, (this may be wrong because I haven't written code like this in 2 years) I think you only need one = when referring to the value of the button, not ==.

Quote:

2. We are using spikes this year, and I am not sure how to program them, I'm sure I have to program it somehow but I am at a lost of how to.
The PWM cable from the Spike goes into one of the Relay outputs on the robot controller. Then you just map which buttons you want to control which relays and you're good. Not sure if you know how to or not, but send me a PM if you want to know how to have one button control the in/out of a piston and such (like if you want a solenoid to act like a festo).

Keith Watson 14-02-2006 21:42

Re: A few simple programming questions;
 
Quote:

Originally Posted by Oumonkey
if(p3_sw_trig == 1)
{
pwm09 = 0;
pwm10 = 0;
}
else (p3_sw_trig == 0);
{
pwm09 = 254;
pwm10 = 254;
}

The portion highlighted in red is a syntax error. There are two ways to correct the error. Since the value of the variable can only be 0 or 1 you can just delete the portion in red. Or you can change the syntax to:
Code:

if (p3_sw_trig == 1)
{
  pwm09 = 0;
  pwm10 = 0;
}
else if (p3_sw_trig == 0)
{
  pwm09 = 254;
  pwm10 = 254; 
}


Keith Watson 14-02-2006 21:45

Re: A few simple programming questions;
 
Quote:

Originally Posted by Inverted
Also, (this may be wrong because I haven't written code like this in 2 years) I think you only need one = when referring to the value of the button, not ==.

Changing to a single = would be legal but have a totally different meaning and in this case be wrong. A single = would cause an assignment to occur instead of doing a comparison.

DanDon 14-02-2006 21:46

Re: A few simple programming questions;
 
Code:

  if(p3_sw_top == 1)
  {
  pwm09 = 127;
  pwm10 = 127;
  }
  else
  {
  pwm09 = 254;
  pwm10 = 254;
  }

This might be your intention, but with this code, the pwm's will only be at 127 when the button is held down, when the button is not held down, they will be at 254.


Quote:

2. We are using spikes this year, and I am not sure how to program them, I'm sure I have to program it somehow but I am at a lost of how to.
With spikes, you would basically program them using two variables. If your spike was hooked up to relay1, then your two variables would be:
relay1_fwd and relay1_rev. If you wanted to fire the spike in one direction, you would set:

Code:

relay1_fwd=1;
relay1_rev=0;

The other direction would be:

Quote:

relay1_fwd=0;
relay1_rev=1;
And neutral would be:
Code:

relay1_fwd = relay1_rev = 0;
Quote:

else (p3_sw_trig == 0);
{
pwm09 = 254;
pwm10 = 254;
}
If you were to have the condition after the else, despite the fact that it's not needed, since the button can only be at 1 or 0, and if it isn't at 1, then a value of 0 is implied, the statement would have to be an

Code:

else if(p3_sw_trig==0)
You also should not have a semi-colon right after an if or else statement.

Quote:

think you only need one = when referring to the value of the button, not ==.
This is not true. The '==' operator is the equality operator in C, '=' is the assignment operator, which would set the variable equal to what you are comparing it with, and the condition would always be true.

Regarding the syntax error, from a brief glance at the code, it looks like you might be missing a brace or two. Try adding them one by one to the end of your code, and see if that helps.

Hope this helps,

6600gt 14-02-2006 22:15

Re: A few simple programming questions;
 
in the printf statement put in the end ...%d\r",

You can also change the stuff in the quotes to print what every you want.

KenWittlief 14-02-2006 22:55

Re: A few simple programming questions;
 
Quote:

and
is that a line of code or only a comment in your post?

Im almost certain there is no IF AND ELSE statement ?

Keith Watson 15-02-2006 00:13

Re: A few simple programming questions;
 
Quote:

Originally Posted by KenWittlief
is that a line of code or only a comment in your post?

Im almost certain there is no IF AND ELSE statement ?

You are correct. A search shows that syntax was not used in his code.

Mark McLeod 15-02-2006 09:45

Re: A few simple programming questions;
 
I see you have an unmatched bracket here that will give you the syntax error.
Code:

if(p3_sw_trig)
        {

My main suggestion would be to change how you are commenting out lines and blocks of code.
Switch over to the // form rather than use the /* */ form.
Your blocks are ending in random places.

aaeamdar 15-02-2006 10:42

Re: A few simple programming questions;
 
Quote:

Originally Posted by Inverted
Sorry I'm not being as big of a help as I should be, but I'm in the middle of homework and just answering the questions where the solution is on the top of my head.


You don't need (p3_sw_trig == 0) after the else, it's already implied. Also, (this may be wrong because I haven't written code like this in 2 years) I think you only need one = when referring to the value of the button, not ==.


The PWM cable from the Spike goes into one of the Relay outputs on the robot controller. Then you just map which buttons you want to control which relays and you're good. Not sure if you know how to or not, but send me a PM if you want to know how to have one button control the in/out of a piston and such (like if you want a solenoid to act like a festo).


Technically, it's not implied. The code within the segment following the else executes no matter what if the first IF and all IF ELSEs after it fail.

Another correction: == is an operator that returns a one or a zero based on whether the LHS (left hand side) is equal to the RHS. the = operator sets the LHS (which has to be a valid LHS, aka a variable) equal to the RHS. It returns the set value. C will let you get away with this:

if (x = 5)
stuff();

It assumes, "Oh, I know what he wants to do. He wants me to set x to five and then check to see if x or not." AKA, this will not cause a compile time error, whereas in Java it will.

Note that the == returns a 1 or a 0, not any boolean value, for those used to Java or other such. The other comparisons (>, < , <=, >=, etc) work the same way.

Neat but terrible shorthand (don't do this):

x += x < 100;

Which does? I throw it out there.

Paul Dennis

aaeamdar 15-02-2006 10:50

Re: A few simple programming questions;
 
Quote:

Originally Posted by Mark McLeod
I see you have an unmatched bracket here that will give you the syntax error.
Code:

if(p3_sw_trig)
        {

My main suggestion would be to change how you are commenting out lines and blocks of code.
Switch over to the // form rather than use the /* */ form.
Your blocks are ending in random places.

You're absolutely right, Mark. The problem is that the code is poorly written. IE, the original code that they gave us. Whoever writes the code apparently hasn't come to the realization that C++ style comments (//like this) are available in C now. It used to be that /*this*/ was all there was. However, it's bad coding practice to write one liners like this:
Code:

complicatedMethod(); /*Some comment here*/
Because if you want to block comment that line and a bunch of other like it, you will not be able to easily because the */ at the end will also end yours! The easiest way I have found around this without bothering to change the code (i.e. change /**/ to //) is to use this:

Code:

#if 0
complicatedMethod1(); /*Some comment here*/
complicatedMethod2(); /*Some comment here*/
complicatedMethod3(); /*Some comment here*/
#endif

Alright, that's all for now folks. I think cruella wants to make a coat out of my posts.

Paul Dennis

Alan Anderson 15-02-2006 11:42

Re: A few simple programming questions;
 
Quote:

Originally Posted by aaeamdar
Neat but terrible shorthand (don't do this):

x += x < 100;

Which does? I throw it out there.

Ooh, clever. Dangerous, but clever. It increments x unless it's already 100 or greater.
Code:

if (x < 100)
    x = x + 1;

It's true, C does give you enough rope to shoot yourself in the foot.

Mark McLeod 15-02-2006 11:49

Re: A few simple programming questions;
 
Quote:

Originally Posted by KenWittlief
Im almost certain there is no IF AND ELSE statement ?

I agree, it should be no IF, AND, or BUT statements. :)

aaeamdar 15-02-2006 19:27

Re: A few simple programming questions;
 
Quote:

Originally Posted by Alan Anderson
Ooh, clever. Dangerous, but clever. It increments x unless it's already 100 or greater.
Code:

if (x < 100)
    x = x + 1;

It's true, C does give you enough rope to shoot yourself in the foot.

Yes it does. I came up with this one myself... a very interesting mix of metaphors there. I would use the ++ but it's all semantic.

But there is absolutely no reason to do this. It doesn't save you any time, and it doesn't save you an space in memory. However, there is, and I kid you not, a contest to obfuscate code. This is a fairly simple example of that.

Paul Dennis

Keith Watson 15-02-2006 20:11

Re: A few simple programming questions;
 
Quote:

Originally Posted by aaeamdar
Yes it does. I came up with this one myself... a very interesting mix of metaphors there. I would use the ++ but it's all semantic.

I've seen bugs introduced when people use pre or post ++ at the wrong time.

Quote:

Originally Posted by aaeamdar
But there is absolutely no reason to do this. It doesn't save you any time, and it doesn't save you an space in memory. However, there is, and I kid you not, a contest to obfuscate code. This is a fairly simple example of that.

Paul Dennis

I agree with you on making code readable. Years ago I encountered a line of code written by a guy -- it was doing some sort of math but had around 10 different operators in one line including bit and math operators and was impossible to understand. And this was in production software that a team of people had to maintain.

Even something as simple as always using parentheses to establish precendence instead of relying on operator precendence makes code much more understandable.


All times are GMT -5. The time now is 01:35.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi