Go Down

Topic: menu case problem with starting up (Read 848 times) previous topic - next topic

bjoang

Jan 14, 2013, 11:57 am Last Edit: Jan 14, 2013, 01:03 pm by bjoang Reason: 1
I write thing a menu structuur and ther i use a case.

But now for changing the menu I use some buttons.
I use a LCDEdit and there I change my settings.
And with lcdItem I choce the case item.

Code: [Select]
void lcdDisplay(){
  if (lcdEdit == 0){
    lcdItem = loopConstrain(lcdUpDownControl(lcdItem),0,8);


I have now only the problem if I connect the power on the arduino my arduino is starting in to case 8.

I think my problem is in this part of the sketch.

Code: [Select]
int loopConstrain(int x,int a,int b){
if (x == a-1 || x ==255){
   return b;
}
if (x == b+1){
  return a;
}
}


is there anyone who can help me how I can solve this?

Many thanks.

Gr .

MarkT

You need to read the sticky thread about how to post a programming question...
[ I won't respond to messages, use the forum please ]

bjoang

dear Mark,

I have change the post and I hope that now the post is good.

Let me now otherwise i changed a gain.


PaulS

Quote
I have change the post and I hope that now the post is good.

It's not. You need to post ALL of your code.

bjoang

After some commentar I have post my code in total.

I hope that som bodycan help me now.

If I start the arduino than he come in to case 8.

I think that the porblem is on:

Code: [Select]
int loopConstrain(int x,int a,int b){
if (x == a-1 || x ==255){
   return b;
}
if (x == b+1){
   return a;
}
}


This is the total code of my menu structure.

Code: [Select]
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SPI.h>
const int  cs=8; 
LiquidCrystal_I2C lcd(0x27,16,2);

const int buttonPinUp = 2;   
const int buttonPinDown = 3;   
const int buttonPinEsc = A0;   
const int buttonPinEnter = 9;   
const int Sensormicro = A2;

boolean LastValUp = true;
boolean LastValDown = true;
boolean LastValEsc = true;
boolean LastValEnter = true;
boolean autoEnabled = true;

int lcdItem = 0;
int lcdEdit = 0;

boolean timeEnabled[] = {false,false,false};
int timeHours[] = {0,0,0};
int timeMinutes[] = {0,0,0};

int Weight = 0;
byte Value_E1 = 150;
byte Value_E2 = 100;
int C_Day=25;
int C_Month=6;
int C_Year=12;
int C_Hour=14;
int C_Min=49;
int C_second=55;

byte lcdCharUpDown[8] = {
  0b00100,
  0b01110,
  0b11111,
  0b00000,
  0b00000,
  0b11111,
  0b01110,
  0b00100
};

void setup(){
  lcd.init();
  lcd.backlight();
 
  pinMode(buttonPinUp, INPUT);
  pinMode(buttonPinDown, INPUT);
  pinMode(buttonPinEnter, INPUT);
  pinMode(buttonPinEsc, INPUT);
}

void loop(){


if (lcdEdit == 0){
    lcdItem = loopConstrain(lcdUpDownControl(lcdItem),0,8);
     }
  lcdButtonControl();
  lcdMenu();
  lcdEditValues();
}

void lcdButtonControl(){
  boolean ValEnter=digitalRead(buttonPinEnter);

  if (ValEnter !=LastValEnter){
    if (ValEnter==LOW){
      Serial.print("ENTER");
      lcd.clear();
      lcdEdit ++;
    }
  }
  LastValEnter=ValEnter;
  boolean ValEsc=digitalRead(buttonPinEsc);

  if (ValEsc !=LastValEsc){
    if (ValEsc==LOW){
      lcd.clear();
      lcdItem = 0;
    }
  }

  LastValEsc=ValEsc;
}

boolean lcdUpDownControl(int oldValue){
 
  boolean ValUp=digitalRead(buttonPinUp);


  if (ValUp !=LastValUp){
    if (ValUp==LOW){
       Serial.println("plus");
         LastValUp=ValUp;
         lcd.clear();
      return oldValue + 1;
    }
  }

  LastValUp=ValUp;

   
  boolean ValDown=digitalRead(buttonPinDown);
  if (ValDown !=LastValDown){
    if (ValDown==LOW){
        LastValDown=ValDown;
        lcd.clear();
       
          return oldValue - 1;
       
    }
  }
  LastValDown=ValDown;
  return oldValue;
}
void lcdMenu(){
  switch (lcdItem){
  case 0:

    lcd.setCursor(0,0);
    if (autoEnabled){
      lcd.print("A ");
    }                                               
    else{
      lcd.print("M ");
    }
    lcd.print("1:12:15");
    lcd.setCursor(0,1);
    lcd.print("Temp ");
    lcd.print("25");
    lcd.print(" C      ");
    delay(10);
    break;
  case 1:

    lcd.setCursor (0,0);
    lcd.print ("Time");
    break;
  case 2:

    lcd.setCursor (0,0);
    lcd.print ("Time");
    break;
  case 3:

    lcd.setCursor (0,0);
    lcd.print ("Time");
    break;
  case 4:

    lcd.setCursor (0,0);
    lcd.print ("Weight");
    break;
  case 5:

    lcd.setCursor (0,0);
    lcd.print ("Speed");
    break;
  case 6:

    lcd.setCursor (0,0);
    lcd.print ("Set time");
    break;
  case 7:

    lcd.setCursor (0,0);
    lcd.print ("Set date");
    break;
  case 8:

    lcd.setCursor (0,0);
    lcd.print ("Sample run");
    break;
  }
}
void lcdEditValues(){
 
  byte timeId = 0;
  lcd.setCursor (0,1);
  switch(lcdItem){
  case 0:
  lcdEdit = 0;
  break;
  case 1:
  case 2:
  case 3:
    timeId = lcdItem-1;
    if (timeEnabled[timeId]){
      lcd.print("on");
    }
    else{
      lcd.print("off");
    }
    if (lcdEdit == 1){
      lcd.print('\x00');
      timeEnabled[timeId] = loopConstrain(lcdUpDownControl(timeEnabled[timeId]), 0, 1);
    }
    else{
      lcd.print(" ");
    }
    lcd.print(timeHours[timeId]);
    if (lcdEdit == 2){
      lcd.print('\x00');
      timeHours[timeId] = loopConstrain(lcdUpDownControl(timeHours[timeId]),0,23);//constrain(, 0, 23);
    }
    else{
      lcd.print(" ");
    }
    lcd.print(":");
    lcd.print(timeMinutes[timeId]);
    if (lcdEdit == 3){
      lcd.print('\x00');
      timeMinutes[timeId] = loopConstrain(lcdUpDownControl(timeMinutes[timeId]), 0, 59);
    }
    else{
      lcd.print(" ");
    }
    if (lcdEdit == 4){
      lcdEdit = 0;
    }
    break;
   case 4:
    lcd.print(Weight);
    if (lcdEdit == 1){
      lcd.print('\x00');
      Weight = loopConstrain(lcdUpDownControl(Weight), 0, 1000);
    }
    if (lcdEdit == 2){
      lcdEdit = 0;
    }
    break;
    case 5:
    lcd.print(Value_E2);
    if (lcdEdit == 1){
      lcd.print('\x00');
      Value_E2 = loopConstrain(lcdUpDownControl(Value_E2), 0, 255);
    }
    if (lcdEdit == 2){
      lcdEdit = 0;
    }
    break;
    case 6:
      lcd.print(C_Hour);
    if (lcdEdit == 1){
      lcd.print('\x00');
      C_Hour = loopConstrain(lcdUpDownControl(C_Hour), 0, 23);
    }
     else{
      lcd.print(" ");
    }
    lcd.print(":");
    lcd.print(C_Min);
    if (lcdEdit == 2){
      lcd.print('\x00');
      C_Min = loopConstrain(lcdUpDownControl(C_Min), 0, 59);
    }
    if (lcdEdit == 3){
      SetTimeDate(C_Day,C_Month,C_Year,C_Hour,C_Min,C_second);
      lcdEdit = 0;
    }
    break;
    case 7:
      lcd.print(C_Day);
    if (lcdEdit == 1){
      lcd.print('\x00');
      C_Day= loopConstrain(lcdUpDownControl(C_Day), 1, 31);
    }
     else{
      lcd.print(" ");
    }
    lcd.print("/");
    lcd.print(C_Month);
    if (lcdEdit == 2){
      lcd.print('\x00');
      C_Month = loopConstrain(lcdUpDownControl(C_Month), 1, 12);
    }
         else{
      lcd.print(" ");
    }
    lcd.print("/");
    lcd.print(C_Year);
    if (lcdEdit == 3){
      lcd.print('\x00');
      C_Year = loopConstrain(lcdUpDownControl(C_Year), 0, 99);
    }
    if (lcdEdit == 4){
      SetTimeDate(C_Day,C_Month,C_Year,C_Hour,C_Min,C_second);
      lcdEdit = 0;
    }
    break;
    case 8:
    lcd.print("Press enter to run");
    if (lcdEdit == 1){
      lcdEdit = 0;
    }
    break;
  }
}

int loopConstrain(int x,int a,int b){
if (x == a-1 || x ==255){
   return b;
}
if (x == b+1){
   return a;
}
}
int RTC_init(){
  pinMode(cs,OUTPUT);
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE3); 
  digitalWrite(cs, LOW); 
  SPI.transfer(0x8E);
  SPI.transfer(0x60);
  digitalWrite(cs, HIGH);
  delay(10);
}
//=====================================
int SetTimeDate(int d, int mo, int y, int h, int mi, int s){
int TimeDate [7]={s,mi,h,0,d,mo,y};
for(int i=0; i<=6;i++){
if(i==3)
i++;
int b= TimeDate[i]/10;
int a= TimeDate[i]-b*10;
if(i==2){
if (b==2)
b=B00000010;
else if (b==1)
b=B00000001;
}
TimeDate[i]= a+(b<<4);
 
digitalWrite(cs, LOW);
SPI.transfer(i+0x80);
SPI.transfer(TimeDate[i]);       
digitalWrite(cs, HIGH);
  }
}
//=====================================
String ReadTimeDate(){
String temp;
int TimeDate [7];
for(int i=0; i<=6;i++){
if(i==3)
i++;
digitalWrite(cs, LOW);
SPI.transfer(i+0x00);
unsigned int n = SPI.transfer(0x00);       
digitalWrite(cs, HIGH);
int a=n & B00001111;   
if(i==2){
int b=(n & B00110000)>>4;
if(b==B00000010)
b=20;       
else if(b==B00000001)
b=10;
TimeDate[i]=a+b;
}
else if(i==4){
int b=(n & B00110000)>>4;
TimeDate[i]=a+b*10;
}
else if(i==5){
int b=(n & B00010000)>>4;
TimeDate[i]=a+b*10;
}
else if(i==6){
int b=(n & B11110000)>>4;
TimeDate[i]=a+b*10;
}
else{
int b=(n & B01110000)>>4;
TimeDate[i]=a+b*10;
}
}
       
temp.concat(TimeDate[4]);
temp.concat("/") ;
temp.concat(TimeDate[5]);
temp.concat("/") ;
temp.concat(TimeDate[6]);
temp.concat("  ") ;
temp.concat(TimeDate[2]);
temp.concat(":") ;
temp.concat(TimeDate[1]);

  return(temp);
}

Go Up