Re: Function Error Updated

See below.

To call newPinData you need to write newPinData().
newPinData refers to the address of the function.

Just one of those mistakes that someone else can find in an instant. duh. :blush: compiles fine after that. Thank you.

I am having a problem with this function not working as intended.

I need it to get the pin data and compare it to the last time it was called to see if there was a change in the pin state. If there was it needs to return true. Some reason it will not do that. It is returning true at all times and the pin states are not changing.

int y;
int last_val;

boolean newPinData(){  //boolean that returns true on new pin data or false on same pin data
  int y = getPinData();
  if (y != last_val){
    return true;
    last_val = y;
    Serial.println("Newpindata called and detected");
  }
  else{
    return false;
    Serial.println("Newpindata called and not detected");
  }
}

when you return a value (true/false), you break out of the method completely. Any code below the return will not be executed.

Thank you.

So this is an oddity:

int getPinData(){  // function to evaluate the input pin states and return an int with binary pin states
  int pin_data;
  for (int z=0; z < 4; z++){  
     
     //read the input pins to aquire a data state of 10 digits
     int temp = digitalRead(input[z]);  
      
      //if the read indicates high then set a 1 for that digit
      if (temp){
        bitWrite(pin_data,z,1);
      }
      
      //otherwise if it reads low then set a 0
      else{
        bitWrite(pin_data,z,0);
      }
   }
   Serial.print("getPinData called");
   Serial.println(pin_data, BIN);
   return pin_data;
}

When i set z<2 it will work as expected. I have it set to spit out the number over telnet and I can probe the pins high low and see the change.
When i set z from <3 to <10, what I need, it sends out a 14 bit number of random 1s and 0s. I have had the same code process running to get the number out to a webpage in a seperate sketch and it works fine.
Can anyone think of why?

Relevant bits:

int input[] = {        
 22,23,24,25,26,27,28,29,30,31};

// set pins in the input list as inputs
  for (int x = 0; x < 10; x++){
    pinMode(input[x],INPUT);
  }

What is connected to those 10 pins? Switches with pull-down resistors?

At the moment there is nothing. The pins are setup to be low inputs. I don't have them connected to ground at all, so there is some signal noise. Each pin, 22-31, is wired out to a header, Arduino is 'packaged' and so I am manually testing each input with a 5v signal.

When it sends out the 14 bit number it always looks like this 10000000000000, even though I am only requesting a > 2 bit number. I have tried grounding with probe, but that high bit is not actually a pin. All the other pins register high or low just fine in my telnet output.

int variables are fixed size - 16 bits. You can not "request a 2 bit number". Try the bar down the street with the live band.

With nothing connected to the pins, they float. Reading a pin with nothing connected, its a crap-shoot what you'll get.

Thanks for the jackass comment but without any real idea as to why I see a 14 bit number when a 3 bit number should be sentI have nothing to go on.

If anyone else has anything constructive to add please be my guest.

Not entirely sure if this is your problem but it may be informative to print all the bits of your data. The BIN formatter discards leading zeroes.

Use a function like:

void printInt (int myInt){
  for(word mask = 0x800; mask; mask >>= 1){
    if(mask  & myInt)
      Serial.print('1');
    else
      Serial.print('0');
  }
}

You might consider using unsigned ints to hold bit values to avoid unexpected issues with negatives.

And note that what you are trying to do could be done more easily by using:

bitWrite(pin_data,z,digitalRead(input[z]));

or perhaps even more easily by using port manipulation.

Thanks for the jackass comment

Hey, no problem. You are most welcome.

Thank you I will implement your suggestions and see what happens.

..

arizona55:

int getPinData(){  // function to evaluate the input pin states and return an int with binary pin states

int pin_data;
 for (int z=0; z < 4; z++){

// blah blah

You don't initialize pin_data so it starts with random stuff. I would expect gibberish as a result.

Try:

int getPinData(){  // function to evaluate the input pin states and return an int with binary pin states
  int pin_data = 0;
  for (int z=0; z < 4; z++){  

// blah blah