Show Posts
Pages: 1 ... 15 16 [17] 18
241  Using Arduino / Programming Questions / Re: "Return" question on: March 05, 2012, 10:43:52 pm
what do you mean it is available in all my functions?
There is return in the code I just post in Basic...why do you state there is no return used in that code?
242  Using Arduino / Programming Questions / Re: "Return" question on: March 05, 2012, 10:24:44 pm
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
243  Using Arduino / Programming Questions / Re: "Return" question on: March 05, 2012, 10:08:48 pm
could you show me how you modify it?
244  Using Arduino / Programming Questions / Re: "Return" question on: March 05, 2012, 09:45:28 pm

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);
}
245  Using Arduino / Programming Questions / Re: "Return" question on: March 05, 2012, 09:17:35 pm
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?
246  Using Arduino / Programming Questions / Re: "Return" question on: March 05, 2012, 08:58:09 pm
To:WizenedEE

Could you give example by defining one of my function?
I much learn by example.
Thank you best!
247  Using Arduino / Programming Questions / Re: "Return" question on: March 05, 2012, 08:37:42 pm
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
248  Using Arduino / Programming Questions / "Return" question on: March 05, 2012, 08:34:36 pm
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
249  Using Arduino / Programming Questions / Re: many errors when compiling the C program on: March 05, 2012, 07:53:57 pm
Thank you all friends!
250  Using Arduino / Programming Questions / Re: many errors when compiling the C program on: March 05, 2012, 02:35:43 am
Quote

AWESOME!!!

I turned 42 this year. So many people just don't get it when i tell them I am the answer to Life the Universe and Everything.

woo hooo! You are...
251  Using Arduino / Programming Questions / Re: many errors when compiling the C program on: March 05, 2012, 02:34:12 am
Hmm, both codes you gave me is much different for newbie like me...
I think to save time, can I just directly learn the pattern for the IDE rather than C++ then convert to IDE C++...?

Hmmm, where I can learn C++ for Arduino IDE?
I'm so sorry coz I ask too many...
252  Using Arduino / Programming Questions / Re: many errors when compiling the C program on: March 05, 2012, 01:50:07 am
again i found problems starting to learn C++:

I write this code in my compiler Arduino IDE:

Code:
#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World!";
  return 0;
}

Then it show this:

sketch_mar05a.cpp:1:20: error: iostream: No such file or directory
sketch_mar05a.cpp: In function 'int main()':
sketch_mar05a:5: error: 'cout' was not declared in this scope

is it C or C++?or is there any wrong with the compiler?

253  Using Arduino / Programming Questions / Re: many errors when compiling the C program on: March 05, 2012, 01:42:46 am
heuu, well I must start from beginning again for C++
Thank you all very very very very much, at least now I know where I should go rather than stay fixing crazy code now.
254  Using Arduino / Programming Questions / Re: many errors when compiling the C program on: March 05, 2012, 01:28:36 am
To Nick Gammon:
Oh I think it uses C and not C++,thank you for your worth info, recommendation book to download for C++?

Can you give example in what part of the void I must put the 'int'?
And why you return 42? why not 43?where the return goes?
255  Using Arduino / Programming Questions / Re: many errors when compiling the C program on: March 05, 2012, 01:20:56 am
To pYro_65:
But I'm working with C not C++,so?
Pages: 1 ... 15 16 [17] 18