Pages: 1 [2]   Go Down
Author Topic: Buttons for different display modes  (Read 1478 times)
0 Members and 1 Guest are viewing this topic.
Norfolk UK
Offline Offline
Edison Member
*
Karma: 66
Posts: 2467
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post you current code please. I don't have a LCD but I can substitute using Serial. The code should start in displayMode 0 and be updated every 2 seconds if no button pressed.

EDIT Just found bug after simulation. You need to change
Code:
byte displayMode = -1;                     // Assume no change in display mode
to
Code:
int displayMode = -1;                     // Assume no change in display mode
« Last Edit: November 09, 2012, 08:30:24 am by Riva » Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Its tricky when one word or just a capital letter can stuff things up, Ive added in some code for my 3rd battery,
 This is how it stands so far,

Code:
#include <LiquidCrystal.h> //import the Class for the LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //pin numbers for the LCD

const int switchPin = A0;                       // Select button is connected to pin
const long refreshDelay = 2000;                 // Duration between display updates

byte oldDisplayMode = 0;                        // Old display mode

int val; // variable for reading the pin status
int val2; // variable for reading the delayed status

int primary_battPin = 1; // Input from primary Battery
int secondary_battPin = 2; // Input from secondary Battery
int camper_battPin = 3; //Input from camper battery

int primary_val = 0; //temporarily store the primary input
int secondary_val = 0; //temporarily store the secondary input
int camper_val = 0;//  //temporarily store the Camper input

float primary_pinVoltage = 0; // variable to hold the calculated voltage
float secondary_pinVoltage = 0; // variable to hold the calculated voltage
float camper_pinVoltage =0; // variable to hold the calculated voltage

float primary_batteryVoltage = 0; //Temporarily store the primary Voltage
float secondary_batteryVoltage = 0; //Temporarily store the secondary Voltage
float camper_batteryVoltage = 0; //Temporarily store the camper Voltage

float ratio = 4.039; // The multiplier depending on which resistors used
// I used 68K and 22K 1/2 watt resistors.

void setup() {
    pinMode(switchPin, INPUT);      // Set the switch pin as input
    lcd.begin(20, 4);
}

void loop(){
       
    long currentTime = millis();                    // Get current time
   
    while ((millis() - currentTime) < refreshDelay){
        int buttonValue = analogRead(switchPin);    // buttonValue should be between 0 and 1023
        int displayMode = -1;                      // Assume no change in display mode
        if (buttonValue > 159) {
            displayMode = 3;
        }
        if (buttonValue > 340) {
            displayMode = 2;
        }
        if (buttonValue > 529) {
            displayMode = 1;
        }
        if (buttonValue > 792) {
            displayMode = 0;
        }
        if (displayMode != -1) {                    // Was button pressed?
            if (displayMode != oldDisplayMode) {    // Has display mode changed?
                oldDisplayMode = displayMode;       // Set new display mode
                break;                              // Exit while loop early as button pressed
            }
        }
    }
   
    switch (oldDisplayMode) {
        case 0:
           lcd.setCursor(0, 0);// Place the Cursor on the Fist line first character position on the LCD screen
            lcd.print ("Hello:");// Heading
            break;
        case 1:
  primary_val = analogRead(primary_battPin); //Read the primary Battery input per loop
            secondary_val = analogRead(secondary_battPin); //Read the secondary Battery input per loop
            camper_val = analogRead(camper_battPin); //Read the Camper Battery input per loop
           
            primary_pinVoltage = primary_val * 0.00488; //0.00488mV per 1024 steps for 5 Volts (ie; 5 / 1024 = 0.00488)
            secondary_pinVoltage = secondary_val * 0.00488;
            camper_pinVoltage = camper_val * 0.00488;
            primary_batteryVoltage = primary_pinVoltage * ratio; //grab the calculated under 5Volt input and multiply it by the ratio to ..........
            secondary_batteryVoltage = secondary_pinVoltage * ratio; // .............take it back to the 11~12~13Volt Range.
            camper_batteryVoltage = secondary_pinVoltage * ratio;
           
            lcd.setCursor(0, 0);// Place the Cursor on the Fist line first character position on the LCD screen
            lcd.print ("Batteries:");// Heading
            lcd.setCursor(0, 1);    // First Line
            lcd.print("Primary  : "); // Second Line
            lcd.print(primary_batteryVoltage); //The Voltage of Primary Battery.. S
            lcd.setCursor(0, 2);// Place the Cursor on the Second line first character position on the LCD screen
            lcd.print("Secondary: "); // Third Line
            lcd.print(secondary_batteryVoltage); //The Voltage of Secondary Battery.
             lcd.setCursor(0, 4);    // First Line
            lcd.print("Camper   : "); // The Voltage of the Battery in the camper.
        break;
        case 2:
               lcd.clear();
        lcd.setCursor(0, 0);// Place the Cursor on the Fist line first character position on the LCD screen // Put display mode 1 code here
        lcd.print ("temps:");
        break;
        case 3:
        // Put display mode 3 code here
        break;
    }
}



Logged

Norfolk UK
Offline Offline
Edison Member
*
Karma: 66
Posts: 2467
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Its tricky when one word or just a capital letter can stuff things up, Ive added in some code for my 3rd battery,
 This is how it stands so far,
Does it work as expected now with the byte to int change?
Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep, Works Great now smiley

My next step is going tobe doing temps.

Just had a bit of an idea today, How hard would it be to:

1. Make the Arduino Switch a relay when a voltage is reached in the primary battery.
2. The Arduino knows when I have the car on.

Im guessing for 1 ill have to tell it something along the lines of, When Primary = (desired voltage), Output on an analog or digital pin?

and for 2 just have a voltage comes onto a pin and the arduino can read that ?

Cheers
Logged

Norfolk UK
Offline Offline
Edison Member
*
Karma: 66
Posts: 2467
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep, Works Great now smiley

My next step is going tobe doing temps.

Just had a bit of an idea today, How hard would it be to:

1. Make the Arduino Switch a relay when a voltage is reached in the primary battery.
2. The Arduino knows when I have the car on.

Im guessing for 1 ill have to tell it something along the lines of, When Primary = (desired voltage), Output on an analog or digital pin?

and for 2 just have a voltage comes onto a pin and the arduino can read that ?
Pretty much like that. For the relay you would use digitalWrite. A good idea would be to program a slight dead zone/overlap so relay would come on a say 8V but only go off at 7V this could/would reduce relay flutter around the target voltage.

Most car ignitions have a voltage output that is only active when ignition is on. You could use this for question 2 but remember Arduino can only handle 5V where a car is most likely 12V
Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

Pages: 1 [2]   Go Up
Jump to: