Go Down

Topic: Beginner needing help with building a menu for standalone system (Read 1 time) previous topic - next topic

RMPictures

Statements like this really concern me, as it usually means you have a bunch of examples working and just want to throw it all into one sketch tweaking a few things along the way. This is a terrible way to start a project and will always lead to more headaches than necessary.

You've already broke it down nicely into a handful of steps, so start by getting your company logo to display for 3 seconds and go from there. You can probable get the menu's working first without touching the motors.


No, that is not what I'm doing.  I DID open the the Adafruit Library and have modified it to set the LCD color, display my company name for 3 seconds and then display "Main Menu".  From there, I don't know how to proceed to make it actually call a function.  Sure, I can make it display something else on the screen if I press any of the buttons, but that's just making it say something on the screen.  It isn't calling to a function.  This is what I need help with.

Is this where I need to incorporate an array?

Arrch


Statements like this really concern me, as it usually means you have a bunch of examples working and just want to throw it all into one sketch tweaking a few things along the way. This is a terrible way to start a project and will always lead to more headaches than necessary.

You've already broke it down nicely into a handful of steps, so start by getting your company logo to display for 3 seconds and go from there. You can probable get the menu's working first without touching the motors.


No, that is not what I'm doing.  I DID open the the Adafruit Library and have modified it to set the LCD color, display my company name for 3 seconds and then display "Main Menu".  From there, I don't know how to proceed to make it actually call a function.  Sure, I can make it display something else on the screen if I press any of the buttons, but that's just making it say something on the screen.  It isn't calling to a function.  This is what I need help with.

Is this where I need to incorporate an array?


So it's responding to button presses? Calling a function is trivial:

Code: [Select]
void loop() {
  ...
  if (someButtonIsPressed) {
    // Do stuff
    runSomeFunction();
  }
}

void runSomeFunction() {
  // Do stuff
}

luxxtek

this basic code should get you started.

works with Ardduino UNO,

LCD shield
http://www.dfrobot.com/index.php?route=product/product&product_id=51

and motor shield
http://www.dfrobot.com/index.php?route=product/product&product_id=69




Code: [Select]
/*


The circuit:
* LCD RS pin to digital pin 8
* LCD Enable pin to digital pin 9
* LCD D4 pin to digital pin 4
* LCD D5 pin to digital pin 5
* LCD D6 pin to digital pin 6
* LCD D7 pin to digital pin 7
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)

Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 25 July 2009
by David A. Mellis


http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/

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

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


//Key message
char msgs[5][17] = {
  "RIGHT pressed  ", 
  "UP pressed     ", 
  "DOWN pressed   ", 
  "LEFT pressed   ",
  "STOP pressed"};

int  adc_key_val[5] ={
  30, 150, 360, 535, 760 }; // for voltage divider
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;

int M1 = 3;
int E1 = 11;         
int rpm;
void setup() {
  Serial.begin(9600);
  // set up the LCD's number of rows and columns:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("any text here 1");
 
  delay (2000);
        lcd.setCursor(0, 1);             
   lcd.print("any text here 2"); 
     delay (2000);
//  pinMode(M1, OUTPUT);   
rpm = 70;



}

void loop()
{

  adc_key_in = analogRead(0);          // read the analog value from the keybaord keys   

  key = get_key(adc_key_in);           // call subroutine to convert analog value into key number
  if (key != oldkey)                   // if keypress is detected (key differs from oldkey)
  {
    delay(50);                         // wait for debounce time
    adc_key_in = analogRead(0);        // read the value from the sensor   
    key = get_key(adc_key_in);         // call subroutine to convert analog value into key number
    if (key != oldkey)     
    {   
      oldkey = key;
      if (key >=0){
        lcd.setCursor(0, 0);           
        lcd.print(msgs[key]);         


        lcd.setCursor(0, 1);              //line=1, x=0
        lcd.print("                ");    // clear line
        lcd.setCursor(0, 1);              //line=1, x=0lcd.print(key);                   
        lcd.print(key);                   
        Serial.println(key);             
        delay(500);                       

if (key ==1){
            rpm = rpm +5;
            if (rpm > 255) rpm = 255;
            analogWrite(E1, rpm);   //PWM Speed Control

            Serial.println(rpm);
}

if (key ==2){
            rpm = rpm -5;
            analogWrite(E1, rpm);   //PWM Speed Control

}
if (key ==0){
            digitalWrite(M1,LOW);      // set rotation direction
            analogWrite(E1, rpm);      // PWM Speed Control

}
if (key ==3){
            digitalWrite(M1,HIGH);      // set rotation direction
            analogWrite(E1, rpm);       // PWM Speed Control

}
if (key ==4){
            digitalWrite(M1,HIGH);      // set rotation direction
            analogWrite(E1, 0);         //PWM Speed Control

}
      }
    }         
  }
  else
  {
    lcd.setCursor(0, 0);               // no button pressed, display aanpassen
    lcd.print("WAIT FOR INFO   ");   
    lcd.setCursor(0, 1);             
    lcd.print("PRESS A BUTTON  ");   
  }



}
// SUBROUTINE Convert ADC value to key number

int get_key(unsigned int input)
{
  int k;                         //
  for (k = 0; k < NUM_KEYS; k++)  // increase k as long as it remains smaller than NUM_KEYS
  { 
    if (input < adc_key_val[k])  // compare input with incresing values
    { 
      return k; 
    }               
  }
  if (k >= NUM_KEYS)
    k = -1;     // No valid key pressed ,
  return k;     



}






Go Up