<SOLVED> why does a small piece of my sketch fail if I put it as a function?

I am just trying various things out and learning programming.
I have a matrix of 4x4 switches on a board like this one
http://www.ebay.co.uk/itm/B-Arduino-4x4-Square-Keyboard-with-16-Push-Buttons-Practical-Keyboard-Module-/371131680163?pt=UK_BOI_Electrical_Components_Supplies_ET&hash=item56692c05a3
I am trying to write a programme to use it, here is a cut down of my full program to date, it works on its own so everything is included.

void setup() {
  // put your setup code here, to run once:
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
Serial.begin(9600);  
}

void loop() {
  // put your main code here, to run repeatedly: 
  digitalWrite (2,LOW);
  digitalWrite (3,LOW);
  if (digitalRead (8) == LOW)
  { 
    pinMode (2, INPUT_PULLUP);
    delay (5);
    pinMode (3, INPUT_PULLUP);
    delay (5);
    pinMode(8, OUTPUT);
    delay (5);
    pinMode(9, OUTPUT);
    delay (5);
//    pinChange;
    delay(5);
    digitalWrite (8,LOW);
    if (digitalRead (2) == LOW)
    {
      Serial.println ("S15");
    }
    if (digitalRead (3) == LOW)
    {
      Serial.println ("S11");
    }
    pinMode(8, INPUT_PULLUP);
    delay (5);
    pinMode(9, INPUT_PULLUP);
    delay (5);
    pinMode(2, OUTPUT);
    delay(5);
    pinMode(3, OUTPUT);
    delay (5);
 }   
}
void pinChange()
{
    pinMode (2, INPUT_PULLUP);
    delay (5);
    pinMode (3, INPUT_PULLUP);
    delay (5);
    pinMode(8, OUTPUT);
    delay (5);
    pinMode(9, OUTPUT);
    delay (5);
}

If I run this, when I press one of the relevant keys I get S11 if I press the other I get S15.
If I comment out the section above the 'pinChange' instruction and clear the '//'s from the pinchange instruction I get the result in the monitor switching between S11 and S15
i.e.
S11
S15
S11
S15
etc.
Can anyone explain what I am doing wrong?

If I comment out the section above the 'pinChange' instruction and clear the '//'s from the pinchange instruction I get the

    pinChange;

might as well be

    47;
   pinChange();

on the other hand, actually calls the function.

The value of pinChange is a pointer to the code of the pinChange function, so what
you've done is place the expression "pinChange" as a statement.

An expression when in the place of a statement is evaluated for side effects only
and the result discarded (unless its the last statement in its function, in which case
the compiler (in some modes) will return the value.

There are no side-effects in evaluating a pointer to code.

The syntax

  pinChange () ;

is a function call statement. The part before the argument list is evaluated to
give a pointer to the function code, and the argument expressions (if any) are
evaluated and then a function call is performed.

If this all sounds complicated, then don't worry, you only have to be aware of it
if passing pointers to functions around explicitly (for instance one of the arguments
to attachInterrupt () is a function pointer:

void my_handler ()
{
  ..
}

void setup ()
{
  attachInterrupt (0, my_handler, CHANGE) ;
  ..
}

Note that my_handler is passed to attachInterrupt - it is not called in setup.

In some other programming languages functions that take no arguments can
be called by just writing "func ;", but not in C/C++

thank you people it looks so obvious when it is pointed out and so embarrassing when missed.
The function works now.
is it acceptable to swop the diection of digital pins in a program like this for short periods?
does it do any damage?
is the delay of 2 milliseconds a good idea or not necessary?
just wondering.

is it acceptable to swop the diection of digital pins in a program like this for short periods?

Yes, the ping sensors require it.

does it do any damage?

No.

is the delay of 2 milliseconds a good idea or not necessary?

Depends on why you are doing it.

is the delay of 2 milliseconds a good idea or not necessary?

Depends on why you are doing it.

in this case :-
The push button matrix has eight pins. Four pins are the four rows and four are the columns. If I send 5v down each column in turn and read each pin from each row in turn each time. I will get a signal each time I press a button and read a pin from the live column. I know the column.
I then swop the direction of the pins and do a similar thing, this will give me the row and therefore the pressed pin.
I am using pins 2345 and 6789 hence the numbering.
It looks a good exersize to work out how to use functions and reduce the program (hopefully) to just one section.
I have added the program with four pins checked, this is the bottom right four i.e. s11,12,15,16 and it works well.
I am still working on it

void setup() {
  // put your setup code here, to run once:
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
Serial.begin(9600);  
}

void loop() {
  // put your main code here, to run repeatedly: 
  digitalWrite (2,LOW);
  digitalWrite (3,LOW);
  {
    if (digitalRead (i) == LOW)
    { 
      pinChange();
      delay (5);
      digitalWrite (8,LOW);
      if (digitalRead (2) == LOW)
      {
        Serial.print ("S15");
      }
      if (digitalRead (3) == LOW)
      {
        Serial.println ("S11");
      }
      pinMode(8, INPUT_PULLUP);
      delay (5);
      pinMode(9, INPUT_PULLUP);
      delay (5);
      pinMode(2, OUTPUT);
      delay(5);
      pinMode(3, OUTPUT);
      delay (5);
      }   
  }
 
  digitalWrite (2,LOW);
  digitalWrite (3,LOW);
  if (digitalRead (9) == LOW)
  { 
    pinChange();
    delay(5);
    digitalWrite (9,LOW);
    if (digitalRead (2) == LOW)
    {
      Serial.println ("S16");
    }
    if (digitalRead (3) == LOW)
    {
      Serial.println ("S12");
    }
    pinMode(8, INPUT_PULLUP);
    delay (5);
    pinMode(9, INPUT_PULLUP);
    delay (5);
    pinMode(2, OUTPUT);
    delay(5);
    pinMode(3, OUTPUT);
    delay (5);
 }   
}
void pinChange()
{
    pinMode (2, INPUT_PULLUP);
    delay (5);
    pinMode (3, INPUT_PULLUP);
    delay (5);
    pinMode(8, OUTPUT);
    delay (5);
    pinMode(9, OUTPUT);
    delay (5);
}

So, you are trying to re-invent the Keypad library? Why not look at how it does it?

So, you are trying to re-invent the Keypad library? Why not look at how it does it?

It looks a good exersize to work out how to use functions and reduce the program (hopefully) to just one section.

Two questions / one answer is not a good ratio.

matelot:
thank you people it looks so obvious when it is pointed out and so embarrassing when missed.

Welcome to the club :slight_smile:

I am converting some code from Ruby to Python. I wasted about 2 hours today trying to figure why something wasn't working - I had forgotten the () - which aren't essential in Ruby.

...R

*thank you Robin * XD and if you think about it Paul I think my one answer answers both your questions, no?

another reason I don’t use libraries in my programs is that I have already opened a thread on here asking how to go about understanding libraries, here

(how DO you hide a link in a single word so all you would see here is here)
and some of the answers I got included

Your fundamental assumption here, that we know what “the library” means, is fatally flawed.

and

I’ve made quite a few libraries, and don’t even bother with the keywords file, so put that down the bottom of your priority list.

I have just spent the last hour using the example ‘customKeypad’ finally got it to work (found out which end you start numbering the pins) and tried to add two lines to use one of the keys, see the last two lines below, nothing.
I assume it is something I am missing but ???

/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','4'},
  {'5','6','7','8'},
  {'9','a','B','c'},
  {'d','e','f','g'}
};
//with the pins down the left side number up
byte rowPins[ROWS] = {2,3,4,5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6,7,8,9}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

void setup(){
  Serial.begin(9600);
}
  
void loop(){
  char customKey = customKeypad.getKey();
  
  if (customKey){
    Serial.println(customKey);
  }
  if (customKey == 4){
    Serial.println("here");
  }
}

in the if test you check to see if the char is 4. In the array holding the values you have '4'. 4 and '4' are not the same thing. Putting the single quotes around it means to interpret it as ASCII.

'4' == 52

matelot:
(how DO you hide a link in a single word so all you would see here is here)

[url=http://www.gammon.com.au/tips]Tips[/url]

The above (not inside code tags) gives: Tips

thank you Delta, it works now. I might just investigate the library a bit more now.
According to Robin I am a club member now so I am allowed to make errors like this (see earlier comment in this thread).

Welcome to the club :smiley:

:grin: :grin:
And thanks again Nick, I have been wondering how that is done.