Taking the next step !

Hi Guys n Girls,

I am a newbie and have been programming for a couple of months. I have had success putting all of my software in the loop but am having alot of difficulty understanding functions (void myfunction()). I have gone thru alot of tutorials but they don't cover this much.
Can someone tell me why the attached code does not work as a function ? (it does work inside the loop).
I am trying to write a whole bunch of sub-routines so my loop is easier to follow and not so crowded.
Thanks

void setup ()
  {
    lcd.begin(16,2); // intiallises the LCD display and defines the size (16 Characters, 2 Lines).
    pinMode(Relay, OUTPUT); // Sets the Relay on Pin 8 to an OUTPUT.
    pinMode(AuxRelay, OUTPUT); // Sets the AuxRelay on Pin 7 to an OUTPUT.
    Serial.begin(9600); // Intialilises the Serial display for testing purposes.
  }
  
  
// Main Program - This will run thru continously until power is disconnected or reset button is pressed.  
void loop()  
  {
  TurnMeOn; 
   delay(50); // Delay for LCD display   
}


void TurnMeOn(){    
// Check the state of the Ignition and turn off backlight if off or PWM backlight and display company details.
  int Power = analogRead(A4); // Check the state of the ignition pin and store the result in "power".
    Serial.print(LDR); // Serial Monitor for testing purposes only
    Serial.println(Power); // As above
// Igniton is on - Control intensity of Backlight by reading the LDR and PWMing the Backlight.
 if (Power > 700 && LastState == HIGH) // Checks to see if the Ignition is on by looking for more then 700 DAC counts
    
    {                                   // Also checks Last State is HIGH (It has already displayed company logo.
    
      LDR = analogRead(A5); // reads the DAC Counts from Analog 5 and stores the result in LDR.
      analogWrite(BackLight, LDR / 2); // PWM's the backlight between 0 & 255.
      lcd.setCursor(0,0); // Sets the Cursor to Position 1, Line 1.
      lcd.print("section else if"); // Prints to LCD
    
    
// Ignition has just been switched ON from the OFF state, We want to throw in a little advertising.  
  if ( Power > 700 && LastState == LOW )
    {
      analogWrite(BackLight, 200); // Sets the Backlight to 3/4 brightness for logo only.
      lcd.clear(); // Clears the LCD display.
      lcd.print(" BLACKSNAKE SYS "); // Prints whats in the brackets on the LCD display.
      // Spaces  1234567890123456 , this is just to make sure we are using the correct number of characters available on the LCD.
      lcd.setCursor(0,1); // Sets the cursor the the first position on line 2. (Character, Line (16,2)).
      lcd.print("DBC MARK 4, Ver1"); // prints to LCD.
      LastState = HIGH; // Stops this from occuring every loop.
      delay(2000); // Delay's for 2 seconds whilst company logo is being displayed.
      lcd.clear(); // Clears the display.
    

// If the ignition is switched off we will turn the backlight off to reduce theft factor.   
  if ( Power  < 500 ) // Checks that the ignition is below 500 DAC counts.
    {
      analogWrite(BackLight, 0); // Sets the Backlight PWM to 0 (OFF)
      LastState = LOW; // Sets state to LOW so logo is written when ignition is turned back on
    
}}}}

I have tried it with the"{ }" as one set only and also 3 sets.

TurnMeOn ();

Without the parentheses, you're just testing the value of the function pointer and discarding the result (which would have been true).

The compiler issues a warning, but is suppressed by the IDE.

Also here

}}}}

Ugly.

So should i expect a result from this function ?
This is where I have trouble working out what can and can't be done in a function.
I want it to do whats in the function and then return to the main program, Is the code correct ?
I added the () at the end of the function call but it made no difference.
I get no display, no backlight, no nothing !
P.S. The ugly code is only cause I was trying different things, I would normally put them on separate lines :slight_smile:

So should i expect a result from this function ?

No, the function type is "void".

You need to post your code.
All of it this time.

  TurnMeOn;

This is not calling the function. So what is does, or doesn't do, is irrelevant.

As AWOL said.

I have tried it with the"{ }" as one set only and also 3 sets.

I don't understand that.

BlackSnake:
I get no display, no backlight, no nothing !

Do you get any output in the serial monitor? Would you would like to share it with us?

Now it's look good to read.

void setup ()
{
  lcd.begin(16,2); 
  pinMode(Relay, OUTPUT); 
  pinMode(AuxRelay, OUTPUT); 
  Serial.begin(9600);
}
// Main Program - This will run thru continously until power is disconnected or reset button is pressed.  
void loop()  
{
  TurnMeOn(); 
  delay(50); // Delay for LCD display   
}


void TurnMeOn(){    
  int Power = analogRead(A4); 
  Serial.print(LDR);
  Serial.println(Power); 

  if (Power > 700 && LastState == HIGH) 

  {                                   
    LDR = analogRead(A5); 
    analogWrite(BackLight, LDR / 2); 
    lcd.setCursor(0,0); 
    lcd.print("section else if"); 
    if ( Power > 700 && LastState == LOW )
    {
      analogWrite(BackLight, 200); 
      lcd.clear(); 
      lcd.print(" BLACKSNAKE SYS "); 
      lcd.setCursor(0,1); 
      lcd.print("DBC MARK 4, Ver1"); 
      LastState = HIGH; 
      delay(2000); 
      lcd.clear(); 
      if ( Power  < 500 ) 
      {
        analogWrite(BackLight, 0); 
        LastState = LOW; 
      }
    }
  }
}

Is this is full source code?

"YES" then first you face error

"NO".

You use nested IF and if result of first one if (Power > 700 && LastState == HIGH) false
it never enters to execute all other statements.

Tarce the values of Power and LastState.

Is this is full source code?

Quite clearly it isn't, which is why I asked to see all the code.

Tarce the values

?

Quite clearly it isn't, which is why I asked to see all the code.

Yes you right but i want to confirm this from boy(BlackSnake ). 8)

?

this is answer of his question "This is where I have trouble working out what can and can't be done in a function."

Thanks Nick !
Great to see your always so helpful and understand that not everyone is up to your standards...
FYI - I wrote it to work and in the void loop() it does. Then I just moved everything outside the loop to try to create it as a sub routine.
I realise the serial does not work in the posted code and was only using it when it was in the loop.
Awol, Here is the full code, If you want I can send you the earlier full working version that has some of Nicks input (It's what I am trying to re-write as it was my first real program.

/* DUAL BATTERY CONTROLLER - MARK 4. Created by Chris Cobley on 20th November, 2012.
   Essentially the same functions of the Mark 3 software but it's a full re-write using 
   all the new found programming skills. WISH ME LUCK !
   This version eliminates alot of electronic components and uses the cap sense library 
   PWM to control the backlight. 
*/

#include <LiquidCrystal.h> // Library for the Liquid Crystal Display
#include <CapacitiveSensor.h> // Library for the Cap Sense switch.

// Constants to assign digital I/O's, these will not change.
const int Relay = 8; // Relay on digital pin 8.
const int AuxRelay = 7; // Load relay on digital pin 7.
//const int Ignition = 9; // Digital input to sense when the ignition is turn on or off.
const int BackLight = 6; // Digital OUTPUT for the Pulse width modulation to control the backlight.

// Variables to store data in.
int MainVolts = 0; // Stores the DAC value from the analog input connected to the Main Battery.
int AuxVolts = 0; // Stores the DAC value from the analog input conneted to the Aux Battery.
int LDR = 0; // Stores the DAC value from the analog input connected to the Light dependent resistor.
int LastState = LOW; // Stores the previous state of the ignition, Starts in the ignition OFF position.

// Define the pins the liquid crystal display is connected too.
LiquidCrystal lcd (12, 11, 5, 4, 3, 2); 

// Setup - Define pins, intiallise display, any code that you want run only once.
// Analog inputs & PWM (analog outputs) do not need to be defined.
void setup ()
  {
    lcd.begin(16,2); // intiallises the LCD display and defines the size (16 Characters, 2 Lines).
    pinMode(Relay, OUTPUT); // Sets the Relay on Pin 8 to an OUTPUT.
    pinMode(AuxRelay, OUTPUT); // Sets the AuxRelay on Pin 7 to an OUTPUT.
    Serial.begin(9600); // Intialilises the Serial display for testing purposes.
  }
  
  
// Main Program - This will run thru continously until power is disconnected or reset button is pressed.  
void loop()  
  {
    int result = analogRead(A4); // Power
    int result1 = analogRead(A5); // LDR
    // I WONDER HOW TO PUT SPACES BETWEEN MY RESULTS, NEED TO FIND THAT OUT
   Serial.print(result); // Power in DAC Counts
   Serial.println(result1); // LDR in DAC Counts
   TurnMeOn(); // Meant to run code inside the function (TurnMeOn).
   delay(50); // Delay for LCD display   
  }


void TurnMeOn(){ 

// Check the state of the Ignition and turn off backlight if off or PWM backlight and display company details.
int Power = analogRead(A4); // Check the state of the ignition pin and store the result in "power".
// Igniton is on - Control intensity of Backlight by reading the LDR and PWMing the Backlight.
 if (Power > 700 && LastState == HIGH) // Checks to see if the Ignition is on by looking for more then 700 DAC counts
    
    {                                   // Also checks Last State is HIGH (It has already displayed company logo.
    
      LDR = analogRead(A5); // reads the DAC Counts from Analog 5 and stores the result in LDR.
      analogWrite(BackLight, LDR / 2); // PWM's the backlight between 0 & 255.
      lcd.setCursor(0,0); // Sets the Cursor to Position 1, Line 1.
      lcd.print("power off"); // Prints to LCD
    
    
// Ignition has just been switched ON from the OFF state, We want to throw in a little advertising.  
  if ( Power > 700 && LastState == LOW )
    {
      analogWrite(BackLight, 200); // Sets the Backlight to 3/4 brightness for logo only.
      lcd.clear(); // Clears the LCD display.
      lcd.print(" BLACKSNAKE SYS "); // Prints whats in the brackets on the LCD display.
      // Spaces  1234567890123456 , this is just to make sure we are using the correct number of characters available on the LCD.
      lcd.setCursor(0,1); // Sets the cursor the the first position on line 2. (Character, Line (16,2)).
      lcd.print("DBC MARK 4, Ver1"); // prints to LCD.
      LastState = HIGH; // Stops this from occuring every loop.
      delay(2000); // Delay's for 2 seconds whilst company logo is being displayed.
      lcd.clear(); // Clears the display.
    

// If the ignition is switched off we will turn the backlight off to reduce theft factor.   
  if ( Power  < 500 ) // Checks that the ignition is below 500 DAC counts.
    {
      analogWrite(BackLight, 0); // Sets the Backlight PWM to 0 (OFF)
      LastState = LOW; // Sets state to LOW so logo is written when ignition is turned back on
    
}
}
}
}

I'm thinking I need to send the analog results to the function ( kinda like void TurnMeOn(int power, int LDR); )
not sure what exactly I need to do. This is what I am thinking, let know if I am on the right track.

void loop ()
{
int a = analogRead(A4);
int b = analogRead(A5);
TurnMeOn(int Power, int LDR);

Will a be Power & b be LDR ?

 TurnMeOn(int Power, int LDR);

Did you mean

 TurnMeOn(Power, LDR);

or TurnMeOn(a, b); ?

BTW

}
}
}
}

is just as ugly as }}}} Use the IDE's auto format tools.

int LastState = LOW;

By assigning this value

if (Power > 700 && LastState == HIGH)

the result of this statment always false
it never enters to execute all other statements.

Great to see your always so helpful and understand that not everyone is up to your standards...

Lots of things go wrong here, don't worry about that. :slight_smile:

Cybernetician has a point. If you look at the way you have the code, LastState will never go high. Use the auto-indent tool, and it is more obvious:

void TurnMeOn(){ 

  // Check the state of the Ignition and turn off backlight if off or PWM backlight and display company details.
  int Power = analogRead(A4); // Check the state of the ignition pin and store the result in "power".
  // Igniton is on - Control intensity of Backlight by reading the LDR and PWMing the Backlight.
  if (Power > 700 && LastState == HIGH) // Checks to see if the Ignition is on by looking for more then 700 DAC counts

  {                                   // Also checks Last State is HIGH (It has already displayed company logo.

    LDR = analogRead(A5); // reads the DAC Counts from Analog 5 and stores the result in LDR.
    analogWrite(BackLight, LDR / 2); // PWM's the backlight between 0 & 255.
    lcd.setCursor(0,0); // Sets the Cursor to Position 1, Line 1.
    lcd.print("power off"); // Prints to LCD


    // Ignition has just been switched ON from the OFF state, We want to throw in a little advertising.  
    if ( Power > 700 && LastState == LOW )
    {
      analogWrite(BackLight, 200); // Sets the Backlight to 3/4 brightness for logo only.
      lcd.clear(); // Clears the LCD display.
      lcd.print(" BLACKSNAKE SYS "); // Prints whats in the brackets on the LCD display.
      // Spaces  1234567890123456 , this is just to make sure we are using the correct number of characters available on the LCD.
      lcd.setCursor(0,1); // Sets the cursor the the first position on line 2. (Character, Line (16,2)).
      lcd.print("DBC MARK 4, Ver1"); // prints to LCD.
      LastState = HIGH; // Stops this from occuring every loop.
      delay(2000); // Delay's for 2 seconds whilst company logo is being displayed.
      lcd.clear(); // Clears the display.


      // If the ignition is switched off we will turn the backlight off to reduce theft factor.   
      if ( Power  < 500 ) // Checks that the ignition is below 500 DAC counts.
      {
        analogWrite(BackLight, 0); // Sets the Backlight PWM to 0 (OFF)
        LastState = LOW; // Sets state to LOW so logo is written when ignition is turned back on

      }
    }
  }
}

Thanks Cyber, I change it around with the second statement and it works.. Whoo Hooo.
:wink:

I had to close off each if statement of course but now it works the same as it did in the loop..
Whoo Hooo I have finally written a successful function outside of the loop.
Your patience and advice is VERY appreciated Cyber

I had to close off each if statement of course but now it works the same as it did in the loop..
Whoo Hooo I have finally written a successful function outside of the loop.
Your patience and advice is VERY appreciated Cyber

it's my pleasure 8)

Here is my code now, if you can see any problems or have any suggestions, feel free ?

#include <LiquidCrystal.h> // Library for the Liquid Crystal Display
#include <CapacitiveSensor.h> // Library for the Cap Sense switch.

// Constants to assign digital I/O's, these will not change.
const int Relay = 8; // Relay on digital pin 8.
const int AuxRelay = 7; // Load relay on digital pin 7.
//const int Ignition = 9; // Digital input to sense when the ignition is turn on or off.
const int BackLight = 6; // Digital OUTPUT for the Pulse width modulation to control the backlight.

// Variables to store data in.
int MainVolts = 0; // Stores the DAC value from the analog input connected to the Main Battery.
int AuxVolts = 0; // Stores the DAC value from the analog input conneted to the Aux Battery.
int LDR = 0; // Stores the DAC value from the analog input connected to the Light dependent resistor.
int LastState = LOW; // Stores the previous state of the ignition, Starts in the ignition OFF position.

// Define the pins the liquid crystal display is connected too.
LiquidCrystal lcd (12, 11, 5, 4, 3, 2); 

// Setup - Define pins, intiallise display, any code that you want run only once.
// Analog inputs & PWM (analog outputs) do not need to be defined.
void setup ()
{
  lcd.begin(16,2); // intiallises the LCD display and defines the size (16 Characters, 2 Lines).
  pinMode(Relay, OUTPUT); // Sets the Relay on Pin 8 to an OUTPUT.
  pinMode(AuxRelay, OUTPUT); // Sets the AuxRelay on Pin 7 to an OUTPUT.
  //Serial.begin(9600); // Intialilises the Serial display for testing purposes.
}


// Main Program - This will run thru continously until power is disconnected or reset button is pressed.  
void loop()  
{
  //int result = analogRead(A4); // Power
  //int result1 = analogRead(A5); // LDR
  // I WONDER HOW TO PUT SPACES BETWEEN MY RESULTS, NEED TO FIND THAT OUT
  //Serial.print(result); // Power in DAC Counts
  //Serial.println(result1); // LDR in DAC Counts
  TurnMeOn(); // Meant to run code inside the function (TurnMeOn).
  delay(50); // Delay for LCD display   
}


void TurnMeOn(){ 

  // Check the state of the Ignition and turn off backlight if off or PWM backlight and display company details.
  int Power = analogRead(A4); // Check the state of the ignition pin and store the result in "power".


  // Ignition has just been switched ON from the OFF state, We want to throw in a little advertising.  
  if ( Power > 700 && LastState == LOW )
  {
    analogWrite(BackLight, 200); // Sets the Backlight to 3/4 brightness for logo only.
    lcd.clear(); // Clears the LCD display.
    lcd.print(" BLACKSNAKE SYS "); // Prints whats in the brackets on the LCD display.
    // Spaces  1234567890123456 , this is just to make sure we are using the correct number of characters available on the LCD.
    lcd.setCursor(0,1); // Sets the cursor the the first position on line 2. (Character, Line (16,2)).
    lcd.print("DBC MARK 4, Ver1"); // prints to LCD.
    LastState = HIGH; // Stops this from occuring every loop.
    delay(2000); // Delay's for 2 seconds whilst company logo is being displayed.
    lcd.clear(); // Clears the display.
  } 
  // Igniton is on - Control intensity of Backlight by reading the LDR and PWMing the Backlight.   
  if (Power > 700 && LastState == HIGH) // Checks to see if the Ignition is on by looking for more then 700 DAC counts

  {                                   // Also checks Last State is HIGH (It has already displayed company logo.

    LDR = analogRead(A5); // reads the DAC Counts from Analog 5 and stores the result in LDR.
    analogWrite(BackLight, LDR / 2); // PWM's the backlight between 0 & 255.
    lcd.setCursor(0,0); // Sets the Cursor to Position 1, Line 1.
    lcd.print("power off"); // Prints to LCD
  }
  // If the ignition is switched off we will turn the backlight off to reduce theft factor.   
  if ( Power  < 500 ) // Checks that the ignition is below 500 DAC counts.
  {
    analogWrite(BackLight, 0); // Sets the Backlight PWM to 0 (OFF)
    LastState = LOW; // Sets state to LOW so logo is written when ignition is turned back on
  }

}
#include <LiquidCrystal.h>
#include <CapacitiveSensor.h> 

const int Relay = 8; 
const int AuxRelay = 7; 
const int BackLight = 6; 


int MainVolts = 0; 
int AuxVolts = 0; 
int LDR = 0; 
int LastState = LOW; 

void setup ()
{
  lcd.begin(16,2); 
  pinMode(Relay, OUTPUT); 
  pinMode(AuxRelay, OUTPUT); 
}
// Main Program - This will run thru continously until power is disconnected or reset button is pressed.  
void loop()  
{
  TurnMeOn(); 
  delay(50); // Delay for LCD display   
}
void TurnMeOn(){    
  int Power = analogRead(A4); 
  if ( Power > 700 && LastState == LOW )
  {
    analogWrite(BackLight, 200); 
    lcd.clear(); 
    lcd.print(" BLACKSNAKE SYS "); 
    lcd.setCursor(0,1); 
    lcd.print("DBC MARK 4, Ver1"); 
    LastState = HIGH; 
    delay(2000); 
    lcd.clear(); 
  }
  else if (Power > 700 && LastState == HIGH) 
  {                                   
    LDR = analogRead(A5); 
    analogWrite(BackLight, LDR / 2); 
    lcd.setCursor(0,0); 
    lcd.print("section else if"); 
  }
  else if ( Power  < 500 ) 
  {
    analogWrite(BackLight, 0); 
    LastState = LOW; 
  }
}

Yes, it's seem good
One point that i add if elseif elseif to it because there is no need to check every "IF" if one of them true.