Going to Sub Routines

i am using a LCD to display a value from a Pot. I want to read the value from the POT and convert it to a particular time. I believe the switch statement is the best possible solution to this. however i do not knw how to move from the loop to the subroutine labeled: "pot_time_display"

LOOP:

lcd.setCursor(0, 1); // set the cursor to column 0, line 1
sensorValue = analogRead(sensorPin); // read the value from the sensor
if (analogRead(sensorPin) <= 1023) goto pot_time_display;

SUBROUTINE:

void pot_time_display ()
{
switch(sensorValue)
{
case '=0' :lcd.print("0.00");break;
case '1 < 17.05' :lcd.print("0.10");break;
case '17.05 < 34.1' :lcd.print("0.20");break;
case '34.1 < 51.15' :lcd.print("0.30");break;
case '51.15 < 68.2' :lcd.print("0.40");break;
case '68.2 < 85.25' :lcd.print("0.50");break;
case '85.25 < 102.3' :lcd.print("1.00");break;

and so forth...

lcd.setCursor(0, 1);                                                                       // set the cursor to column 0, line 1
  sensorValue = analogRead(sensorPin);                                             // read the value from the sensor
  if (analogRead(sensorPin) <= 1023)  pot_time_display (); 


better would be

Though it is difficult to imagine the circumstances where analogRead would return something > 1023.

 pot_time_display (analogRead (sensorPin)); 

SUBROUTINE:

void pot_time_display (int SensorValue)
{

the error message: expected primary expression before ')' token appears

for this line...

if (analogRead(sensorPin) <= 1023) pot_time_display ();

and do i need the whole line can i just use...

pot_time_display ();

You need to post your code.

You're going to get big problems with your switch/case.

here you go...

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int sensorPin = A0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor

#define pot_time_display;

void setup() {

lcd.begin(16, 2); // set up the LCD's number of columns and rows:

lcd.setCursor(4, 0); // Set the cursor to Column 4, Row 0
lcd.print("Welcome"); // Print "Welcome"

lcd.setCursor(0,1); // Set the cursor to Column 0, Row 1
lcd.print("Auto FollowFocus"); // Print "Auto Follow Focus"
delay(4000); // Delay
lcd.clear(); // Clear Screen

lcd.setCursor(2, 0); // set the cursor to column 2, line 0
lcd.print("Created by"); // Print "Created By"

lcd.setCursor(4, 1); // set the cursor to column 4, line 1
lcd.print("Amarjit"); // Print "Amarjit"
delay(2000); // Delay
lcd.clear(); // Clear Screen

lcd.setCursor(1, 0); // set the cursor to column 1, line 0
lcd.print("Time & Speed"); // Print "Time & Speed"
lcd.setCursor(5, 1); // set the cursor to column 5, line 1
lcd.print("Min"); // Print "Min"
lcd.setCursor(12, 1); // set the cursor to column 12, line 1
lcd.print("Deg"); // Print "Deg"

}

void loop()

{

lcd.setCursor(14,0); // set the cursor to column 14, line 0
lcd.print("A"); //Print "A"

lcd.setCursor(0, 1); // set the cursor to column 0, line 1
sensorValue = analogRead(sensorPin); // read the value from the sensor
pot_time_display (analogRead (sensorPin));

if (digitalRead(8) == HIGH) // if button is pressed
{ // then go to sub routine
enter_point_b_data();
}

}

void enter_point_b_data()
{
while(1)
{

}

}

void pot_time_display (int SensorValue)
{
switch(sensorValue)
{
case '=0' :lcd.print("0.00");break;
case '>= 1, < 17.05' :lcd.print("0.10");break;
case '>=17.05, < 34.1' :lcd.print("0.20");break;
case '>=34.1, < 51.15' :lcd.print("0.30");break;
case '>=51.15, < 68.2' :lcd.print("0.40");break;
case '>=68.2, < 85.25' :lcd.print("0.50");break;
case '>=85.25, < 102.3' :lcd.print("1.00");break;

}

}

Go back, click "modify" on your last post, highlight the code, and click on the # icon on the toolbar, then click "save".

#define pot_time_display;

What's that?

somthin i tryed to sort out the problem as it said it was not defined.

#define pot_time_display;

pot_time_display (analogRead (sensorPin));

It said "not defined", not " not #defined".

Take it out.

Please correct your code posting.

case '=0'               :lcd.print("0.00");break;

That is perfectly valid C, but it doesn't do what you want it to.

it does compile nw.

but no value is displayed in location 0, 1 as required.

lcd.setCursor(0, 1); // set the cursor to column 0, line 1
sensorValue = analogRead(sensorPin); // read the value from the sensor
pot_time_display (analogRead (sensorPin));

When posting code, please use the # icon on the editor's toolbar.

but no value is displayed in location 0, 1 as required.

See replies #3 and #7

im sorry im a new to the arduino.

i do not understand what is wrong with this..

case '=0'               :lcd.print("0.00");break;

and the lcd doesnt display the values as requested still.

thanks for helping btw

case '=0'    :

Perfectly valid C, but that isn't how a switch/case works.

Go over to the reference page at the main site.

http://arduino.cc/en/Reference/SwitchCase

i am going to have 60 different cases. should it be like this? is there a simplier way.
all im trying to do is give a particular time display as the Pot is rotated.
I plan to increase in 10 second intervals. i.e. 1.10, 1.20, 1.30... depending on the rotation of the pot.

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A0);
  // map the sensor range to a range of four options:
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

  // do something different depending on the 
  // range value:
  switch (range) {
  case 0:    // your hand is on the sensor
    Serial.println("dark");
    break;
  case 1:    // your hand is close to the sensor
    Serial.println("dim");
    break;
  case 2:    // your hand is a few inches from the sensor
    Serial.println("medium");
    break;
  case 3:    // your hand is nowhere near the sensor
    Serial.println("bright");
    break;
  }

There's two other ways you can do it:

If it both inputs and outputs a number, you can generally have a function. For example, instead of having

switch (number){
case 0: num  = 0; break;
case 1: num = 1; break;
case 2: num = 4; break;
case 3: num = 9; break;
}

you could just have num = number^2
More complicated functions should be fine as well.

The other way would be a lookup table. This would be if your inputs are sequential (0, 1, 2, 3, etc) and your outputs are all the same type (eg println("somestring");). You do this by having an array of values at the top, like:

nums [] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100};

and then simply calling num = nums[number]. You can do the same with strings.

If you are using strings, or having a lot of different values, you may want to put them in program memory. See here: Arduino Playground - PROGMEM