Go Down

Topic: "Return" question (Read 1 time) previous topic - next topic

creativen

Here I put my code that I assembly by myself looking at the existing program on many website.
And I put it into simulation and it works as I expected.
I would like to developed more complex circuit but I can not find the way, because in my programming I use too many void and it has no return value.
So when it comes to more complicated circuit, the programming is like a mess.
One way I can solve this by use "return", it is just like we go to certain subroutine and after it finish, it will be back to the place which command it to go.

Do you know how I can use "return", surely without void.
It will help me so much to have more structured programming.
I hope you do not suggest me to learn C++ from "Hello World", I hope you can tell me how to make it in the IDE.
Thank you friends!

Best Regards,

Sugianto

creativen

Here I forgot to post the code:

Code: [Select]

void setup();
void loop();
void increment ();
void show();
void sendSerialDatainitial();
int switchPin = 2;
int no=1;

// This pin gets sets low when I want the 595s to listen
const int  g_pinCommLatch = 8;

// This pin is used by ShiftOut to toggle to say there's another bit to shift
const int  g_pinClock     = 12;

// This pin is used to pass the next bit
const int  g_pinData    = 11;

// Definitions of the 7-bit values for displaying digits
byte g_digits [10];

// Current number being displayed
int g_numberToDisplay = 0;

// Number of shift registers in use
const int g_registers = 5;

// Array of numbers to pass to shift registers
byte g_registerArray [g_registers];

void setup()
{
  pinMode (g_pinCommLatch, OUTPUT);
  pinMode (g_pinClock, OUTPUT);
  pinMode (g_pinData, OUTPUT);
 
  // Setup the digits array
  g_digits [0] = 1 + 2 + 4 + 8 + 16 + 32 + 00;
  g_digits [1] = 0 + 2 + 4 + 0 + 00 + 00 + 00;
  g_digits [2] = 1 + 2 + 0 + 8 + 16 + 00 + 64;
  g_digits [3] = 1 + 2 + 4 + 8 + 00 + 00 + 64;         
  g_digits [4] = 0 + 2 + 4 + 0 + 00 + 32 + 64;             
  g_digits [5] = 1 + 0 + 4 + 8 + 00 + 32 + 64;         
  g_digits [6] = 1 + 0 + 4 + 8 + 16 + 32 + 64;
  g_digits [7] = 1 + 2 + 4 + 0 + 00 + 00 + 00;                   
  g_digits [8] = 1 + 2 + 4 + 8 + 16 + 32 + 64;
  g_digits [9] = 1 + 2 + 4 + 8 + 00 + 32 + 64;     
 
  sendSerialDatainitial(g_registers, g_registerArray);
  randomSeed(analogRead(0));
}

void sendSerialDatainitial(   
  byte registerCount,  // How many shift registers?
  byte *pValueArray)   // Array of bytes with LSByte in array [0]
{
    g_registerArray [4] = g_digits [0];
    g_registerArray [3] = g_digits [0];
    g_registerArray [2] = g_digits [0];
    g_registerArray [1] = g_digits [0];
    g_registerArray [0] = g_digits [0];
   
  // Signal to the 595s to listen for data
  digitalWrite (g_pinCommLatch, LOW);
 
  for (byte reg = registerCount; reg > 0; reg--)
  {
    byte value = pValueArray [reg-1];

    for (byte bitMask = 128; bitMask > 0; bitMask >>= 1)
    {
      digitalWrite (g_pinClock, LOW);
   
      digitalWrite (g_pinData, value & bitMask ? HIGH : LOW);
       
      digitalWrite (g_pinClock, HIGH);
    }
  }
  // Signal to the 595s that I'm done sending
  digitalWrite (g_pinCommLatch, HIGH);
}  // sendSerialData


// Simple function to send serial data to one or more shift registers by iterating backwards through an array.
// Although g_registers exists, they may not all be being used, hence the input parameter.
void sendSerialData (
  byte registerCount,  // How many shift registers?
  byte *pValueArray)   // Array of bytes with LSByte in array [0]
{
  // Signal to the 595s to listen for data
  digitalWrite (g_pinCommLatch, LOW);
 
  for (byte reg = registerCount; reg > 0; reg--)
  {
    byte value = pValueArray [reg - 1];

    for (byte bitMask = 128; bitMask > 0; bitMask >>= 1)
    {
      digitalWrite (g_pinClock, LOW);
   
      digitalWrite (g_pinData, value & bitMask ? HIGH : LOW);
       
      digitalWrite (g_pinClock, HIGH);
    }
  }
  // Signal to the 595s that I'm done sending
  digitalWrite (g_pinCommLatch, HIGH);
}  // sendSerialData


void loop()

  if (digitalRead(switchPin))
  {
    blinks();
  }
  delay(100);
}

void blinks() {
  int a = random (1,3);
  int b = random (1,5);
  int c = random (3,5);
  int d = random (1,3);
  int e = random (2,3);
  int f = random (1,4);
  if (a==1)
    {
    digitalWrite(9, HIGH);   // set the LED on
    delay(1000);              // wait for b times a second
    digitalWrite(9, LOW);   
    {increment();}
    }
  else
    {
    digitalWrite(9, LOW);    // set the LED off
    delay(b*1000);              // wait for c times one second
    {blinks();}
    }
   if (c==3)
   {
     digitalWrite(10, HIGH);   // set the LED on
    delay(1000);              // wait for b times a second
    digitalWrite(10, LOW);   
    {increment();}
    }
  else
    {
    digitalWrite(10, LOW);    // set the LED off
    delay(b*1000);              // wait for c times one second
    {blinks();}
    }
     
}

void increment()
{
  g_numberToDisplay = no;
  no=no+1;
  show(g_numberToDisplay);
  delay(500);
  }

void show(int no)
{
  if (g_numberToDisplay < 10)
  {
    g_registerArray [4] = g_digits [0];
    g_registerArray [3] = g_digits [0];
    g_registerArray [2] = g_digits [0];
    g_registerArray [1] = g_digits [0];
    g_registerArray [0] = g_digits [g_numberToDisplay];
  }
  else if (g_numberToDisplay < 100)
  {
    g_registerArray [4] = g_digits [0];
    g_registerArray [3] = g_digits [0];
    g_registerArray [2] = g_digits [0];
    g_registerArray [1] = g_digits [g_numberToDisplay / 10];
    g_registerArray [0] = g_digits [g_numberToDisplay % 10];
  }
  else if (g_numberToDisplay < 1000)
  {
    g_registerArray [4] = g_digits [0];
    g_registerArray [3] = g_digits [0];
    g_registerArray [2] = g_digits [g_numberToDisplay / 100];
    g_registerArray [1] = g_digits [(g_numberToDisplay % 100) / 10];
    g_registerArray [0] = g_digits [g_numberToDisplay % 10];
  }
  else if (g_numberToDisplay < 10000)
  {
    g_registerArray [4] = g_digits [0];
    g_registerArray [3] = g_digits [g_numberToDisplay / 1000];
    g_registerArray [2] = g_digits [(g_numberToDisplay % 1000) / 100];
    g_registerArray [1] = g_digits [(g_numberToDisplay % 100) / 10];
    g_registerArray [0] = g_digits [g_numberToDisplay % 10];
  }
  else
  {
    g_registerArray [4] = g_digits [g_numberToDisplay / 10000];
    g_registerArray [3] = g_digits [(g_numberToDisplay % 10000) / 1000];
    g_registerArray [2] = g_digits [(g_numberToDisplay % 1000) / 100];
    g_registerArray [1] = g_digits [(g_numberToDisplay % 100) / 10];
    g_registerArray [0] = g_digits [g_numberToDisplay % 10];   
  }
  sendSerialData (g_registers, g_registerArray);
  delay (1000);
} // loop

bibre

Just define your function as follows: (assume you need to return an Int from a function)

Code: [Select]

Int myFunction (any arguments)
{
    if (condition is true)
    {
        return 1;    // or any other value you want
    }
  else
    {
         return 2;    // or any other value you want
    }
}


So if the condition is true you get a 1 returned to the calling Sketch otherwise you get a 2. You may y return as many values as you need by using several return statements or just one return using a variable with the right value.
Billy     http://www.z-world.com/operations/gbremer/

When you've eliminated the impossible,
whatever remains, however improbable, must be

WizenedEE


Just define your function as follows: (assume you need to return an Int from a function)


Big assumption; most people will want to return an int.

creativen

To:WizenedEE

Could you give example by defining one of my function?
I much learn by example.
Thank you best!

Go Up