Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
dbrion0606
PostPosted: Jun 12, 2012 - 12:59 PM
Posting Freak


Joined: Jan 07, 2012
Posts: 1208
Location: North of France

Bonjour, Monsieur Clawson
Yes,
I first read of the term "structured programming" in 1979 (two millenia) .
I also know languages such as R where gotos do not even exist at all (no need to forbid them : people remain happy without goto's)...

But

I had noticed he knew basic and I kow gotos are not forbidden ic C (should be reserved for escaping when an error happens into abyssally deep program structures).

And
one should be indulgent with beginners.... (and even you did not protest whis his goto's)

And
I would be happy to understand what he wants to do, because I am curious and because I am old and silly enough not to understand....
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 12, 2012 - 01:02 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62345
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

one should be indulgent with beginners...

Teaching them bad programming practice?

Even BASIC these days is a structured language since the need for line numbers GOTO and GOSUB were removed. There's simply no excuse for bad programming technique in 2012.

(and yes goto has it's place even in C but it's usually for escaping early from a heavily nested situation when a serious error has occurred (like malloc() returned 0) and should not be considered for "normal" programming)

I was formally taught programming 31 years ago at university using Pascal and even back then the idea of using goto was severely frowned upon - hopefully things have progressed in 30 years.

(anyway you were advocating "goto switch1" when it's actually the name of a function not a goto label so the syntax didn't make sense even if the goto did)

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
dbrion0606
PostPosted: Jun 12, 2012 - 01:21 PM
Posting Freak


Joined: Jan 07, 2012
Posts: 1208
Location: North of France

"anyway you were advocating "goto switch1" when it's actually the name of a function not a goto label so the syntax didn't make sense even if the goto did"

was "switch1" the name of a void function in his mind?

(in my mind, I did not read the () and thought of a badly spelled label..., if he had not spoken about basics, maybe I would have read the "()"...)

Therefore, he might happily remove the gotos here if switch is meant as a void function...

and what makes
while(conditionx);
if (conditiony);
except testing the conditions (x y)?

Is that was he was meaning?

But, if one shows too many errors at a time, that might be discouraging! An error after the other is more efficient.

I was not advocating anything, but before structuring
 
 View user's profile Send private message  
Reply with quote Back to top
david.prentice
PostPosted: Jun 12, 2012 - 01:42 PM
10k+ Postman


Joined: Feb 12, 2005
Posts: 16323
Location: Wormshill, England

@nickelis,

Your C syntax is seriously wrong in many places.
I see this as a minor problem that is easily resolved.

However the first job is to lay your program out neatly even in pseudo-code.
1. Indent blocks of code.
2. Put each statement on a fresh line
3. Add explanatory comments that say what you want to do.

Once you have done this, you can translate into C, Pascal, Java or whichever language you choose.
You can lookup the correct syntax for if() or while() in your textbook, and we will help you to get everything right.

Those (1), (2), (3) steps are your responsibility. Get them sorted and translating into C is trivial.

David.
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
dbrion0606
PostPosted: Jun 21, 2012 - 02:36 PM
Posting Freak


Joined: Jan 07, 2012
Posts: 1208
Location: North of France

Bonjour Nickelis
I found some code which debounces a push button switch here (http://www.elektor.fr/extra/programmation-en-c-des-microcontroleurs-risc-avr.966219.lynkx, download and unzip AVRprog-VF.zip and have a look at the very well written c source in AVRprog\commut2 :
when the bounce switch is high, it waits 50 ms (you prefer 85 ms) and, if it is confirmed, it waits until it gets low... (in the book, the switch is connected to PORTA, but it doesnot seem that complicated to move it where you want)

Perhaps you should have a variable, let it be called "state", whith two values (this "state" increases each time the button is pressed and released, and if it goes too high, it cames back to its lower value)

state 1 : relay is on, LED A is on, led B is off
state 2 : relay is off, LED A is off, led B is on

BTW: perhaps led A and relay might be put together , and I do not understand why your relay needs two inputs .... This would spare 1 or 2 pins and therefore some hardware complexity

I do not understand hou you initialise it (you make a kind of greeting with a LED blinking?) but your topic is interesting (is already in a book if I do not make any misunderstanding) though it seems simple...
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jun 21, 2012 - 03:41 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21272
Location: Orlando Florida

Before expending too much worry about false button presses due to bouncing, maybe just try to get the program running using no debouncing at all, and just limit the loop time of the program to about 30Hz or 50Hz. (33ms or 20ms per loop, well within the carbon based unit's perception of 'crisp response'). Buttons don't bounce more than a couple of times a couple of ms apart, and a 20 ms loop time might just make the problem moot.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
js
PostPosted: Jun 21, 2012 - 11:14 PM
10k+ Postman


Joined: Mar 28, 2001
Posts: 20387
Location: Sydney, Australia (Gum trees, Koalas and Kangaroos, No Edelweiss)

Quote:
I do not understand why your relay needs two inputs ....
A latching relay? 1 input for on 1 input for off.

_________________
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
dbrion0606
PostPosted: Jun 23, 2012 - 04:27 PM
Posting Freak


Joined: Jan 07, 2012
Posts: 1208
Location: North of France

Bonjour js:
well, I wish to thank you for the idea of latching delays (according to wikipedia, it remains in a given state even if not power supplied).

Bonjour Bobgardner:
well, perhaps giving a function which changes states and one of which parameters is the delay to debounce would be versatile enough.
Such a function would be

Code:
// for syntax checking under cygwin and native gcc (gcc -c or gcc -S)
// should be deleted, of course, with avr-gcc
#define PORTB 1
#include <stdint.h>

#define TIME2DEBOUNCE 85 // ms to see whether the switch is dreaming
#define MAXSTATES 2 // Two states are allowed (1 , 2)

uint8_t changestate(uint8_t state, uint8_t behavior, uint8_t push_button,
      uint8_t debouncetime){
   /* push-button is the portsB'sspin
    *
    * behavior is what one wants to do
    * == 1 -> one waits until key is depressed
    * else -> one goes on, the LEDS and the relay being
    * allowed to change states every
    * 1/10 seconds...
    *
    * state is the present state
    */

   uint8_t aux;
   aux = state;
   if (~input_high(PORTB,push_button))  return (state); // do not change state
   delay_ms(debounce_time);
   if (input_high(PORTB,push_button))
   {
      aux = state + 1;
      if (aux > MAXSTATES) aux=1;
   }
   /*
    * some code to account quickly for state changes could be put there
    */
   if (behavior == 1)
   {
      while (input_high(PORTB,push_button)) ;
        }

   return(aux);
}


Once input_high is defined, perhaps it might work... or there is a better push button "manager"....
 
 View user's profile Send private message  
Reply with quote Back to top
dbrion0606
PostPosted: Jun 23, 2012 - 04:33 PM
Posting Freak


Joined: Jan 07, 2012
Posts: 1208
Location: North of France

@me
s/debounce_time/debouncetime/ and be sorry for posting before syntax checking...
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits