Noob Programming Question

I just purchased a Freeduino from solarbotics in an attempt to learn C. I have alot of BASIC programming experience but I wanted to try and learn C and the Arduino Boards were a popular choice. I have a question about programming in C; In BASIC you can create subroutines that can be called anytime by the main loop of the program, but in C (I’m using the Arduino compiler by the way) I can’t find a specific command that can call other sections of the program. The closest thing I can find is the int-function command. Is there any commands or techniques to creating larger subroutine sections of code? This is probably a noob question to most of you out there ;D

Actually, if you have used the Arduino IDE you’ve already used functions / methods / subroutines.

The void setup() and void loop() are functions.

void setup(){}

void loop(){
  // ...
}

void myCodeBlock(){
  int result = add(3,4); //result is 7
}

int add(byte x,byte y){
  return x+y;
}

Arduino Reference

So putting void (routine name) calls the section of the code somewhere else in the program called myCodeBlock () right?

Well kinda. Void is a function type that returns no data. Hence:

int functionName(){
}

will return an integer value.

Void functions are used to run small portions of code that will be repeated and different times in the code. [Which you can also pass values and data too.)

/me

So putting void (routine name) calls the section of the code somewhere else in the program called myCodeBlock () right?

If you, by that, mean:

writing
void routineName(){
** //code**
}
will enable me to call that section of code, from anywhere @ global scope, by writing ‘routineName();’ then you are on the right track.

Then a return function can bring it back to the main loop right? Also, void (routine name) can also call on other void (routine names) and functions as well?

The keyword return is used to terminate a function. That is, make it return to wherever it was called from.

Try to uplad this:

byte value = 1;

void setup(){
  Serial.begin(9600);
}

void loop(){
  function1(); //this call will execute and proceed to the val = !val; line
  value++;
  value %= 3;
}

void function1(){ 
  if (value>1){
     Serial.println("Return at first if");
     return;
  }
  if(function2()){
    switch(value){
      case 0: 
        Serial.println("Return at case 0:");
        return;
      case 1:
        Serial.println("No return at case 1:");
        break;
    }
  }
  int x = multiply(value,2);
  Serial.println("Return at function end");
}

boolean function2() { return (value==0 || value==1 ); }

int multiply(byte x,byte y){ return x*y; }

(Intended) Code Analysis:

Iteration 1:
value variable is equal to 1
function1(); will execute the code ‘inside’ function1
if (value>1) evaluates false, proceed
if(function2()) calls function2 and will execute the code ‘inside’ function2
(value==0 || value==1 ) will evaluate true, becuase value is 1
if(function2()) evaluates to true
return true and proceed to execute the code ‘inside’ the if(function2()) block
switch(value) will trigger case 1: bacause value is 1 and execute the code associated with that case
case 0: break;
int x = multiply(value,2); will execute the code ‘inside’ multiply
value**2 = 12 = 2
return 2 and proceed to execute code
return void
value++; makes value equal to 2
value %= 3; //does nothing

Iteration 2:
value variable is equal to 2
function1(); will execute the code ‘inside’ function1
if (value>1) evaluates true and proceeds to execute the code ‘inside’ the if block
return void
value++; makes value equal to 3
value %= 3; makes value equal to 0

Iteration 3:
value variable is equal to 0
function1(); will execute the code ‘inside’ function1
if (value>1) evaluates false, proceed
if(function2()) calls function2 and will execute the code ‘inside’ function2
(value==0 || value==1 ) will evaluate true, becuase value is 0
if(function2()) evaluates to true
return true and proceed to execute the code ‘inside’ the if(function2()) block
switch(value) will trigger case 0: bacause value is 0 and execute the code associated with that case
case 1: return void
value++; makes value equal to 1
value %= 3; //does nothing

Jump to Iteration 1.

Which version of Basic are you familiar with? Basic has a lot of dialects, but in general I think the original poster is being confused by C’s lack of keywords. For example, in microsoft Visual basic you might have (something like):

Public flashLed(ByVal pinNo As Integer)
    rem code here for subroutine without a return value
End Sub

Public Function readButton(ByVal pinNo as Integer) As Integer
    rem code here to read and debounce a button and return the value
    Return myResult
End Function

rem main code here:
myButton = readButton(12);
if myButton <> 0 then
    Call flashLed(13)
End If

In C, this would look like this instead:

void flashLed(int pinNo) {
  // Code there doesn't return a value (that's what "void" means.)
}

int readButton(int pinNo) {
  // code here to read/debounce a button
  return myResult
}

void loop() { // the main code here
   int myButton;

   myButton = readButton(12);
   if (myButton != 0) {
      flashLed(13);
   }
}

(it’s quite possible that my VB example is all wrong; it’s not a language I use, so I’m mostly copying from online tutorials.)

I am very familiar with PBASIC with the Basic stamps and BASIC with the SX chip. Yes, the lack of keywords is confusing. :stuck_out_tongue:

I have this code here I am starting with (I just got my Freeduino) before I attempt anything more complicated. It’s supposed to have 3 LEDs and 2 pushbuttons. The LEDs have 3 sequences that are triggered by 1)Switch 1 is HIGH, 2)Switch 2 is HIGH, and 3)Any other cases. I dont know how to attatch that block of code but heres a copy&paste version;
int LED1 = 12; // LED connected to digital pin 12
int LED2 = 11; // LED connected to digital pin 11
int LED3 = 10; // LED connected to digital pin 10

int SW1 = 7; // Switch connected to pin 2
int SW2 = 8; // Switch connected to pin 3

//------------------------------------------------------------------------------------------------
void setup() // run once, when the sketch starts
{
pinMode(LED1, OUTPUT); // sets the digital pin as output
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT); // sets 3 pins as outputs for LEDs

pinMode(SW1, INPUT); // sets the digital pin as input
pinMode(SW2, INPUT); // sets 2 pins as inputs for push-button switches
}
//-------------------------------------------------------------------------------------------------

void loop() // run over and over again
{
delay(500);
//-------------------------------------Sequence 1--------------------------------------------------
if (digitalRead(SW1) == HIGH )
{ // blinks each of the 2 LEDs on and off every 1/2 second
digitalWrite(LED1, HIGH); // sets the LED on
delay(500); // waits for 1/2 a second
digitalWrite(LED1, LOW); // sets the LED off
delay(500); // waits for 1/2 a second

digitalWrite(LED2, HIGH);
delay(500);
digitalWrite(LED2, LOW);
delay(500);

digitalWrite(LED3, HIGH);
delay(500);
digitalWrite(LED3, LOW);
delay(500);

delay (1000); // Pause for time to push push-button
}
//-------------------------------------Sequence 2--------------------------------------------------
else if (digitalRead(SW2) == HIGH)
{ // blinks the LEDs with HIGH-time longer, LOW-time shorter, reverse order
digitalWrite(LED3, HIGH);
delay(750);
digitalWrite(LED3, LOW);
delay(250);

digitalWrite(LED2, HIGH);
delay(500);
digitalWrite(LED2, LOW);
delay(250);

digitalWrite(LED1, HIGH);
delay(750);
digitalWrite(LED1, LOW);
delay(250);

delay (1000); // Pause for time to push push-button
}
//-------------------------------------Sequence 3--------------------------------------------------
else
{
digitalWrite(LED1, HIGH);
delay(500);
digitalWrite(LED2, HIGH);
delay(500);
digitalWrite(LED1, LOW);
delay(500);
digitalWrite(LED3, HIGH);
delay(500);
digitalWrite(LED2, LOW);
delay(500);
digitalWrite(LED3, LOW);
delay(500);

delay (1000); // Pause for time to push push-button
}
} // end main loop

The problem is that when the buttons aren’t connected the ‘else’ case runs but sometimes randomly does one of the other sequences. Then when they are connected the ‘SW1 == HIGH’ case is favored over the ‘anything else’ sequence. The remaining case only sometimes runs. I did a continuity test on my circuitry and it seems to be fine, and i even added a hardware debouncer ciruit. Is there any bugs anyone can spot?

Oh nevermind the debouncing circuit worked I just forgot that the way I made it causes the states to be flipped so the HIGHs should have been LOWs. ;D

I see where this is coming from, I too am very familiar with BASIC and decided to “Make the switch” to C.

We’re in the same boat bro! good luck! lol

Yea! I heard the arduino is the best for learning C! cant wait 2 build robots with this beast! :wink: