Go Down

Topic: Multiple in/outputs from functions (Read 479 times) previous topic - next topic


Jan 11, 2013, 05:15 pm Last Edit: Jan 11, 2013, 05:42 pm by HazardsMind Reason: 1
I want to make functions that can get in and put out data for multiple pins at once and try to make it a use able library function.

pins[] = {2,3,4,5,6};
DataOut[] = digitalMultiRead(pins[]);

Example, DataOut[] result => HIGH,LOW,LOW,HIGH,LOW

This is goes in the wiring_digital.c in the core files.
I can't test it myself right now, but im almost certain it WON'T work, so if anyone can try it and respond back that would be great.
If it doesn't work, that fine. I just wanted to try to make a function without having to make it in my actual sketch.

Code: [Select]
int digitalMultiRead(uint8_t pin[]) 
  int i = cnt = 0; //fixed
  int Out[cnt]; //Edit, missed that
  while(pin[i] != NULL)i++;

for( cnt = 0; cnt <=i; cnt++)
   uint8_t timer[cnt] = digitalPinToTimer(pin[cnt]);
   uint8_t bit[cnt] = digitalPinToBitMask(pin[cnt]);
   uint8_t port[cnt] = digitalPinToPort(pin[cnt]);

   if (port[cnt] == NOT_A_PIN) return LOW;

   // If the pin that support PWM output, we need to turn it off
   // before getting a digital reading.
   if (timer[cnt] != NOT_ON_TIMER) turnOffPWM(timer[cnt]);

   if (*portInputRegister(port[cnt]) & bit[cnt]) Out[cnt] = HIGH;
   Out[cnt] = LOW;
return Out[];
My GitHub:


The Out[] variable is on a stack and when you exit the code, it is gone.

You should pass a pointer (Out) to the function or you could make Out[] static (a wasteful way of doing things).

btw, 'i'  is uninitialized too.

My suggestion would be to start simple and then go fancy if you want.


A basic framework for what you want to do would be this:

Code: [Select]

void digitalXRead(unsigned char * pin, unsigned char *pout, unsigned char pins) {
  while (pins) {
    pins -=1;

You may be able to add additional features (returning -1 to indicate error for example), but the gist is there.


My GitHub:


Code: [Select]
void digitalXRead(unsigned char * pin, unsigned char *pout, unsigned char pins) {
This makes some undocumented assumptions that you must be aware of before using this function.
The art of getting good answers lies in asking good questions.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131