Show Posts
Pages: 1 2 3 [4] 5 6 ... 24
46  Using Arduino / Programming Questions / Re: Passing a function another function on: February 10, 2013, 08:52:18 am
@Jim_Socks

I seem to remember some quirkyness to Arduino typedefs, boiling down to requiring that certain functions must be "prototyped" .  Here is some working code that shows the technique you are looking for.  If you remove the "Arduino Quirk" prototype line, the compile will fail

Code:
typedef void (*drinkFun)(void);

void glasscheck( drinkFun fptr);  // Arduino quirk; must be prototyped

void glasscheck( drinkFun fptr){
    // ...
    fptr();
    // ...
}

void VodkaOnRocks(void) {
    Serial.println("Vodka On Rocks Fun");
}
void Margarita(void) {
    Serial.println("Margarita Fun");
}


void setup(){
   Serial.begin(19200);
   glasscheck( VodkaOnRocks);
   // or
   drinkFun rita=Margarita;
   glasscheck( rita);
}


void loop(){}

Cheers,
John

edit: renamed functions for domain
47  Using Arduino / Programming Questions / Re: Problem with setSyncProvider() [from Time Library] on: February 09, 2013, 02:51:08 pm
Is this part of the standard example that comes with 1.0.3?

P.S. I downloaded 1.0.3 and confirmed there is no Time library that "comes with" the IDE.  The most current/recommended Time library appears to be http://playground.arduino.cc/Code/Time which does not include the adjustDstEurope in it's example.  But it does require an additional library, "udpbytewise"

John
48  Using Arduino / Programming Questions / Re: Problem with setSyncProvider() [from Time Library] on: February 09, 2013, 01:47:13 pm
Hi purehunter,

I believe your problem is in adjustDstEurope(), it works if I remove the call from getNtpTime as so:
Code:
     unsigned long epoch = secsSince1900 - seventyYears ;  // removed + adjustDstEurope();

adjustDstEurope is calling time library functions including month() and day().  It would not suprise me if this is causing recursive calls to getNtpTime, which would explain why it "never returns" once you call setSyncProvider in setup.

Is this part of the standard example that comes with 1.0.3?

Cheers,
John
49  Using Arduino / Programming Questions / Re: Problem with setSyncProvider() [from Time Library] on: February 09, 2013, 11:37:54 am
Quote
So what is the "flawed fundamental assumption" that you think he is making?  Maybe I am making it too.
That setSyncProvider() needs to be called every time a sync is to occur, and that that is the only time the sync occurs.

Well, that's not an assumption I am making.  But I can't speak for purehunter...
50  Using Arduino / Programming Questions / Re: Help Understanding Code: processInput on: February 09, 2013, 11:00:38 am
I remember seeing this code when you posted it before because the variable name just cracks me up smiley

Code:
void processNumber (int x)
{

  switch (whichNumber)
  {

  case CASE:
    Casev = x;
    whichNumber = Speed;
    break;

  case Speed:
    Speedv = x;
    whichNumber = CASE;
    break;


  }



//-------------------------------------------------------------------

void processInput ()
{
  static int receivedNumber = 0;
  static boolean False = false;
  byte c = Serial.read ();   

  switch (c)
  {

  case EOP: 
    if (False)
      processNumber (- receivedNumber);
    else
      processNumber (receivedNumber);

    // fall through to start a new number
  case SOP:
    receivedNumber = 0;
    False = false;
    break;

  case '0' ... '9':
    receivedNumber *= 10;           // receivedNumber = recievedNumber*10 increments decimal places
    receivedNumber += c -'0';      //receivedNumber = recievedNumber + (c - '0') goes from char to decimal         
    break;

  case '-':           // What is this for?
    False = true;
    break;

  }
}

I have enough grasp on this code to use it but it bugs me that I do not fully understand it.
Is processInput a special function in the Arduino language?
What is the case '-' for? Does this just keep the received number positive?
processInput is not "Arduino language", it is defined right there in your code.
It examines the current character coming in, and if it is a '-', it takes note that the number that follows should be a negative number.  If the variable were named "isNegativeNumber", it would be clearer.

seealso http://arduino.cc/forum/index.php/topic,145379.msg1092845.html#msg1092845
51  Using Arduino / Programming Questions / Re: Problem with setSyncProvider() [from Time Library] on: February 09, 2013, 09:45:41 am
Quote
My time library does appear to attempt a sync at the time setSyncProvider is called, so as far as I can tell your code should work.
But not JUST when setSyncProvider is called.

Yes, expectation is probably that it be called every 5 sec per
Code:
setSyncInterval(5); // Set seconds between re-sync (5s for test only)


So what is the "flawed fundamental assumption" that you think he is making?  Maybe I am making it too.
52  Using Arduino / Programming Questions / Re: Problem with setSyncProvider() [from Time Library] on: February 09, 2013, 09:02:34 am
...
Below the code I printed the serial output. In this example two times the setting of the clock did not work, the third try runs and the clock output begins.
Code:
unsigned long getNtpTime()
{
  sendNTPpacket(timeServer); // send an NTP packet to a time server
    delay(500);
   
    if ( Udp.parsePacket() ) {
     ... guts ...
     return epoch;
     }
  return 0; // return 0 if unable to get the time
}

In order to succeed, getNtpTime must get an answer back with 0.5 sec-- I wonder if this is enough to be reliable?  The examples that came with the Time library I am using uses delay(1000) here, and is different in other ways.  Which Time library are you using?  I believe I am using http://playground.arduino.cc/Code/Time.

My time library does appear to attempt a sync at the time setSyncProvider is called, so as far as I can tell your code should work.

John
53  Using Arduino / Programming Questions / Re: syntax question on: February 09, 2013, 01:15:22 am
so I'm getting numbers like B111 and B100 and my cube is 3x3x3 so wouldnt it need more bits to represent all 9 led's?

Which 9 leds?

Sorry, let me put it differently.  ledcol only varies from 0 to 2.  There are only 3 "columns"...
54  Using Arduino / Programming Questions / Re: syntax question on: February 09, 2013, 12:13:11 am
Code:
    // load current plane pattern data into ports
        ledpin = 0;
        for (ledrow=0; ledrow<CUBESIZE; ledrow++) {
          for (ledcol=0; ledcol<CUBESIZE; ledcol++) {
            digitalWrite( LEDPin[ledpin++], PatternBuf[patbufidx] & (1 << ledcol) );
          }
          patbufidx++;
        }

The digitalWrite here is going through each bit of the current location in the PatternBuff to decide whether to turn the corresponding LED on  (1) or off (0).

The (1 << ledcol) is the "mask" which would specify which bit in PatternBuf[patbufidx] to look at.

HTH?
John
55  Using Arduino / Programming Questions / Re: Is there an easier way to extract a value from a string? on: February 09, 2013, 12:06:15 am
If your format is quite fixed, don't overlook this possibility:
Code:
    char s[] = "      <description>030 W/m2</description>";
    int d = atoi(s+10);  // extract integer beginning at s[10]

John
56  Using Arduino / Programming Questions / Re: syntax question on: February 09, 2013, 12:00:29 am
It's not a comparision, << is a bit shift operator.  And & is the bit "and" operator. http://playground.arduino.cc/Code/BitMath

I can't quite get the meaning of the one line of code you posted though.  Perhaps if you posted more of the surrounding code?

Cheers,
John
57  Using Arduino / Programming Questions / Re: Need help understanding this piece of code (ArduPlane) on: February 08, 2013, 11:49:58 pm
"Object-oriented programming (OOP) is a programming paradigm that represents concepts as "objects" that have data fields (attributes that describe the object) and associated procedures known as methods. Objects, which are usually instances of classes, are used to interact with one another to design applications and computer programs.[1] [2]"  -- wiki

In c++, for you right now, a struct is basically the same as a class.

Cheers,
John
58  Using Arduino / Programming Questions / Re: Need help understanding this piece of code (ArduPlane) on: February 08, 2013, 11:36:38 pm
Could be a struct or a class.  Either way, configure_msg() is a method "on" the object:
Code:
struct G {
  Camera camera;
};
struct Camera {
  void configure_msg(int msg){}
};
...

G g;
g.camera.configure_msg(msg);

HTH,
John
59  Using Arduino / Programming Questions / Re: Simple counting problems on: February 07, 2013, 07:10:09 am

If the condition is met.

I want the pin to go high , ++count,then go low after a time..

Is this a bracket issue?
Yes
Quote
is it only the line directly below the condition that is linked, and how do i add more...

if (condition)
   statement;

where statement is either one line like
   digitalWrite( 13, HIGH);

or multiple like
   {
       digitalWrite(13, HIGH);
       count++;
       ... etc
   }

http://arduino.cc/en/Reference/If

Cheers,
John
60  Using Arduino / Programming Questions / Re: RealTimeClock does not allow a date year > 2012! on: February 04, 2013, 06:46:09 pm
Hmm there does seem to be a bug with the set(int, int) method:
Code:
 case DS1307_MTH:
    if(v<13 && v>-1)
    {
rtc_bcd[DS1307_MTH]=((v / 10)<<4) + (v % 10);
    }
    break;
  case DS1307_YR:
    if(v<13 && v>-1)
    {
rtc_bcd[DS1307_YR]=((v / 10)<<4) + (v % 10);
    }
    break;
  } // end switch

Looks like they may have just (inadvisably) copied that conditional from the set-month case.  You could change that line in your DS1307.cpp file to something like
Code:
if (v<100 && v>-1)    // n.b. this will break in year 2100

That seems to be a different RTC library than what came with my Time library (1.0.2)

Cheers,
John
Pages: 1 2 3 [4] 5 6 ... 24