if,else calling a function

Can anyone suggest why this doesn't work.

I have two separate functions() to display two sets of LCD information.

void displayVolts() and void displayScore()

I am trying to call the appropriate LCD screen via an if.. else statement, based on the state of a digital pin.

if (state = HIGH)
displayScore();

else
displayVolts();

This doesn't work. Error is "displayScore" not declared in this stage.

As displayScore if a function, do I need to declare this?

Thanks

Two things...

  1. Your if is not going to work the way you want. It's missing an equals...
if (state =[glow]=[/glow] HIGH)
   displayScore();
 else 
   displayVolts();
  1. Which makes me suspect that the if you posted isn't the one in your Sketch. If you'd like help, post the actual code ... the entire Sketch. Please put "code tags" around your Sketch; click the # button above the edit window to insert the tags.

This my code so far. (it's not complete)

The addition I would like to make is to have a statement, based on the state of a digital output (HIGH or LOW), to switch between two different lcd text. If I strip out the void displayVolts and void displayScore statements the code seems to work. Except for switching between texts.

What I am trying to achieve if have the LCD display different text based on the state of a digital pin.

if state is HIGH then display xxxx else dispaly yyyy. Cant' seem to get my head around it.

#include <WString.h>
#include <Wire.h>

int lcd = 40; 
int voltage;  
int inputVoltagePin = 0;              
int val=0;
int inPin = 7;         // the number of the input pin
int ledPin = 13;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
int selPin=2;
int valjoy;

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 100;   // the debounce time, increase if the output flickers

void lcd2s_init()
{
  delay(200);
  Wire.beginTransmission (lcd); // transmit to device 0x50
  Wire.send(0x8C); // send lcd clear command
  delay(200);
  Wire.send(0x28); //backlight ON
  Wire.endTransmission(); // stop transmitting

}

void setup(){

  Serial.begin(9600);
  pinMode(inPin, INPUT);
  pinMode(ledPin, OUTPUT);
}
 
 void displayVolts(){
  // read voltage value
  int voltage=analogRead(inputVoltagePin);
  int valueVolts=voltage/102;  
  int valueHundrethsOfMilliVolts=((voltage % 102)*100)/102;
  // send formated value to serial com port
  Serial.print("Battery Volts ");
  Serial.print(valueVolts);
  Serial.print('.');
  Serial.print(valueHundrethsOfMilliVolts);
  Serial.println('V');

  //convert to ascii for LCD
  val=(voltage/10);
  char* valToAscii(float voltage);
  char ascii[10];
  itoa((int)val,ascii,10);
  delay(200);

  //display volts/temp

  Wire.begin();
  lcd2s_init();
  delay(200);

  Wire.beginTransmission(lcd); // transmit to device 0x50
  Wire.send(0x80);
  Wire.send("\fBattery Power ");
  delay(200);
  Wire.send(ascii);
  Wire.send ("%\n\n");
  Wire.endTransmission(); // stop transmitting
  delay(200);
  }

void loop()
{
  //button toggle

  reading = digitalRead(inPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
    time = millis();    
  }

  digitalWrite(ledPin, state);
  previous = reading;

  Serial.print (state);

  pinMode(selPin, INPUT);      //Set the Joystick 'Select'button as an input
  digitalWrite(selPin, HIGH);  //Enable the pull-up resistor on the select button
  if (analogRead(0)>700)
    Serial.print("UP");          //Read the position of the joysticks X axis and print it on the serial port.
  Serial.print(",");
  if (analogRead(0)<300)
    Serial.print("DOWN");        //Read the position of the joysticks X axis and print it on the serial port.
  Serial.print(","); 
  if (analogRead(1) >700)
    Serial.print("RIGHT");       //Read the position of the joysticks Y axis and print it on the serial port.
  Serial.print(","); 
  if (analogRead(1) <300)
    Serial.print("LEFT");         //Read the position of the joysticks Y axis and print it on the serial port.

  valjoy=digitalRead(selPin);
  if (valjoy==LOW)
    Serial.print("SELECT");

  delay (200);

//********************** I am lost here

if (state == HIGH)
  displayScore();
else 
  dispalyVolts();
}

void displayScore(){
  Wire.begin();
  lcd2s_init();
  delay(200);

  Wire.beginTransmission(lcd); // transmit to device 0x50
  Wire.send(0x80);
  Wire.send("\fScore is ");
  Wire.endTransmission(); // stop transmitting
  delay(200);
  }
  disp[glow]al[/glow]yVolts();

Looks like a typo, to me.

Error is "displayScore" not declared in this stage.

sp. "in this scope"

Basic idea seems sound... don't give up!

Do you really want/ need to execute lcd2s_init over and over again?

I'm not familiar with Wire or with your LCD unit, so maybe you do... but I doubt it!

===
Try something similar, but much simpler:

Put two LEDs on your Aruduino, in addition to the switch you currently use to switch between "display volts" and "display score". Use funtions, as you are already doing, but have the functions merely turn on one or the other of the LEDs.

When you have that working, you "only" have to make the functions "a little" fancier to get to where you want to be.