help getting loop to loop

Hello all. I am requesting a little help regaring a code that dosent loop. am i corect in thinking that once an if statment is proved true then it ends?

my code

#include <LiquidCrystal.h>   // include LCD library

int val = 0;
int analogPin = 0;
float counter = 0;
float seconds = 0;


// Defenitions
#define BUTTON_ADC_PIN           A0  // A0 is the button ADC input
#define LCD_BACKLIGHT_PIN         3  // D3 controls LCD backlight

#define RIGHT_10BIT_ADC           3  // right
#define UP_10BIT_ADC            142  // up
#define DOWN_10BIT_ADC          328  // down
#define LEFT_10BIT_ADC          504  // left
#define SELECT_10BIT_ADC        741  // select

#define LCD_BACKLIGHT_OFF()     digitalWrite( LCD_BACKLIGHT_PIN, LOW )
#define LCD_BACKLIGHT_ON()      digitalWrite( LCD_BACKLIGHT_PIN, HIGH ) 

;LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );   

void setup()
{
    pinMode( BUTTON_ADC_PIN, INPUT );         //ensure A0 is an input
    digitalWrite( BUTTON_ADC_PIN, LOW );      //ensure pullup is off on A0
    
    digitalWrite( LCD_BACKLIGHT_PIN, HIGH );  //backlight control pin D3 is high (on)
    pinMode( LCD_BACKLIGHT_PIN, OUTPUT );     //D3 is an output
    
    //set up the LCD number of columns and rows: 
    lcd.begin( 16, 2 );
    
    lcd.setCursor( 0, 0 );
    
    lcd.print("Select Time");
    
    lcd.setCursor (0, 1);
    
    lcd.print("Sec:");
    
    lcd.print(counter);
    
}

void loop()
{
  val = analogRead(0);
  
    if (val ==142)
      {
       seconds = counter +100;
       lcd.setCursor(4, 1);
       lcd.print(seconds/100);
       }
      
}

I would like to keep pusing the button that gives the analoug value of 142 and keep increesing the counter. any help would be apreciated :slight_smile:

Regards; Alex

It will be almost impossible to hit an exact value from an analogue input. Try a greater than some number and less than some other.

For a test, you might write the actual values that you read to some free space of your display.

add

       lcd.setCursor(12, 1);
       lcd.print(val); 
       lcd.print("  "); // to erase trailing remainders with val < 100

after the if () { .. }

BTW: if you see a constant 1.00 , that's no error. It's your code as you posted it ( you never increase counter and seconds will be the same value always )

I have done both as for analog input number less then and the whole number. It's good.

I did a code where I printed the input to the screen and the numbers at the top of the code in the define section are what I was reading off the board. So am I going to have to re write the code with something that will allow me to increase the counter by pushing the one buttn? What would I have to use?

What do you read if there's no button pressed ?

If that's > 900, then you might simply map your analogRead(0) to 5 areas
0 ... 100
100 ... 240
240 ... 380
380 ... 560
560 ... 800

using some

enum direction {RIGHT, UP, DOWN, LEFT, SELECT, NONE} dir; 
     if  ( val < 100 ) dir = RIGHT;
else if  ( val < 240 ) dir = UP;
else if  ( val < 380 ) dir = DOWN;
else if  ( val < 560 ) dir = LEFT;
else if  ( val < 800 ) dir = SELECT;
else  dir  = NONE;

After that you can use your dir variable in switch statements or check it immediately:

if ( dir == UP) 
{
    counter +=0.010; // 10 msec units
    lcd.setCursor(4, 1);
    lcd.print( counter );  // defaults to 2 digits
    delay (10);  // to have a speed of roughly  +1.00 per second
}

Edit: minimize float arithmetics, avoid unnecessary division.

Unless you're measuring how hard you push the button, the smart thing would be to set the button up as digital INPUT with the pullup on and have the button simply ground the pin. When it reads LOW, the button is pressed so increase the count.

But then you need one more bit of code to watch until the button is released before checking if it is pressed again which you probably could use now too.

thanks michael_x exactly what i was looking for

you're welcome.

BTW: I slightly modified my sample, to have less unnecessary float calcs.
If it weren't based on your code, I'd try to go for unsigend long milliseconds

BTW2: Which hardware do you use, needing 6 pins for the LCD but just one for the keypad ?

I entered this topic with completely different problem, and now I have a clear idea of what should I do to solve the problem I have..
You have no idea how your comments helped me! thanks a lot

Note: my problem was related to counters.. and now I have an idea that must work :slight_smile:

I'm using freetronics eleven (arduino uno) and there 2x16 LCD desplay with keypad. There made in aus and was easy to get my hands on. Am making my own h bridge motor driver tho :slight_smile: motor just came today so will be spending time on it. Will I be able to programm when select it pushed go.into the motor drive loop or just write the loop into the if statment regarding the select button?

drwkab:
Will I be able to programm when select it pushed go.into the motor drive loop or just write the loop into the if statment regarding the select button?

Huh?

Well whatever those words mean then yes, you can program anything you can describe.
Work on your description.

ok let me try to explane that better. sencerest apoligies that the wording was so bad :frowning:

what i am trying to figure out is weather when the if statment consering select is activated, do i need to totay back out of the loop to run the next loop or can i run a second loop within the 1st loop?

#include <LiquidCrystal.h>   // include LCD library
#include <Stepper.h>

#define STEPS 200

Stepper stepper(STEPS, 2, 12, 10, 11);


int val = 0;
int analogPin = 0;
float counter = 0;
float seconds = 0;


// Defenitions
#define BUTTON_ADC_PIN           A0  // A0 is the button ADC input
#define LCD_BACKLIGHT_PIN         3  // D3 controls LCD backlight

#define RIGHT_10BIT_ADC           3  // right
#define UP_10BIT_ADC            142  // up
#define DOWN_10BIT_ADC          328  // down
#define LEFT_10BIT_ADC          504  // left
#define SELECT_10BIT_ADC        741  // select

#define LCD_BACKLIGHT_OFF()     digitalWrite( LCD_BACKLIGHT_PIN, LOW )
#define LCD_BACKLIGHT_ON()      digitalWrite( LCD_BACKLIGHT_PIN, HIGH ) 

;
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 ); 

// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to



void setup()
{
  pinMode( BUTTON_ADC_PIN, INPUT );         //ensure A0 is an input
  digitalWrite( BUTTON_ADC_PIN, LOW );      //ensure pullup is off on A0

  digitalWrite( LCD_BACKLIGHT_PIN, HIGH );  //backlight control pin D3 is high (on)
  pinMode( LCD_BACKLIGHT_PIN, OUTPUT );     //D3 is an output

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

  lcd.setCursor( 0, 0 );

  lcd.print("Select Time");

  lcd.setCursor (0, 1);

  lcd.print("Sec:");

  lcd.print(counter);
  
  stepper.setSpeed(30);

}

void loop()
{
  val = analogRead(0);

  enum direction {
    RIGHT, UP, DOWN, LEFT, SELECT, NONE  } 
  dir; 
  if  ( val <100 ) dir = RIGHT;
  else if  (val == 142) dir = UP;
  else if  ( val == 328 )  dir = DOWN;
  else if  ( val == 504 ) dir = LEFT;
  else if  ( val == 741 ) dir = SELECT;
  else  dir  = NONE; 



  if ( dir == UP )
  {
    seconds = counter += 1000;
    delay(1000);
  }
  else if (dir == DOWN)
  {
    seconds = counter -= 1000;
    delay(1000);
  }
  else if (dir == RIGHT)
  {
    seconds = counter += 100;
    delay(1000);
  }
  else if (dir == LEFT)
  {
    seconds = counter -= 100;
    delay(1000);
  }
  else if (dir == SELECT)
  {
   { void loop()
      
      stepper.step(STEPS/10);
      delay(seconds);
      }
  }



  lcd.setCursor(4, 1);
  lcd.print(seconds/1000);


}

the problem i have right now the fault in compieling is in this section of code

else if (dir == SELECT)
  {
   { void loop()
      
      stepper.step(STEPS/10);
      delay(seconds);
      }
  }

it says expected initializer before "stepper"

sorry if its a litle had to follow.

btw: both codes work great when there run seperatley :slight_smile:

Any one sketch can only have one function in it called loop.
To combine two sketches see this link:-
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

what i am trying to figure out is weather when the if statment consering select is activated, do i need to totay back out of the loop to run the next loop or can i run a second loop within the 1st loop?

you need to work on the wording of this as well.

You can't back out of a loop. You can run one loop inside another but by loop I mean a looping statement like a 'for' or a 'while' not a function called loop. You can't define a function inside another function.

I guess you problem is the "magic keyword" void loop().

It is not a magic keyword though, but a function definition, and as such it is

  1. in the wrong place, as inside a function you may call (execute) other functions, but you can't declare new ones.
  2. has a wrong name, as each function name can be declared only once.

What's wrong if you simply say:

else if (dir == SELECT)
  {
      stepper.step(STEPS/10);
      delay(seconds);
  }

... besides the fact that delay () might not be too smart, as it makes your system unresponsive during that time.

I need the motor to keep going, not just every time I push the select button. Will look into some more reference materials. :slight_smile:

I'll throw in my 2 cents...

I have a similar shield (NuElectronics LCD+Keypad shield). The 5 buttons are connected together, and produce different readings on analog channel 0 when pressed. If no key is pressed, AD0 will read 1023.

The sketch has two sections: keyboard scanning and debouncing, and motor control.

First you analogRead(0) and map that value to a key symbol.
Then you process that pressed key information through an antidebounce mechanism.
Finally, you decide to how to change motor status (i.e. start/stop it or change its direction) based on the debouced-key information.

The stepper method to move the motor is repeatedly called at appropriate intervals (or not called if the motor is stopped) based on what the two status variables say: start/stop and direction.

I suggest you take a look at the "timing" section in the Playground. There you'll find libraries that simplify calling functions at defined time intervals.