Go to Post Congratulations! Welcome to the hardest fun you'll ever have. - Undertones [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 03-02-2006, 17:36
Calvin Calvin is offline
Registered User
no team
 
Join Date: Feb 2005
Location: Portland OR
Posts: 105
Calvin has a spectacular aura aboutCalvin has a spectacular aura about
Variable question (solved)

OK i'm going to be sitting by my computer refreshing every 30 seconds checking for reply

In file user_routines.c, under

/***DEFINE USER VARIBLES AND INITIALIZE THEM HERE ***/
I added:
static unsigned int bob = 2;

------------------------------------------------------------
------------------------------------------------------------
Now under the:
Void Default_Routine(void)
{
if(bob == 2 && p1_sw_trig){bob = 0;}
if(bob == 0 && p1_sw_trig){bob = 1;}
if(bob == 1 && p1_sw_trig){bob = 0;}

}

------------------------------------------------------------
------------------------------------------------------------
Before I click on the trigger, I get the value 2 for bob...
Click on it once, I get 0
click on it again I get 0(it's suppose to change to 1)

WHAT AM I DOING WRONG!?!!?
Please help!

Last edited by Calvin : 03-02-2006 at 22:26.
  #2   Spotlight this post!  
Unread 03-02-2006, 18:06
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 574
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
Re: Beginning programmer: Variable question HELP!!!

Your problem is that you set bob to 1 and then check to see if it is 1 and the button is pressed and set it back to 0.
Code:
if(bob == 2 && p1_sw_trig)
{
  bob = 0;
}
/** assuming bob == 0, bob is set to 1 ****/
if(bob == 0 && p1_sw_trig)
{
  bob = 1;
}
/** bob is now 1 and gets set to 0 ****/
if(bob == 1 && p1_sw_trig)
{
  bob = 0;
}
/*** bob is always 0 if the trigger has been pressed ***/
You can fix this with by using an else if.
Code:
static unsigned int bob = 2;

Void Default_Routine(void)
{
  if(bob == 2 && p1_sw_trig)
  {
    bob = 0;
  }
  else if(bob == 0 && p1_sw_trig)
  {
    bob = 1;
  }
  else if(bob == 1 && p1_sw_trig)
  {
    bob = 0;
  }
}
Once you do that you're going to run into the problem that the state is constantly toggling between 0 and 1 when the button is pressed.

This is because there is no way that you as a user will be able to tap the trigger for 1 loop (1/42 of a second) and the trigger will actually be pressed for multiple loops.

What I think you want to do is keep track of the previous button state and only change states if your current state is pressed and your previous is not.
  #3   Spotlight this post!  
Unread 03-02-2006, 18:08
RIgnazio RIgnazio is offline
Registered User
no team
Team Role: Webmaster
 
Join Date: Jan 2005
Rookie Year: 2004
Location: .
Posts: 45
RIgnazio will become famous soon enough
Re: Beginning programmer: Variable question HELP!!!

Quote:
Originally Posted by Calvin
OK i'm going to be setting by my computer refreshing every 30 seconds checking for reply

In file user_routines.c, under

/***DEFINE USER VARIBLES AND INITIALIZE THEM HERE ***/
I added:
static unsigned int bob = 2;

------------------------------------------------------------
------------------------------------------------------------
Now under the:
Void Default_Routine(void)
{
if(bob == 2 && p1_sw_trig){bob = 0;}
if(bob == 0 && p1_sw_trig){bob = 1;}
if(bob == 1 && p1_sw_trig){bob = 0;}

}

------------------------------------------------------------
------------------------------------------------------------
Before I click on the trigger, I get the value 2 for bob...
Click on it once, I get 0
click on it again I get 0(it's suppose to change to 1)

WHAT AM I DOING WRONG!?!!?
Please help!
Try this:
Quote:
Void Default_Routine(void)
{
static unsigned int trigger_count = 0;

if(p1_sw_trig)
{
++trigger_count; //sets a trigger counter to control the value
}
else
{
}
if(trigger_count > 1) //Change as needed
{
trigger_count = 0; //Resets trigger count
}
else
{
}

if(bob == 2 && p1_sw_trig && trigger_count == 0)
{
bob = 0;
}
else if(bob == 0 && p1_sw_trig && trigger_count == 1)
{
bob = 1;
}
else
{
}

}

Last edited by RIgnazio : 03-02-2006 at 18:11.
  #4   Spotlight this post!  
Unread 03-02-2006, 18:08
Unsung FIRST Hero
Mike Betts Mike Betts is offline
Electrical Engineer
no team
Team Role: Engineer
 
Join Date: Dec 2001
Rookie Year: 1995
Location: Homosassa, FL
Posts: 1,442
Mike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond repute
Re: Beginning programmer: Variable question HELP!!!

It takes about 400 to 600 nanoseconds (maybe a bit more) for the three IF statements to execute. You cannot hit the trigger fast enough to satisfy only one IF statement.

The first time through, bob goes from 2 to 0.

The next time though, bob goes from 0 to 1 to 0.

The code is doing exactly what you told it to do...

Regards,

Mike
__________________
Mike Betts

Alumnus, Team 3518, Panthrobots, 2011
Alumnus, Team 177, Bobcat Robotics, 1995 - 2010
LRI, Connecticut Regional, 2007-2010
LRI, WPI Regional, 2009 - 2010
RI, South Florida Regional, 2012 - 2013

As easy as 355/113...
  #5   Spotlight this post!  
Unread 03-02-2006, 18:12
Calvin Calvin is offline
Registered User
no team
 
Join Date: Feb 2005
Location: Portland OR
Posts: 105
Calvin has a spectacular aura aboutCalvin has a spectacular aura about
Re: Beginning programmer: Variable question HELP!!!

wow thanks for the quick reply,
I figured out a way to do it with two buttons. Now it works fine.

I completely forgot about the quick loop, and started questioning my programming skills.

PHP Code:
/////**** Start of RELAY 7 code ****/////
    
if(bob7 == 0)
    {
       
printf("Bob7 = 0\r");
       
relay7_fwd 1;
    }
    if(
bob7 == 1)
    {
       
printf("Bob7 = 1\r");
       
relay7_fwd 0;
    }
    if(
bob7 == 2){printf("Bob7 = 2\r");}
    
    if(
bob7 == && p1_sw_trig){bob7 0;}
    if(
bob7 == && p1_sw_top){bob7 1;}
    if(
bob7 == && p1_sw_trig){bob7 0;}
/////**** End of RELAY 7 code ****///// 

Last edited by Calvin : 03-02-2006 at 22:29.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
VEX programming Gene F Programming 14 08-08-2006 22:21
Printf Oddity : Programming Challange DarkAlex Programming 7 21-03-2005 08:12
have a question to ask about Double Solenoids to one spike programming davelu Programming 3 22-02-2005 09:52
General programming question mikesown Programming 1 07-02-2005 16:09
'Fix It Window' and Programming.... JMac Programming 19 25-01-2005 18:57


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

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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