Pages: [1]   Go Down
Author Topic: menu case problem with starting up  (Read 633 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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 .
« Last Edit: January 14, 2013, 07:03:16 am by bjoang » Logged

0
Offline Offline
Shannon Member
****
Karma: 161
Posts: 10445
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to read the sticky thread about how to post a programming question...
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

dear Mark,

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

Let me now otherwise i changed a gain.

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
#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);
}
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3487
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try putting some Serial.prints at the start of loopConstrain() so that you know what the values of x, a and b are.

Are they what you expect ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear UKHelibob

Thanks for your comment.

I have put the Serial.prints after the code

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


It give me code in the serial monitor from

plus
108ENTER808108108108108108108108108108108108

And than the 108 is comming back again and again.

I am not sure how it possible that the progam is doing this every time again.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3487
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know that I said Serial.print but Serial.println would make the output easier to read.

Do you get that output even before you press any buttons ?

One thing that does look wrong is the lcdUpDownControl() function.  It is declared as returning a boolean, takes an int as an argument, adds or subtracts one to/from the int if a button is pressed and returns the int.  Zero is false and any other value is true so most of the time the function will return true.  Is that what you want to happen ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I know that I said Serial.print but Serial.println would make the output easier to read.
Even better would be something like:
Code:
Serial.print("x=");
Serial.println(x);
so you can see what a raw number corresponds to.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have do it with the code from PaulS and now it give following code back.

plus
x=1
a=0
b=8
ENTERx=8
a=0
b=8
x=3

And than a=0,b=8 and x=3 repeat again and again.

Do enybody is nowing how this is possible?

Many thanks.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Do enybody is nowing how this is possible?
Paging any psychics on the forum. Without seeing any code, do you know what is wrong? Hmmm, no answers. Can you guess what you need to do?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry PaulS I thought you understood what I meant and what code I have used.

Here's the code I've used

Code:
#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);
  Serial.begin(9600);
 
}

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;
   }
   Serial.print("x=");
  Serial.println(x);
  Serial.print("a=");
  Serial.println(a);
  Serial.print("b=");
  Serial.println(b);   
   
}
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);
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
     Serial.print("ENTER");
This should be println(), to add a carriage return/line feed.

Code:
int loopConstrain(int x,int a,int b){
if (x == a-1 || x ==255){
   return b;
 }
 if (x == b+1){
   return a;
   }
   Serial.print("x=");
  Serial.println(x);
  Serial.print("a=");
  Serial.println(a);
  Serial.print("b=");
  Serial.println(b);  
  
}
Most compilers would output a warning that says that not all paths return a value. If there no constraining taking place, there is nothing returned from this function. If there is constraining, nothing is printed.

You should endeavor to write functions that have exactly one return statement, and that should be the last statement in the function, like this:
Code:
int loopConstrain(int x,int a,int b)
{
   int constrainedValue = x;
   if (x == a-1 || x ==255)
   {
      constrainedValue = b;
   }
   else if (x == b+1)
   {
      constrainedValue = a;
   }
   Serial.print("x=");
   Serial.println(x);
   Serial.print("a=");
   Serial.println(a);
   Serial.print("b=");
   Serial.println(b);
   Serial.print("constrainedValue=");
   Serial.println(constrainedValue);
   return constrainedValue;  
}

The lack of the return statement in most instances of the call to your function meant that garbage was popped off the stack as the return value.
« Last Edit: February 12, 2013, 02:56:14 pm by PaulS » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Pauls for your quick respons.

I have take the code from you in the sketch
 
Code:
int loopConstrain(int x,int a,int b)
{
   int constrainedValue = x;
   if (x == a-1 || x ==255)
   {
      constrainedValue = b;
   }
   else if (x == b+1)
   {
      constrainedValue = a;
   }
   Serial.print("x=");
   Serial.println(x);
   Serial.print("a=");
   Serial.println(a);
   Serial.print("b=");
   Serial.println(b);
   Serial.print("constrainedValue=");
   Serial.println(constrainedValue);
   return constrainedValue;  
}

I'm not sure what happened in this piece of code
If starting now the arduino noting is on the display.
If I push first the escape button than the diplay is comming with case 0 the main page.
I whant that the arduino is directy starting with case 0.

If I looking in to the serial monitor than I see

uplus
x=1
a=0
b=8
constrainedValue=1
ENTERx=255
a=0
b=8
constrainedValue=8
x=8
a=0
b=8

After this than from constainedValue=8,x=8,a=0 and b=8 it is comming again and again.

Sorry that I don't put the hole code here there is a limit from 9500 characters.

I have change the code
Code:
int loopConstrain(int x,int a,int b){
if (x == a-1 || x ==255){
   return b;
 }
 if (x == b+1){
   return a;
   }
   Serial.print("x=");
  Serial.println(x);
  Serial.print("a=");
  Serial.println(a);
  Serial.print("b=");
  Serial.println(b);   
   
}
For the code
Code:
int loopConstrain(int x,int a,int b)
{
   int constrainedValue = x;
   if (x == a-1 || x ==255)
   {
      constrainedValue = b;
   }
   else if (x == b+1)
   {
      constrainedValue = a;
   }
   Serial.print("x=");
   Serial.println(x);
   Serial.print("a=");
   Serial.println(a);
   Serial.print("b=");
   Serial.println(b);
   Serial.print("constrainedValue=");
   Serial.println(constrainedValue);
   return constrainedValue;   
}
« Last Edit: February 12, 2013, 03:45:12 pm by bjoang » Logged

Pages: [1]   Go Up
Jump to: