Go Down

Topic: Taking the next step ! (Read 1 time) previous topic - next topic

BlackSnake

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


Cybernetician

Quote

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)
From Idea To Invention

BlackSnake

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

Code: [Select]
#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
  }

}

Cybernetician

Code: [Select]

#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.
From Idea To Invention

Go Up