Show Posts
Pages: 1 [2] 3 4 ... 1852
16  Using Arduino / Programming Questions / Re: what does this statement mean? from virtualwire library on: July 25, 2014, 03:41:26 pm
Quote
I would have assumed it made them both pointers.
Assume makes an ass out of you and me.
17  Using Arduino / Programming Questions / Re: switches within a counter sketch on: July 25, 2014, 03:36:25 pm
Look at it.
Does it look like any other "if" you've ever seen?
18  Using Arduino / Programming Questions / Re: switches within a counter sketch on: July 25, 2014, 03:26:43 pm
Code:
if (digitalRead(ACWSwitchPin) == HIGH);
Oops
19  Using Arduino / Programming Questions / Re: what does this statement mean? from virtualwire library on: July 25, 2014, 03:24:37 pm
Code:
//adds 5 to an integer
void add5( int& n )
{
    n += 5;
}
int number = 0;
add5( number);

Just when you thought you'd got the hang of pointers, C++ comes along and introduces Mr Reference.  smiley-grin


Quote
I would personally keep the * with the type, to be consistent with cases like the one you ran into.

That's a tricky one - consider
Code:
int a, b;
Declares two "into" variables.
Now, how about
Code:
int* a, b;
What does that do?

The * goes with the variable, not the type.
20  Using Arduino / Programming Questions / Re: what does this statement mean? from virtualwire library on: July 25, 2014, 03:16:14 pm
Quote
To me a variable and a memory address seems like the same thing.
Now, how about a variable that contains a 100 character string?
21  Using Arduino / Programming Questions / Re: switches within a counter sketch on: July 25, 2014, 03:03:14 pm
Quote
but its not working
Grrrr.

(And I don't just mean the missing apostrophe and code tags)

You've got three virtually identical functions differing only by a pin number.
This is BASIC thinking. I posted a way around this earlier.
22  Using Arduino / Programming Questions / Re: what does this statement mean? from virtualwire library on: July 25, 2014, 03:01:31 pm
You're thinking way too small
23  Using Arduino / Programming Questions / Re: what does this statement mean? from virtualwire library on: July 25, 2014, 12:40:35 pm
Quote
I however do not see the use of a pointer?
Pointers are massively useful and are fundamental to C programming- remember my earlier house/letter/address analogy?

Imagine you have a pointer to a big structure in memory, and you have to pass that structure to a function.
You could make a byte-for-byte copy of that structure (time and memory bandwidth expensive), pass it to the function on the stack, which would operate on it and pass the copy back to the caller which would then have to copy it back to where it belongs.
(That's giving the house to the postman)

Or, you could pass the address of the structure into the function and allow the function to access the memory the structure occupies in situ.
24  Using Arduino / Programming Questions / Re: Converting Analog values to 12bits on: July 25, 2014, 09:38:51 am
"0b" introduces a binary number, and "0x" introduces a hex number.
0xFFF has the decimal value 4095 (212 -1)
25  Using Arduino / Audio / Re: program arduino to send data using a radio commercial. on: July 25, 2014, 09:36:52 am
I'm not clear what you want to do - can you expand a little, please?
26  Using Arduino / Programming Questions / Re: print(negativeInt ,BIN) not working as expected on: July 25, 2014, 09:27:21 am
No, it isn't a bug.
There are only so many overloads for "print", and "int"s are printed as "long".
27  Using Arduino / Programming Questions / Re: 2 inputs controlling 2 outputs on: July 25, 2014, 08:21:29 am
From my dim recollection of any kind of BASIC, setting up subroutines was harder and much less flexible than in C.
Take this routine from the code you posted above
Code:
boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}
(OK, we'll ignore that it uses the hated "delay()" for now)

One tiny change makes it more general-purpose
Code:
boolean debounce(byte pin, boolean last)
{
  boolean current = digitalRead(pin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(pin);
  }
  return current;
}


I'm tempted to rewrite the example, using a full-blown state machine, but I think for students, it is often useful to show them how to do something one way, let them play with it and discover for themselves the limitations, then show them the better way.
(I teach SCUBA diving, so I don't normally use this approach!)
Sometimes, for simple stuff (like a couple of switches and a couple of outputs), the Rolls Royce solution isn't necessary.
28  Using Arduino / Programming Questions / Re: 2 inputs controlling 2 outputs on: July 25, 2014, 07:27:13 am
Code:
const byte greenSwitchPin = 2;
const byte redSwitchPin   = 4;
const byte greenLEDpin    = 8;
const byte redLEDpin      = 13;

boolean lastGreenState;
boolean lastRedState;
boolean greenLEDstate;  // true == ON, false == off.
boolean redLEDstate;

void setup()
{
  pinMode(greenSwitchPin, INPUT_PULLUP);
  lastGreenState = digitalRead (greenSwitchPin);
  pinMode(redSwitchPin, INPUT_PULLUP);
  lastRedState = digitalRead (redSwitchPin);

  pinMode(greenLEDpin, OUTPUT);
  pinMode(redLEDpin, OUTPUT);
  writeLED (greenLEDpin, greenLEDstate);
  writeLED (redLEDPin, redLEDstate);
}

void writeLED (const byte pin, boolean state)
{
#ifdef COMMON_ANODE
  digitWrite (pin, state ? LOW : HIGH);
#else 
  digitWrite (pin, state ? HIGH : LOW);
#endif
}
29  Using Arduino / Programming Questions / Re: 2 inputs controlling 2 outputs on: July 25, 2014, 06:58:36 am
But if we post a solution here, won't the students see it too?
30  Using Arduino / Programming Questions / Re: simple math went wrong!!! help on: July 25, 2014, 05:45:04 am
Sounds like you're wrapping on an "int" (-32768 to +32767)
Post your code.
Pages: 1 [2] 3 4 ... 1852