Show Posts
Pages: 1 ... 100 101 [102] 103 104 ... 360
1516  Using Arduino / Project Guidance / Re: What else do i need? Start two fans every 30 minutes on: November 30, 2013, 08:25:36 am
It fails to mention the concept of blocking and non-blocking code.

I did this for another thread using BWD to show more verbose ternary and bit manipulation command words, ie not using ? and : and replacing ^= 1 with a word. But maybe you'll like some of this, it's got a twist and shows debug data.

Code:
// Variable ON/OFF BlinkWithoutDelay

#define IFTRUE ?
#define NOTTRUE :
#define FLIPBIT0 ^= 1

unsigned long tNow, tStart, tOn, tOff; // BlinkWithoutDelay timer
unsigned long tOffOn[ 2 ] = { 1500, 500 }; // [0] is time OFF, [1] os time ON

byte ledPin = 13;
short tf = 1; // tf is my test true flag for IFTRUE - NOTTRUE

void setup( void )
{
  pinMode( ledPin, OUTPUT ); // default LOW

  Serial.begin( 9600 );
  Serial.print( "\n testing tf\n tf = " );
  Serial.println( tf );
  Serial.print( " tf IFTRUE 100 NOTTRUE -1 returns " );
  Serial.println( tf IFTRUE 100 NOTTRUE -1 );
}

void loop( void )
{
  static byte  reportLines = 0;

  tNow = millis();
  if ( tNow - tStart >= tOffOn[ tf ] ) // is blink OFF or ON time over?
  {
    if ( reportLines < 20 )
    {
      reportLines++;
      Serial.print( " up to " );
      Serial.print( tNow ); // timestamp
      Serial.print( " ms -- led " );
      Serial.println( tf IFTRUE "ON" NOTTRUE "OFF" ); // led state up to timestamp, then it flips
    }

    digitalWrite( ledPin, tf FLIPBIT0 ); // flips bit 0 and writes the pin with the new value
    tStart = tNow; // start the next second where you got the time the last ended
  }
}

1517  Using Arduino / Programming Questions / Re: Touch Screen Password Input on: November 30, 2013, 02:49:47 am
Are you familiar with the term "blocking code"?
1518  Using Arduino / Project Guidance / Re: What else do i need? Start two fans every 30 minutes on: November 30, 2013, 02:45:04 am
Wait for him to want it to react to events and move at the same time. Then say "I toljaso".
1519  Using Arduino / Programming Questions / Re: [Solved] What does this code mean? on: November 30, 2013, 02:42:01 am
Perhaps some defines would help.

Code:
// Variable ON/OFF BlinkWithoutDelay

#define IFTRUE ?
#define NOTTRUE :
#define FLIPBIT0 ^= 1

unsigned long tNow, tStart, tOn, tOff; // BlinkWithoutDelay timer
unsigned long tOffOn[ 2 ] = { 1500, 500 }; // [0] is time OFF, [1] os time ON

byte ledPin = 13;
short tf = 1; // tf is my test true flag for IFTRUE - NOTTRUE

void setup( void )
{
  pinMode( ledPin, OUTPUT ); // default LOW

  Serial.begin( 9600 );
  Serial.print( "\n testing tf\n tf = " );
  Serial.println( tf );
  Serial.print( " tf IFTRUE 100 NOTTRUE -1 returns " );
  Serial.println( tf IFTRUE 100 NOTTRUE -1 );

  tOn = 500;
  tOff = 1500;
}

void loop( void )
{
  static byte  reportLines = 0;

  tNow = millis();
  if ( tNow - tStart >= tOffOn[ tf ] ) // is blink OFF or ON time over?
  {
    if ( reportLines < 20 )
    {
      reportLines++;
      Serial.print( tNow );
      Serial.print( " -- " );
      Serial.println( tf IFTRUE "ON" NOTTRUE "OFF" );
    }

    digitalWrite( ledPin, tf FLIPBIT0 ); // flips bit 0 and writes the pin
    tStart = tNow; // start the next second where you got the time the last ended
  }
}





1520  Using Arduino / Programming Questions / Re: [Solved] What does this code mean? on: November 30, 2013, 01:00:04 am
Well, there's always the risk that building an idiot-proof ecosystem succeeds in breeding better idiots, but I don't really see it that way myself.  No one on this tiny little planet was born an expert.  We all get there through trial and error.  The Arduino project simplifies a lot of things to the bare essentials, which is often seen as a short-circuit to making progress without having a clue how any of it works.  There's truth in that.  On the other hand, I don't think you can enforce curiosity by preserving complexity.  Either a given user will want to expand their knowledge, or they will be satisfied with their limited success and that's that.

Accessibility lowers the barrier to entry for the pragmatists who just want to get something done, but the best thing about Arduinoland is, while it's a walled garden, that garden has an unlocked gate.  You're welcome to come and go any time.  You can stick to flashing LEDs, or you can build automated drones.  You can stick to digitalWrite() and delay(), or you can learn PORTB = ~(1 << PORTB2) | (PINB & (1 << PINB4)) ? (1 << PORTB2) : 0;.  It's as simple or complex as you want it to be -- and if you put in the time and effort to learn it, that knowledge is transferable.  smiley-lol

All good and fine but it doesn't mean throw away everything that would puzzle beginners.
How many beginners do you know that use libraries that they never look inside? That's where to put advanced code and speed-code algorithms with the tricky parts. 
   
1521  Using Arduino / Programming Questions / Re: Touch Screen Password Input on: November 29, 2013, 09:10:30 pm
The compiler either inlines functions or puts minimum, a return address on the stack which eats cycles.

If you run a state machine, your task being in loop() would not be a problem.
I like to blink led 13 as a program running status indicator even when I'm testing something.

I just don't get the part about loop() running other code. What you show never leaves setup().

1522  Using Arduino / Programming Questions / Re: What does this code mean? on: November 29, 2013, 08:54:11 pm
SirNickity, you say that ternary operation "is a very concise way of picking between two (or more) options".  Could you elaborate on the "or more" part.  I seem to remember something like that from Fortran (maybe).  Just curious.

The op itself can only choose between two options, but you can extend that by compounding statements.  Here's an example that determines whether "it's still today" based on whether I've mentally changed days, regardless of wall-clock time:

Code:
uint8_t is_still_today = (have_slept)
                       ? ((just_a_nap) ? true : false)      // Still today if it was just a nap
                       : (up_over_24_hours) ? false : true; // No longer today if it's been an all-nighter

Here's an example that falls through conditions to return a status code based on a signed int.  0 means nothing happened, > 0 means data received, < 0 means there was a specific error, which is passed to the caller:

Code:
int getData () {
    int retval = some_function();
    return (retval == 0) ? ERROR_NOTHING_TO_DO
                         : (retval > 0) ? ERROR_DATA_RECEIVED : retval;
}

In general, compounding statements can get messy and it might be better to expand this to if/else or switch instead.  However, I'll almost always choose the ternary over if/else for simple assignments.  In dynamically-typed langauges like perl and PHP, it can be really handy to do things like this:

Code:
function get_preferences ($uid = NULL) {
    // If the caller passed a UID, look up that user's preferences
    // Otherwise, use system defaults (uid 0 in the database)
    $uid = ($uid === NULL) ? 0 : intval($uid);
   
    $q = Util::getDB()->prepare( "SELECT * from user_prefs WHERE uid = ?" );
    $q->bind_param('i', $uid);
    $q->execute();
   
    return $q->fetch();
}

Even if you, as a non-ternary fan, come across this code, it's pretty evident what it does.  Good comments will help make it obvious, and abuse of syntax and order of operations will make it a tangled, obfuscated mess.  That's the case with just about any language construct.  There's often more than one way to do it, and I'll not hesitate to use different tools (and as you can see in my examples, also different indentation and line-break styles) to help illustrate the purpose and intent, while being as efficient as possible.

You can always hide the scary code in an object definition in a library. Who looks in them at all is probably well enough versed to look it up if they don't already know.

1523  Using Arduino / Programming Questions / Re: Touch Screen Password Input on: November 29, 2013, 05:21:04 pm
Is there something wrong with loop()?
1524  Using Arduino / Programming Questions / Re: Touch Screen Password Input on: November 29, 2013, 02:39:37 pm
Analog Keypad? On a touchscreen? I think this is a misuse of the word analog. It makes no sense to me.

Before you allow unlock, lock or change password you should first require correct password. Set a variable to 0 to flag password not entered and 1 to flag that correct password was entered. Always check that as part of allowing the lock, unlock or password change.

Separate the code that senses events from the code that causes actions. Action code should trigger on values in variables that the event code sets. The value holds the meaning abstracted by the event code. Separating these code sections lets you use complex events to derive meaning without having to fit/nest the action code into the event code. It will make debugging and maintenance including changes much simpler. Code process will be not-tangled (also called spaghetti) see, think and act.

Password should be a sequence of key entries followed by a symbol, * or # to indicate finished entry.

If you keep track of keypresses taken in while watching for an End of Entry symbol then you can check the entry against the correct password.

If they enter other than 4 digits, don't bother to compare. It's easy to count how many digits are entered as they come in. But always clear the last entry before starting a new one.

These are the libraries for AVR C++ that Arduino IDE uses.
http://www.nongnu.org/avr-libc/user-manual/modules.html

Find string.h and in there are the C string commands.
Quote
int strcmp    (    const char *     s1,
      const char *     s2
   )       

Compare two strings.

The strcmp() function compares the two strings s1 and s2.

Returns:
    The strcmp() function returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2. A consequence of the ordering used by strcmp() is that if s1 is an initial substring of s2, then s1 is considered to be "less than" s2.

I could say don't use strings at all but this way you can reuse much of the code to deal with other entry methods.

I see you still use int variables where byte would do. For example there are less than 255 pins. Byte will do.

I see you use delay(). That may cause a missed quick button touch and is not necessary with better code.
The BlinkWithoutDelay example can show how to make more responsive code but here is a link to a full, better, simple explanation of how to make real time code work:
Quote
How to do multiple things at once ... like cook bacon and eggs
http://www.gammon.com.au/forum/?id=11411
1525  Using Arduino / Programming Questions / Re: Arduino using Processing won't start. on: November 28, 2013, 09:31:32 pm
Just please tell me that you understand WHY!
1526  Using Arduino / Programming Questions / Re: What does this code mean? on: November 28, 2013, 03:13:46 pm
IMO, using ternary operators is OK for simple statements, but if they are themselves nested, it becomes quickly very tricky (should I say "unreadable" ? smiley-wink ) .

When taken to extremes, the trade word is "obfuscated". There are even contests.
Given the nature of algorithms, really ingenious code often walks back and forth over the line even with comments.

What can I say? Some Shakespeare isn't immediately obvious either. That doesn't make it poor use of English.
1527  Using Arduino / Programming Questions / Re: What does this code mean? on: November 28, 2013, 10:48:01 am
There are a lot of "no one" who don't need to look it up even if they don't use it all the time.
It's not exactly a new part of C either. It expresses in one line what otherwise takes at least two.

Why bother with switch-case when nested if statements will do?
Why bother with half or more of the C language?
Maybe we should all just switch to BASIC.

But please don't tell me that no one uses code I have seen used for decades!
1528  Using Arduino / Programming Questions / Re: Arduino using Processing won't start. on: November 28, 2013, 10:39:33 am
Thank you for your answer.

Yes by processing I mean the software Processing. I am meant to use it to activate the servo through Arduino.

I don't understand what you mean by set val to something besides H though. When I tried to put serial receive and action part into loop() it was just looping indefinitely. Perhaps you could give me an example of how you would do it?

It's supposed to loop indefinitely.  smiley-lol
That is how over time new inputs from the PC will be noticed and acted upon. That is how real time code works.

What your program does wrong however is not turning the switch (val) off (not 'H') when it moves the servo.

This way below waits for Serial to have an 'H' and acts on it only one time per 'H' received:

if ( Serial.available() )
{
  val = Serial.read();
}

if ( val == 'H' )
{
  move the servo
  val = 0; // so next time through loop this runs ONLY if another 'H' is in Serial
}

If you leave val == 'H' then yes it will keep moving the servo because your code makes it so.
1529  Using Arduino / Programming Questions / Re: Quickly running out of program memory on: November 28, 2013, 09:24:30 am
Don't use an int where a byte will do. If you have 8 flags, pack them into one byte instead of using one byte each. Learn to use C strings (the string.h library) instead of C++ String objects.
Start off with simple projects instead of a seven course banquet....

1530  Using Arduino / Programming Questions / Re: What does this code mean? on: November 28, 2013, 09:18:00 am
I find a lot of references to the ternary operation being "that obscure statement that no one understands"

Which says a lot about how little whoever says that knows! Same person probably thinks that using bits is arcane and can't parse text without using String objects.
Pages: 1 ... 100 101 [102] 103 104 ... 360