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 ?