Pages: [1]   Go Down
Author Topic: help with switch statement  (Read 299 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi
After I call the function getInput the system returns a 'D'
then it jumps into the second menu and tries to get values from my sensor
the problem is that
1. I want arduino to stay in this menu until I am finished
2. Is there a way that i can just jump from one menu to the other???
is there a way to get arduino to only read inputs from my sensor only at certain times?
The proble I'm having is that the sensors are continually reading these values
which is interfering with the loops
Any ideas?

Code:

void loop(){
  
  val0 = analogRead(A0);    // read the value from the sensor
  val1 = analogRead(A1);
  val2 = analogRead(A2);
  val3 = analogRead(A3);
  val4 = analogRead(A4);
  
  char menu=getInput();
  switch(menu)
  {
    case 'A': Serial.print("\n");
              Serial.println("A");  //now call second menu
              delay(5000);
              
                   char menu2=getInput2();
                   switch(menu2)
                   {
                      while(menu2!='X'){
                      case 'B': Serial.println("B");
                                 delay(5000);
                                 break;
                      
                      case 'X':Serial.println("X");
                                 delay(5000);
                                 break;
                    }
                    
                    break;
 }}
« Last Edit: August 29, 2007, 01:09:42 pm by sillyrabbit23 » Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6373
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't think that what you think is happen is what is actually happening.
You won't actually leave your loop till you fall out the back, so you should
be able to stay in your menus.  In fact, I'd be worried that you would need
to read your values more often than you are.   What does getInput() return
if there is no input at all?

In general, you'll need to think pretty carefully about how to integrate something
like a user menu that appears to wait for user input with real time code that should
be reading sensors all the time.  You may need to save the "state" of the menu
system (which characters have been typed so far, and how many are relevant)
and only process menu data when characters exist...


I'd do something like:
Code:

loop() {
  
  val0 = analogRead(A0);    // read the value from the sensor
  val1 = analogRead(A1);
  val2 = analogRead(A2);
  val3 = analogRead(A3);
  val4 = analogRead(A4);
  
  if (inputAvailable()) {
    doMenus(getInput());
  }
}
« Last Edit: August 29, 2007, 01:23:51 am by westfw » Logged

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

Yes i have coded something like this now...see below....as for getInput(), it returns a char but if there is no char returned then i have it printing out the sensors values val1,val2 etc using serial.print(). This works perfectly looping away until i want to use "do/while" condition. The values dont become real time readings, just stay static! Is there any way to code around this or even a way to keep the user in a menu until they opt out (eg letter=='X')????..

Code:

void loop(){
  
  val0 = analogRead(A0);    // read the value from the sensor
  val1 = analogRead(A1);
  val2 = analogRead(A2);
  val3 = analogRead(A3);
  val4 = analogRead(A4);
  
  letter=getInput();
  if(letter=='D')
  {
      Serial.println("D");
      
      do{
          letter=getInput2();        
      }while(letter!='X');
  }
  else
  {
    Serial.print("message");
    Serial.print("\n");
  }
}
« Last Edit: August 29, 2007, 01:03:38 pm by sillyrabbit23 » Logged

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

ok i just figured it out!! took me about 2 hours but it works now! I had to read in the values again in the do loop! thats why it was static.

Thanks a mil for your reply westfw...it always helps to get a fresh look on your code when you cant see the problem yourself!  smiley-wink
« Last Edit: August 29, 2007, 01:11:05 pm by sillyrabbit23 » Logged

Pages: [1]   Go Up
Jump to: