Pages: [1] 2   Go Down
Author Topic: "Return" question  (Read 1012 times)
0 Members and 1 Guest are viewing this topic.
universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here I forgot to post the code:

Code:
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
Logged

Monterrey, N.L. México
Offline Offline
Full Member
***
Karma: 1
Posts: 154
Model Railroading & Arduino are Fun
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

Billy     http://www.z-world.com/operations/gbremer/

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

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

To:WizenedEE

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

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 69
Posts: 2165
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is a few ways of returning values from a function

Code:
void AddNumbers( int a, int b, int &result )
  {
    result = a + b;
    return;
  }

void AddNumbers( int a, int b, int *result )
  {
    *result = a + b;
    return;
  }

int AddNumbers( int a, int b )
  {
    return a + b;
  }

void setup()
  {
    int a = 4;
    int b = 5;    
    int result;

    //Version 1
    AddNumbers( a, b, result );

    //Version 2
    AddNumbers( a, b, &result );

   //Version 3
   result = AddNumbers( a, b );
Logged


universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

what is the different &result and *result?

I thought void setup() should be in the beginning

Code:
void setup()
  {
    int a = 4;
    int b = 5;   
    int result;

    //Version 1
    AddNumbers( a, b, result );

    //Version 2
    AddNumbers( a, b, &result );

   //Version 3
   result = AddNumbers( a, b );

void AddNumbers( int a, int b, int &result )
  {
    result = a + b;
    return;
  }

void AddNumbers( int a, int b, int *result )
  {
    *result = a + b;
    return;
  }

int AddNumbers( int a, int b )
  {
    return a + b;
  }

The code execute the version 1 first and return to it again and execute the version 2,etc, am I right?
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 69
Posts: 2165
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

setup can be at the start, the IDE generates the function prototypes at the top of the file when you compile.

in a function paramater list:

& means pass by reference, rather than by value. ( if the function modifies the variable, it modifies the original passed variable, not a copy )

* means the variable is actually a pointer. ( address to variable ), the * in the code has different meanings, it converts/dereferences the address to the variable ( original passed variable )

when the function is called 'AddNumbers( a, b, &result );' the & operator gets the address of result ( pointer ), the compiler will call 'void AddNumbers( int a, int b, int *result )' as the result paramater is a pointer


Each version produces the same result, just different ways of doing it.

« Last Edit: March 05, 2012, 09:28:29 pm by pYro_65 » Logged


Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what is the different &result and *result?

If you want to know more, you should read up on pointers and references. However, they're more "advanced" topics and are definitely not necessary for what you're trying to do.
Quote
I thought void setup() should be in the beginning
It doesn't matter where functions are defined. Functions are only executed when they are called. The exception is the main function, which then calls setup and loop

Quote
Code:
int AddNumbers( int a, int b )
  {
    return a + b;
  }

This is the best way to solve your problem.

Quote
Code:
{increment();}

This is technically legal, but not how you should be doing things. Braces ( these: { ) are only needed to constrain a "block" -- usually an if, while or for statement. You should just do this:
Code:
increment();
Logged

universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Quote
I thought void setup() should be in the beginning

Quote
It doesn't matter where functions are defined. Functions are only executed when they are called. The exception is the main function, which then calls setup and loop


Hmm, so for example in my code void increment(), so it will be executed when it is called? I thought all the codes will be executed from above all to down?


Hmm, how to make it has return value:
Code:
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); [color=red]// I would like to replace it by return value, so I can have more structured code[/color]
  delay (1000);
}
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 69
Posts: 2165
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The code inside a function runs one line at a time from top to bottom.
Functions can be called in any order.

What do you want that function to return. It modifies variables that are available elsewhere and sends data.
You also don't use the 'int no' passed to the function.
Logged


universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

could you show me how you modify it?
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 69
Posts: 2165
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Change your current declaration from

Code:
void show(int no)
{
  //your code
}

to something like

Code:
bool show( void ) //You don't use 'int no'
{
  //your code
  if( 'success' ){ //Replace 'success' with some real code
    return true;
  }else{
    return false;
  }
}

But like I asked, what do you want to return, an unused return value is a waste.
The function modifies data that you can already access, if you can check that the send happened correctly, you could return like my example above. A void function ( method ) is fine.
« Last Edit: March 05, 2012, 10:20:00 pm by pYro_65 » Logged


Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The type before the name of your function is what it returns. "void" means "no type," so saying "void loop()" means "I'm defining a function which takes no arguments and returns nothing" and "char read()" means "I'm defining a function which takes no arguments and returns a char" and "bool isWithinRange(int x)" means "I'm defining a function which takes an integer and returns a bool"

So to make your function return something, change the void to bool. Then, in the body of the function, you have to honor your promise and actually return something. (Say: "return var;" assuming var is an int. You could also say "return 42;" if you wanted to return a constant.)
Logged

universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

the reason I want to return because I want copy the structure of this program, very neat and structured:

Code:
mulai:
hserout [dec jml]
portc 0# = 1
portc 0.1 = 0
portc 0.2 = 0
portc 0.3 = 0
portb=%00000000
porta = dt11
portb=%00000001
portb=%00000000
porta = dt21
portb=%00000010
portb=%00000000
porta = dt31
portb=%00000100
portb=%00000000


portc 0# = 0
portc 0.1 = 1
portc 0.2 = 0
portc 0.3 = 0
portb=%00000000
porta = dt12
portb=%00000001
portb=%00000000
porta = dt22
portb=%00000010
portb=%00000000
porta = dt32
portb=%00000100
portb=%00000000


portc 0# = 0
portc 0.1 = 0
portc 0.2 = 1
portc 0.3 = 0
portb=%00000000
porta = dt13
portb=%00000001
portb=%00000000
porta = dt23
portb=%00000010
portb=%00000000
porta = dt33
portb=%00000100
portb=%00000000


portc 0# = 0
portc 0.1 = 0
portc 0.2 = 0
portc 0.3 = 1
portb=%00000000
porta = dt14
portb=%00000001
portb=%00000000
porta = dt24
portb=%00000010
portb=%00000000
porta = dt34
portb=%00000100
portb=%00000000
porta = dt44
portb=%00001000
portb=%00000000


tombol:
if portd.0=1 and t1=0 then
GoSub waktu1
t1 = 1
elseif portd.0=0 and t1=1 then
t1 = 0
End If

if portd.2=1 and t2=0 then
GoSub waktu2
t2 = 1
elseif portd.2=0 and t2=1 then
t2 = 0
End If

if portd.4=1 and t3=0 then
GoSub waktu3
t3 = 1
elseif portd.4=0 and t3=1 then
t3 = 0
End If

if portd.1=1 and t1=0 then
dt11 = 0
dt21 = 0
dt31 = 0
End If
if portd.3=1 and t1=0 then
dt12 = 0
dt22 = 0
dt32 = 0
End If
if portd.5=1 and t1=0 then
dt13 = 0
dt23 = 0
dt33 = 0
End If

if portd.6=1 and portd.0=0 and portd.2=0 and portd.4=0 then
dt11 = 0
dt21 = 0
dt31 = 0
dt12 = 0
dt22 = 0
dt32 = 0
dt13 = 0
dt23 = 0
dt33 = 0
dt14 = 0
dt24 = 0
dt34 = 0
End If

GoSub jumlah
GoTo mulai

waktu1:
dt11 = dt11 + 1
If dt11 = 10 Then
dt11 = 0
dt21 = dt21 + 1
End If
If dt21 = 10 Then
dt31 = dt31 + 1
dt21 = 0
End If
If dt31 = 10 Then
dt31 = 0
End If
Return

waktu2:
dt12 = dt12 + 1
If dt12 = 10 Then
dt12 = 0
dt22 = dt22 + 1
End If
If dt22 = 10 Then
dt32 = dt32 + 1
dt22 = 0
End If
If dt32 = 10 Then
dt32 = 0
End If
Return

waktu3:
dt13 = dt13 + 1
If dt13 = 10 Then
dt13 = 0
dt23 = dt23 + 1
End If
If dt23 = 10 Then
dt33 = dt33 + 1
dt23 = 0
End If
If dt33 = 10 Then
dt33 = 0
End If
Return

jumlah:
j1 = dt11 + dt12 + dt13
j2 = (dt21 + dt22 + dt23) * 10
j3 = (dt31 + dt32 + dt33) * 100
jml = j1 + j2 + j3
dt4 = jml / 1000
dt44 = dt4
If dt44 > dt4 Then dt44 = dt44 - 1
dtt44 = dt44 * 1000
dttt34 = (jml - dtt44)
dt3 = dttt34 / 100
dt34 = dt3
If dt34 > dt3 Then dt34 = dt34 - 1
dtt34 = dt34 * 100
dttt24 = dttt34 - dtt34
dt2 = dttt24 / 10
dt24 = dt2
If dt24 > dt2 Then dt24 = dt24 - 1
dtt24 = dt24 * 10
dt1 = dttt24 - dtt24
dt14 = dt1
If dt14 > dt1 Then dt14 = dt14 - 1
Return

it is in Basic. I hope you can get the idea why I always want return. so you can help me solve my code  smiley-cry
Logged

Pages: [1] 2   Go Up
Jump to: