Show Posts
Pages: [1] 2 3
1  Using Arduino / Programming Questions / Re: lcd menu on: May 13, 2013, 02:51:59 pm
I am also very intrested how you can program this on a easy way.

I know how you can change the cases in the top level but how can I change the valeu.

For sample.
I have a menu that I must give the time.
First I must take the hours than the minute and than the seconds.

Only for the hours the valeu must going from 0 to 23 and the minute from 0 to 60 and the seconds also from 0 to 60.

But how can I spring from the top level cases to the lower valeus fro changing?
2  Using Arduino / Programming Questions / Re: lcd menu on: April 14, 2013, 01:10:09 pm
Dear DC42,

It seems to me a good alternative and have also been to watching.
The room thermostat for my heating works that way.
I just do not know what they are here for encoder to use and how to put this program.

Is it possible that you dar a sketch picture of you so I can see how I should set up.

Thank you.
3  Using Arduino / Programming Questions / Re: lcd menu on: April 10, 2013, 04:15:44 pm
@ Liudr
Yes, that is what I searching.
I can make one button more on my arduino that is now problem and than I have 5 buttons.

Do you use a special shield for the buttons?

Is on this sketch also a timer module?
I thought I saw in the beginning of the movie.

Many thanks.
4  Using Arduino / Programming Questions / Re: lcd menu on: April 10, 2013, 03:39:59 pm
Dear UkHeliBob,

I have 4 buttons up /down / enter and escape.
Many thanks.
5  Using Arduino / Programming Questions / lcd menu on: April 08, 2013, 04:14:17 pm
What is the best way to get an LCD menu buttons to program the buttons if you also want to use the buttons for changing the value of the menu item.

Thanks for the effort.
6  Using Arduino / Programming Questions / LCD Menu instable on: March 18, 2013, 05:34:12 pm
I am fighting with my menu that is controlled with 4 button.

If i go testing my sketch on the arduino than the sketch is not so stable.
I upload the sketch and the lcdItem give directly a valeu 8 and it must be 0.( row 64)

The code where i will control the loopconstrain on row 308 is the biggest problem.
There are now 2 open ends.

How can i change the code that it is working well?

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 1");
    break;
  case 2:

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

    lcd.setCursor (0,0);
    lcd.print ("Time 3");
    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);
return x; 
}


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);
}
7  Using Arduino / Programming Questions / Re: menu case problem with starting up on: February 12, 2013, 03:29:29 pm
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;   
}
8  Using Arduino / Programming Questions / Re: menu case problem with starting up on: February 12, 2013, 02:43:09 pm
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);
}
9  Using Arduino / Programming Questions / Re: menu case problem with starting up on: February 10, 2013, 04:23:34 pm
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.
10  Using Arduino / Programming Questions / Re: menu case problem with starting up on: February 07, 2013, 04:30:48 pm
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.
11  Using Arduino / Programming Questions / Re: menu case problem with starting up on: February 07, 2013, 03:39:01 pm
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);
}
12  Using Arduino / Programming Questions / Re: menu case problem with starting up on: January 14, 2013, 07:04:47 am
dear Mark,

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

Let me now otherwise i changed a gain.

13  Using Arduino / Programming Questions / menu case problem with starting up on: January 14, 2013, 05:57:12 am
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 .
14  International / Nederlands / Re: Goede opbouw programma on: September 05, 2012, 02:22:41 pm
Heel erg bedankt voor jullie antwoorden.

Ik denk alleen dat er onder subroutines meer val dan ik dacht.

Ik bedoelde meer de subroutines door middel van functions zoals ze op deze pagina omschrijven.

http://www.arduino.cc/playground/Code/Function

Als ik mijn programma zo op bouw krijg ik dan een beter werking van mijn programma.

Alvast vriendleijk bedankt.

15  International / Nederlands / Goede opbouw programma on: September 04, 2012, 03:58:30 pm
Ik was op zoek hoe ik het best een programma opbouw binnen de sketch.

Ik was een programma voor arduino aan het schrijven maar ik heb last van als ik bv een drukknop programmeer en later staat er in het programma bv een delay  dan werkt dit ook op de reactie tijd van de knop.
Ik heb al een heleboel plc geprogrammeerd en daar kan ik bepaalde stukken programma parralel naast elkaar laten werken.

Ik heb ook al zitten kijken  of ik dit eventueel op kan lossen met sub routine programmatjes maar ik weet niet of dit een goede oplossing is.

Kunne jullie mij hierbij ondersteunen of misschien een goede uitleg hoe ik dit het beste oplos.

Ik hoor het graag van jullie en alvast vriendelijk bedankt. smiley smiley smiley smiley
Pages: [1] 2 3